Title Description:
N Cows sitting in a row, each cow has an AI apple, now you want to transfer apples between them, so that the last of all cows have the same number of apples, every time, you can only take exactly two apples from one cow to another cow, asked how many times to move the minimum number of moves can be divided between apples, if the scheme does not exist output- 1.
Input Description:
Each input contains a test case. The first line of each test case contains an integer n (1 <= n <= 100), and the next line contains n integer ai (1 <= ai <= 100).
Output Description:
The output line indicates how many times you need to move the apples evenly, and output-1 if the scheme does not exist.
Example 1
Input
4
7 15 9 5
Output
3
Ideas:
Mathematical problems, in order to judge can. First you enter n cows, and then you enter the number of apples for each cow in a row.
Calculate the total number of apples owned by all cows, average. Total number of cows cannot be evenly divisible 1
Determine if each cow has more than the average of apples, greater than the value of the judgment can be divisible by 2, can not be returned-1
Can add up the number of excess, divided by 2 is the number of times required to transfer
1 ImportJava.util.*;2 Public classFenpingguo {3 4 Public Static voidMain (string[] args) {5 //TODO auto-generated Method Stub6Scanner sc =NewScanner (system.in);7 intn =sc.nextint ();8 intnum = 0;9 int[] ai =New int[100];Ten for(inti = 0; I < n; i++){ OneAi[i] =sc.nextint (); Anum+=Ai[i]; - } - intx = num/N; the intKey = 0; - intover = 0; - for(inti = 0; I < n; i++){ - if(ai[i]>x) { +over = ai[i]-x; - if(over%2!=0){ +System.out.println (-1); A return; at } -key+=ai[i]-x; - } - } - if(num%n!=0) System.out.println (-1); - //else if (key%2!=0) System.out.println ( -1); in Else { -System.out.println (KEY/2); to } + } - the}
New Ket net Online programming: Apple