導航:首頁 > 源碼編譯 > 防碰撞演算法c語言代碼

防碰撞演算法c語言代碼

發布時間:2023-08-01 19:16:07

㈠ 短作業優先演算法用c語言如何寫

這樣寫應該可以:
#include<iostream.h>

#include<stdio.h>
struct pcb{
char pno;
int come_time; //到達時間
int run_time; //服務時間
};
float fcfs(pcb pro[],int n)
{
struct pcb temp;
int i,j,k; //time為當前時間
float weight_time=0,time=0; //記錄周轉時間的和
//temp=(pcb)malloc(sizeof(pcb));
cout<<"進程調度情況如下:"<<endl;
cout<<"進程號 到達時間 服務時間 周轉時間:"<<endl;
//選擇排序過程,按到達時間升序排列
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(pro[k].come_time>pro[j].come_time)
k=j;
if(k!=i)
{
temp=pro[i];
pro[i]=pro[k];
pro[k]=temp;
}
}
for(i=0;i<n;i++)
{ time+=pro[i].run_time;
weight_time+=(time-pro[i].come_time)/pro[i].run_time; //(time-pro[i].come_time)/pro[i].run_time為排序後第i個進程的周轉時間
cout<<pro[i].pno<<" "<<pro[i].come_time<<" "<<pro[i].run_time<<" "<<(time-pro[i].come_time)/pro[i].run_time<<endl;
}
return weight_time/=n; //返回平均帶權周轉時間
}
void insert(pcb pro[],pcb pro1,int start,int end)//將一pcb類型的元素插入到有序數組中,最後還保持有序
{
int i=end;
while((i--)>start)
if(pro[i].run_time>pro1.run_time)pro[i+1]=pro[i];
pro[i]=pro1;

}
float sjp(pcb pro[],int n)
{
int i,first=0,count,flag[20],k,min;
float time=0,weight_time=0;
//調度第一個到達內存的進程
for(i=1;i<n;i++)
{
if(pro[first].come_time>pro[i].come_time) first=i;
flag[i]=0;
}
flag[first]=1;
time=(float)pro[first].run_time;
weight_time=1;
cout<<pro[first].pno<<" "<<pro[first].come_time<<" "<<pro[first].run_time<<" "<<weight_time<<endl;
//pro_temp[0]=pro[first];
count=n-1;
while(count)
{
k=0;
min=32767; //設置一個較大的閾值,
for(i=0;i<n;i++) //找到一個未被訪問的,作業較短的且已經到達內存的作業調度
if((i!=first)&&(flag[i]==0)&&(time>=pro[i].come_time)&&(min>pro[i].run_time))
{
k=i;
min=pro[i].run_time;

}
flag[k]=1; //訪問後置標記為訪問
time+=pro[k].run_time;
weight_time+=(time-pro[k].come_time)/pro[k].run_time;
cout<<pro[k].pno<<" "<<pro[k].come_time<<" "<<pro[k].run_time<<" "<<(time-pro[k].come_time)/pro[k].run_time<<endl;
count--; //每調度一個作業,count減1
}
return weight_time/=n;
}

void main()
{
pcb pro[5]={{'C',2,5},{'A',0,4},{'B',1,3},{'D',3,2},{'E',4,4}};
cout<<fcfs(pro,5)<<endl;
cout<<sjp(pro,5)<<endl;
}

㈡ 關於BF演算法的C語言實現

我修改的程序都是把S[0] T[0]轉換為strlen(S) strlen(T)函數來實現的
為什麼不把strlen(S),strlen(T)分別賦予S[0],T[0],害怕覆蓋原來的數據嗎?沒有必要,他們原本就是來存儲這個數據的,君不見,它們都不參與匹配!他們的初始化應該在這個函數之外完成,在每次數組長度改變後,就及時設置,換句話說,在調用這個函數之前,應該保證他們已經設置正確,
在列印時,應該從第二個元素S[1]或T[1]開始,因為S[0],T[0]不再是數組的實際內容
不知道我有沒有表述清楚,
一般,數組的第一個元素存放實際的內容,而你這里並不是這樣,數組的第一個元素不再是數組的實際內容,而是數組長度
==================================================================
補充;
比較大小時S[0]的值不就變成了整形的ASCII碼值了么?
1.整數就是整數,沒有ASCII碼,ASCII碼是針對字元的
2.在C中,整數賦予字元變數是合法的
2.在C中,字元與整數的關系運算也是合法的,當你要把一個位元組的數解釋成字元的時候,它就是字元,可他存儲的還是數啊,就把它當整數用吧,畢竟我們沒有打算列印它,當然它能表示的整數太少了,所以數組長度受到限制
如果你要以字元顯示它,那它當然是那個整數所對應的字元,如果那是可列印字元的話

㈢ 這題的c語言源代碼,還有解題思想,隨機化演算法,麻煩手打,謝謝

//隨機化演算法用隨機投點法計算定積分

#include<stdio.h>
#include<math.h>
#include<time.h>//使用當前時鍾做種子

doubleDarts(intn,doublea,doubleb);
doublef(doublex);//積分函數

main(){
inti,n[5]={100,1000,1000,10000,10000000};//隨機投點個數,個數越多結果越精確
doublea=1.0,b=2.0;//積分上下界
srand((unsigned)time(NULL));//初始化隨機數
for(i=0;i<5;i++)
printf("%d: n=%d r=%lf ",i+1,n[i],Darts(n[i],a,b));
}

/*基本思想是在矩形區域內隨機均勻投點,求出由這些點
*產生的函數值的算術平均值,再乘以區間寬度,即可得
*出定積分的近似解
*/
doubleDarts(intn,doublea,doubleb)
{
inti;
doublesum=0.0;
for(i=0;i<n;i++){
doublex=(b-a)*rand()+a;//產生[a,b)之間的隨機數
sum=sum+f(x);
}
return(b-a)*sum/n;
}

doublef(doublex){
returnsin(x)/x;
}

㈣ 請教MD5演算法 用C語言實現

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifdefined(__APPLE__)
#defineCOMMON_DIGEST_FOR_OPENSSL
#include<CommonCrypto/CommonDigest.h>
#defineSHA1CC_SHA1
#else
#include<openssl/md5.h>
#endif

//這是我自己寫的函數,用於計算MD5
//參數str:要轉換的字元串
//參數lengthL:字元串的長度可以用strlen(str)直接獲取參數str的長度
//返回值:MD5字元串
char*str2md5(constchar*str,intlength){
intn;
MD5_CTXc;
unsignedchardigest[16];
char*out=(char*)malloc(33);

MD5_Init(&c);

while(length>0){
if(length>512){
MD5_Update(&c,str,512);
}else{
MD5_Update(&c,str,length);
}
length-=512;
str+=512;
}

MD5_Final(digest,&c);

for(n=0;n<16;++n){
snprintf(&(out[n*2]),16*2,"%02x",(unsignedint)digest[n]);
}

returnout;
}

intmain(intargc,char**argv){
char*output=str2md5("hello",strlen("hello"));

printf("%s ",output);
//上面會輸出hello的MD5字元串:
//
free(output);
return0;
}

㈤ 演算法編程:用c語言實現

解決這類問題可以使用 回溯 演算法,代碼如下:

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

#defineM6//候選數字個數
#defineN5//組合後數字位數

intcheck(intresult[],inti)
{
for(intj=0;j<N;j++)
if(result[j]==i)
return0;

return1;
}

intlist(intnumbers[],intl,intresult[],intcount)
{
if(l>=N){

//將各位數組合成一個數
intnum=0;
for(inti=0;i<N;i++){
num=num*10+numbers[result[i]];
}

//判斷這個數是否能被75整除
if(num%75==0){
printf("%d ",num);
count++;
}

returncount;
}

for(inti=0;i<M;i++){

if(!check(result,i)){
continue;
}

result[l]=i;

count=list(numbers,l+1,result,count);

result[l]=-1;
}

returncount;
}

intmain()
{
intnumbers[M]={1,2,5,7,8,9};
intresult[N]={-1,-1,-1,-1,-1};

intcount=list(numbers,0,result,0);

printf("共有%d個 ",count);

system("pause");
return0;
}

運行結果:

閱讀全文

與防碰撞演算法c語言代碼相關的資料

熱點內容
PDF分析 瀏覽:482
h3c光纖全工半全工設置命令 瀏覽:137
公司法pdf下載 瀏覽:379
linuxmarkdown 瀏覽:347
華為手機怎麼多選文件夾 瀏覽:679
如何取消命令方塊指令 瀏覽:345
風翼app為什麼進不去了 瀏覽:774
im4java壓縮圖片 瀏覽:358
數據查詢網站源碼 瀏覽:146
伊克塞爾文檔怎麼進行加密 瀏覽:886
app轉賬是什麼 瀏覽:159
php的基本語法 瀏覽:792
對外漢語pdf 瀏覽:516
如何用mamp本地web伺服器 瀏覽:869
如何加密自己js代碼 瀏覽:627
排列組合a與c的演算法 瀏覽:534
如何在文件夾中找到同名內容 瀏覽:786
有什麼app文字轉韓文配音 瀏覽:372
循環宏1命令 瀏覽:35
斐波那契數列矩陣演算法 瀏覽:674