hdu 1326 java (理解起來很簡單)
Box of Bricks
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5841 Accepted Submission(s): 2536
Problem Description
Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. ``Look, I've built a wall!'', he tells his older sister Alice. ``Nah, you should make all stacks the same height. Then you would have
a real wall.'', she retorts. After a little con- sideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number
of bricks moved. Can you help?
Input
The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1 <= n <= 50 and 1 <= hi <= 100.
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.
The input is terminated by a set starting with n = 0. This set should not be processed.
Output
For each set, first print the number of the set, as shown in the sample output. Then print the line ``The minimum number of moves is k.'', where k is the minimum number of bricks that have to be moved in order to make all the stacks the same height.
Output a blank line after each set.
Output a blank line after each set.
Sample Input
6
5 2 4 1 7 5
0
Sample Output
Set #1
The minimum number of moves is 5.
Source
題目解釋:
題目意思很簡單,就是 給你n個數,然後通過程式設計變成n個都相等的數,問求怎麼移動的最小數目,然後按題目的要求輸出。每一個輸出之後都要換一行(這裡地方我PE了很多次),大家注意點就好。
package cn.hncu.acm;
import java.util.Scanner;
public class p1326 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);//從鍵盤收數
int d=0;
while(sc.hasNext()){
int n=sc.nextInt();//輸入n
if(n==0){
break;
}
int a[]=new int[n];//用來放n個數
int sun=0,m=0;
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
sun=sun+a[i];//求n個數的和,求平均數
}
sun=sun/n;//求平均數
//當求出平均數後,比平均數多出來的就必須移動其他的地方,也就是不會重複移動,也就是最少的移動路徑
for(int i=0;i<n;i++){
if(a[i]>sun){
m=m+a[i]-sun;//求多出來的數字
}
}
d=d+1;//為目標輸出 SEt # d
System.out.println("Set #"+d);
System.out.println("The minimum number of moves is "+m+".");
System.out.println();//PE 是因為 最後沒有換行
}
}
}
相關文章
- 淘寶放大鏡的簡單實現,原來道理很簡單
- Java的簡單理解(2)Java
- hdu 1237 Java 簡單計算器Java
- HTML很簡單?不!HTML
- 這麼理解執行緒生命週期,是不是很簡單?執行緒
- Promise 其實很簡單Promise
- 越來越“簡單”的JavaJava
- 別再說SUM函式很簡單,進來看看吧!函式
- 資料結構(c語言版)-為什麼想起來很簡單的程式碼,寫起來那麼費勁呢?資料結構C語言
- java介面和抽象類的簡單理解Java抽象
- Java的簡單理解(22)---處理流Java
- 程式設計師自黑的梗!產品經理:功能實現起來很簡單!—程式設計師:*&%程式設計師
- 這樣看java記憶體模型其實很簡單Java記憶體模型
- 看起來很唬人,然而卻簡單實用的CAP理論
- 入門Flink,很簡單
- 其實泛型很簡單泛型
- 誰說 JavaScript 很簡單了?JavaScript
- 複習下 git,很簡單Git
- 把redux封裝起來,更加簡單方便的使用Redux封裝
- hdu 3665Seaside(簡單floyd)IDE
- 關於Java註解(annotation)的簡單理解Java
- 關於MongoDB的簡單理解(二)--Java篇MongoDBJava
- 簡單理解Java中的4種迴圈Java
- 那些解釋起來很蛋疼的IT工種
- 用java呼叫.net的wcf其實還是很簡單的Java
- 簡單理解promisePromise
- Restful簡單理解REST
- EJB簡單理解
- BFC簡單理解
- Blazor一個簡單的示例讓我們來起飛Blazor
- 很簡單的Flutter填小坑Flutter
- The Center of Gravity(一道很很簡單的幾何題)
- hdu5365 簡單幾何問題
- 來來,一起設計一個簡單的活動釋出系統
- 原來 Java 遠端除錯如此簡單Java除錯
- 簡單理解回撥
- 簡單理解OAuth 2.0OAuth
- Redux其實很簡單(原理篇)Redux