㈠ 短作业优先算法用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;
}
运行结果: