PAT_1020(Basic Level)Practise C语言实现代码

/* PAT_1020(Basic Level)Practise   C语言实现代码  */
#include
void sort(float a[],int b[],int n);//函数声明 
int main(){
int N,D,K,T;
int i=0,j=0;
float sum1=0,sum2=0,sum3,sum4;
    float SumMoney[1020],MoneyPerWeight[1020];
int  Weight[1020];
scanf("%d %d",&N,&D);
T=N;
K=N;
while(T--){//输入库存量 
scanf("%d",&Weight[i++]);
}
while(K--){//输入总售价 
scanf("%f",&SumMoney[j++]);
}
for(i=0;i MoneyPerWeight[i]=SumMoney[i]/Weight[i];
}
sort(MoneyPerWeight,Weight,N);
for(i=0;i sum1+=MoneyPerWeight[i]*Weight[i];
sum2+=Weight[i];
if(sum2>D){
break;
}
}
if(sum2<=D){
printf("%.2f",sum1);
}
if(sum2>D){
for(j=0;j<=Weight[i];j++){
sum3=sum2-j;
sum4=sum1-j*MoneyPerWeight[i];
if(sum3==D){
break;
}
}
printf("%.2f",sum4);
}
return 0;
}
void sort(float a[],int b[],int n){//排序 
int i,j;
float temp1,temp2;
for(i=0;i for(j=0;j if(a[j] temp1=a[j+1];
temp2=b[j+1];
a[j+1]=a[j];
b[j+1]=b[j];
a[j]=temp1;
b[j]=temp2;
}
}
}
}

你可能感兴趣的:(C语言)