The following program is my ACcepted code for UVA-591.
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UAV 591 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
//This program is for UVA 591 Box of Bricks
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=532
//note: Output a blank line after each set. 每個測試資料輸出後面要多一行空白!
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int n, h[51];
int t = 1;//test case
int sum, average, answer;
while(scanf("%d", &n)!=EOF)
{
if(n == 0)
break;
sum = 0;
average = 0;
answer = 0;
for(int i=0; i<n; i++)
{
scanf("%d", &h[i]);
sum += h[i];
}
average = sum / n;
for(int i=0;i<n;i++)
answer = answer + abs(average - h[i]);
printf("Set #%d\nThe minimum number of moves is %d.\n\n", t, answer / 2);
t++;
}
return 0;
}
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UAV 591 的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
//This program is for UVA 591 Box of Bricks
//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=532
//note: Output a blank line after each set. 每個測試資料輸出後面要多一行空白!
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
int main()
{
int n, h[51];
int t = 1;//test case
int sum, average, answer;
while(scanf("%d", &n)!=EOF)
{
if(n == 0)
break;
sum = 0;
average = 0;
answer = 0;
for(int i=0; i<n; i++)
{
scanf("%d", &h[i]);
sum += h[i];
}
average = sum / n;
for(int i=0;i<n;i++)
answer = answer + abs(average - h[i]);
printf("Set #%d\nThe minimum number of moves is %d.\n\n", t, answer / 2);
t++;
}
return 0;
}
Please feel free to use it after adding this blog as an reference. (http://autekroy.blogspot.tw) If there is any mistake or comment, please let me know. :D
歡迎使用與分享任何內容,但請記得標示此部落格為出處。(http://autekroy.blogspot.tw/) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!