给同学的配水问题代码

/****************************
writen by DY
*****************************/ 


#include"stdio.h" 
int Compare(double *tmp);//比较四个数的大小,返回最大值所在的位置 
int main(){
double usr1[1000] = {


};

double usr2[1000] = {


};
double usr3[1000] = {


};
double usr4[1000] = {
};



int cont1 = 0;//记录用户1当前边际效益值所在的位置 ,下同 
int cont2 = 0;
int cont3 = 0;
int cont4 = 0;
int i;
double tmp[4];//4个要比较的临时变量 

FILE *fp;//文件指针 
  fp = fopen( "D:\\result.txt", "w+" );//结果放在D盘根目录result.txt里 
 
for(i = 0; i < 851; i++){//进行循环,有多少行循环多少次,根据情况自己写 
tmp[0] = usr1[cont1];
tmp[1] = usr2[cont2];
tmp[2] = usr3[cont3];
tmp[3] = usr4[cont4];
fprintf(fp, "%lf,%lf,%lf,%lf\n",tmp[0],tmp[1],tmp[2],tmp[3]);//每次比较的四个数 
int position = Compare(tmp);//取得最大数,并返回是哪个用户 
fprintf(fp,"配给用户%d\n",position+1); 
switch(position){// 判断分配给哪个用户,然后找到该用户的下一个边际变量的位置 
case 0:
cont1++;
break;
case 1:
cont2++; 
break;
  case 2: 
  cont3++;
  break;
   case 3:
  cont4++;
  break;
}
}//end of for     
  fclose(fp);//关闭文件          
return 0;
}
/*
 * 函数功能:比较四个数大小,并返回最大数的位置  
*/ 
int Compare(double *tmp){ 
double a = -100.0;
int position;
int i;
for(i = 0; i < 4; i++){
if(a < tmp[i]){
a = tmp[i];
position = i;
}
}
return position;
}






                      
                    









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值