杭电acm1326
http://acm.hdu.edu.cn/showproblem.php?pid=1326
解法同UVA591,先求平均数,然后再逐个计算,求和
View Code
1 #include<stdio.h> 2 #include<stdlib.h> 3 int main()//平均数以下的需要多少,就是最低多少 4 { 5 int n,i,a[100],sum,ave,j,ans; 6 for(i=1;;i++) 7 { 8 ans=0; 9 sum=0; 10 scanf("%d",&n); 11 if(n==0) 12 break; 13 for(j=0;j<n;j++) 14 { 15 scanf("%d",&a[j]); 16 sum+=a[j]; 17 } 18 ave=sum/n; 19 for(j=0;j<n;j++) 20 if(a[j]<ave) 21 ans+=ave-a[j];//把平均数和低于平均数的数进行比较,把相差的数量加到答案里 22 printf("Set #%d\nThe minimum number of moves is %d.\n\n",i,ans); 23 } 24 system("pause"); 25 return 0; 26 }