Topic:
N Cows sitting in a row, each cow has an AI-only 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 from one cow exactly two apples to the other cow, ask how many times you need to move can be graded, 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 consists of 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.
Output Example:
4
7 15 9 5
Output Example:
3
1#include <iostream>2#include <vector>3#include <algorithm>4 using namespacestd;5 6 intSumConstvector<int> &v)7 {8vector<int>::size_type n=v.size ();9 intsum=0;Ten for(intI=0; i<n;i++) Onesum+=V[i]; A returnsum; - } - the intCountConstvector<int> &v,Const intN) - { - if(SUM (v)%N) - return-1; + Else - { + intAve=sum (v)/N; A intCnt=0; at for(intI=0; i<n;i++) - { - if(ABS (V[i]-ave)%2) - return-1; - Else - { inCnt=cnt+abs (V[i]-ave)/2; - } to } + returncnt/2; - } the } * $ intMain ()Panax Notoginseng { - intN//N is the number of cows thevector<int>v; +Cin>>N; A for(intI=0; i<n;i++) the { + inttemp; -Cin>>temp; $ V.push_back (temp); $ } -cout<<count (v,n); -}
Apple NetEase 2017 Test engineer Programming problem