當前位置:首頁 » 操作系統 » 演算法實現

演算法實現

發布時間: 2022-01-23 02:43:39

① 設計演算法,並用c語言實現。

#include<stdio.h>

intchange(intamount,intindex,intconstcoins[]){
if(amount==0)return1;
if(index<=0)return0;
for(inti=amount/coins[index-1];i>=0;--i){
if(change(amount-i*coins[index-1],index-1,coins)){
if(i)
printf("%d*%d",i,coins[index-1]);
return1;
}
}
return0;
}

intmain()
{
intcoins[]={20,50};
intconstsize=sizeof(coins)/sizeof(int);
intamount;
for(inti=0;i<size;++i){
for(intj=i+1;j<size;++j){
if(coins[i]>coins[j]){
inttemporary=coins[i];
coins[i]=coins[j];
coins[j]=temporary;
}
}
}
if(coins[0]<=0){
printf("數據有誤,零錢必須大於0 ");
return-1;
}
printf("請輸入要兌換的貨幣金額:");
scanf("%d",&amount);
if(change(amount,size,coins))
printf(" 兌換成功 ");
elseprintf(" 兌換失敗 ");
return0;
}

② JAVA 實現演算法

編碼是不能用字元串的,大大大降低速度

public class Test{
static public int getIntegerComplement(int n){
return ~n&((1<<(32-Integer.numberOfLeadingZeros(n)))-1);
}
public static void main(String[] args){
int a[]={1,5,50,256,65536};
for(int i:a){
int r=getIntegerComplement(i);
System.out.println(i+" "+Integer.toBinaryString(i)+
" => "+r+" "+Integer.toBinaryString(r));
}
}
}
========
1 1 => 0 0
5 101 => 2 10
50 110010 => 13 1101
256 100000000 => 255 11111111
65536 10000000000000000 => 65535 1111111111111111

③ 實現演算法:

輸出成文件

C++實現

代碼:

#define_author"Reskip"

#define_CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>

usingnamespacestd;

intencode(intx)
{
returnx*x+x*3+5;
}

intmain()
{
stringinput;
cin>>input;

freopen("save.txt","w",stdout);
for(chari:input)
{
cout<<encode(i)<<"";
}
return0;
}

④ 演算法如何實現

我的方法比較笨...只限4個數,沒有第一個的大哥哥寫的好,第2個弟弟寫的是錯的,偶試過~我寫的為:
main()
{
char a,b,c,d,e,f,g,h;
printf("請輸入4個數");
scanf("%c,%c,%c,%c\n",&a,&b,&c,&d);
a=e;
b=f;
c=g;
d=h;
printf("四個數倒過來讀為:%c,%c,%c,%c\n",h,g,f,e);
}
(僅供參考)不過給我分我也不介意~~呵呵~~~

⑤ 關於演算法編程題(C語言實現)

char *a; //字元串 改為 char a[20];//存放字元串的字元數組
int jie; //方程的解 改為 double jie;
dy = 0; 刪去 dy=0;
兩處的 for(i=1;i<=z;i++) 都改為 for(i = 0; i < z; i++)
if (a[i] == '==' ) 改為 if (a[i] == '=' )
{z=i; 改為 {dy = i;
a=0;b=0; 刪去 a=0;b=0;
fun(a,1,dy,&b,&c); 改為 fun(a, 0, dy - 1, &b, &c);
fun(a,dy,z,&b,&c); 改為 fun(a, dy + 1, z - 1, &b, &c);
jie=(d-b)/(e-c); 改為 jie=((double)(d-b))/(e-c);
printf("%c = %d",zm,jie); 改為 printf("%c = %f",zm,jie);

⑥ 怎樣實現以下演算法

死循環相信沒什麼問題,這個題目就是隨機生成數據,然後找出是否有相同的數據的過程。如果是簡單的循環類代碼,就是類似窮舉法,一個個比較的過程;但是如果寫的優雅一些,實際上是一個排序過程。

我們來回憶一下排序的過程,從最簡單的冒泡、選擇,到最高級的快排、歸並,每一個排序實際上都進行了比較。那麼我們只需要在自定義的比較函數中對值進行輸出即可。

一般語言中都會內置排序函數,例如C的qsort、JavaScript的Array.prototype.sort、PHP的usort等。都支持傳入自定義的比較函數。下面以C舉例:

#include<stdlib.h>
#include<stdio.h>

#defineNUM20

inthas_equals=0;

typedefstructitem{/*數據元素結構體*/
intval;/*整數值,用於比較*/
intidx;/*記錄初始化時的位置,用於輸出用*/
}Item;
voidinitData(Item*arr,intlen){
inti;
printf("======RANDOMDATA====== ");
for(i=0;i<len;i++){
arr[i].val=rand()%100;/*一百以內的隨機數*/
printf("%d",arr[i].val);
arr[i].idx=i;
}
printf(" ");
}
intmyCmp(constvoid*va,constvoid*vb){
Item*a=(Item*)va;
Item*b=(Item*)vb;
if(a->val==b->val){/*兩個值相等,輸出*/
printf("FoundEquals:items[%d]==items[%d]==%d ",
a->idx,b->idx,a->val);
has_equals=1;/*標記找到相等的值*/
}
returna->val-b->val;
}
voidmain(){
Itemitems[NUM];
while(1){
has_equals=0;/*重置找到重復標記*/
initData(items,NUM);
qsort(items,NUM,sizeof(Item),myCmp);
if(!has_equals){
printf("++++++EveryItemisunique++++++ ");
break;
}
}
printf("Bye. ");
}

當數據有重復的時候會輸出重復項,並標記重復標志。C語言中沒有Exception機制,所以排序完成後再進行處理,不能中斷排序過程(也可以使用LongJump)。其他語言中都有Exception機制,可以直接在檢查到第一個相等之後就拋異常,外層捕獲異常做邏輯即可。

⑦ 的實現演算法是怎樣的

我們要寫個class,實現如下主程序調用:

static void Main(string[] args)
{
MyHash hash = new MyHash();
hash["A1"] = DateTime.Now;
hash["A2"] = 1;
Console.WriteLine(Convert.ToString(hash["A1"]));
Console.WriteLine(Convert.ToString(hash["A2"]));
}

一看,也確實挺簡單的,就是一個所引器,如下:

class MyHash
{
public object this[string key]
{
get
{
}
set
{
}
}
}

程序中要保存的對象,最終是要保存在一個數組中的,而且需要通過一個轉換函數來進行string key與數組Index的Map,如下:

private List<List<Tuple<string, object>>> lstArray = new List<List<Tuple<string, object>>>(defaultSize);

private int MapString2Int(string key)
{
int hashIndex=0;
char[] keyAry = key.ToCharArray();
foreach (var c in keyAry)
hashIndex += (int)c;

hashIndex = hashIndex % lstArray.Capacity;
return hashIndex;
}

這個函數是遍歷string key的每個char,累加,最終取模(同數組的長度),這樣得出的一個value肯定就在數組范圍內。
如果2個key轉換出來的index相同呢?會導致沖突,一個最簡單的解決辦法是把數組中的每個元素變成List, 也就是說,如果string key轉換後出現了相同的Index,沒關系,只要把那2個元素都放在那個Index所標識的數組位置中即可,本文中用的是List<Tuple<string, object>>。
下面是整個程序的代碼:

class Program
{
static void Main(string[] args)
{
MyHash hash = new MyHash();
hash["A1"] = DateTime.Now;
hash["A2"] = 1;
Console.WriteLine(Convert.ToString(hash["A1"]));
Console.WriteLine(Convert.ToString(hash["A2"]));
}
}

class MyHash
{
private const int defaultSize = 99999;
private List<List<Tuple<string, object>>> lstArray = new List<List<Tuple<string, object>>>(defaultSize);

public MyHash()
{
int i = lstArray.Capacity;
while(i>=0)
{
lstArray.Add(new List<Tuple<string,object>>());
i--;
}
}

public object this[string key]
{
get
{
EnsureNotNull(key);

List<Tuple<string, object>> lst;
Tuple<string, object> obj = FindByKey(key, out lst);
if (obj == null)
throw new Exception("Key不存在");

return obj.Item2;
}
set
{
EnsureNotNull(key);

List<Tuple<string, object>> lst;
Tuple<string, object> obj = FindByKey(key, out lst);
if (obj!=null)
lst.Remove(obj);

lst.Add(new Tuple<string, object>(key, value));
}
}

private Tuple<string, object> FindByKey(string key, out List<Tuple<string, object>> lst)
{
int hashIndex = MapString2Int(key);
lst = lstArray[hashIndex];
Tuple<string, object> obj = null;
for (var i = 0; i < lst.Count; i++)
{
if (lst[i].Item1 == key)
{
obj = lst[i];
break;
}
}

return obj;
}

private static void EnsureNotNull(string key)
{
if (key == null || key.Trim().Length == 0)
throw new Exception("Key不能為空");
}

private int MapString2Int(string key)
{
int hashIndex=0;
char[] keyAry = key.ToCharArray();
foreach (var c in keyAry)
hashIndex += (int)c;

hashIndex = hashIndex % lstArray.Capacity;

Console.WriteLine(string.Format("{0}相應的Index為:{1}", key, hashIndex));

return hashIndex;
}
}

⑧ 演算法實現

多看一些源代碼最好,先動手編一些簡單的程序,程序能力的提高都是靠看靠編出來的,理論不用很到位的面面具到的都非常懂會用最好,遇到問題會查閱資料。參考書籍的話:c就是譚浩強那本比較經典了,外國書是好但看起來費勁。c++就清華的潛能也不錯。你直接學《數據結構和演算法分析設計》這門課不太合理,因為得有一些基礎和編程經驗在學會更好一些。
多去csdn學習,多尋找一些好的源代碼看看。這樣學起來很快而且不枯燥,如果一味得看書你會煩得不行也沒有盡頭,邊學邊練習會極大增強信心。

熱點內容
筆記本什麼配置能流暢運行cf 發布:2024-09-20 00:14:19 瀏覽:951
實測華為編譯器 發布:2024-09-19 23:50:52 瀏覽:821
linux匯總 發布:2024-09-19 23:46:39 瀏覽:452
阿里雲伺服器環境搭建教程 發布:2024-09-19 23:21:58 瀏覽:837
黃色文件夾圖標 發布:2024-09-19 23:19:22 瀏覽:684
mysql資料庫導出導入 發布:2024-09-19 23:00:47 瀏覽:183
lua腳本精靈 發布:2024-09-19 23:00:41 瀏覽:659
任務欄文件夾圖標 發布:2024-09-19 22:54:25 瀏覽:101
解壓來一波 發布:2024-09-19 22:46:36 瀏覽:933
mysqlpythonubuntu 發布:2024-09-19 22:46:27 瀏覽:501