[Zhengqing] [1898] 985's Number Challenge

Problem Description

985的数字难题
Time limit: 1 SecMemory limit: 128 MB

Description

985 has n numbers. It is known that in each operation, he can add one to one of two different numbers and subtract one from the other, with no limit on the number of operations.
What is the maximum number of equal numbers he can obtain?

Input

The first line contains an integer t, representing t groups of test data.
Each test case occupies two lines. The first line contains an integer n representing the number of elements, and the next line contains n integers a[].
Note: 1 <= t <= 30, 1 <= n <= 1e4, 1 <= a[] <= 1e3.

Output

Output an integer representing the maximum number of equal numbers that can be obtained.

Sample Input

1
2
3
4
5
2
3
1 1 1
2
2 3

Sample Output

1
2
3
1

Problem Analysis

Because adding one and subtracting one leave the total unchanged,
it follows that the final number is related to sum/n, i.e., the average.
When sum%n==0, it is always possible to eventually turn them into n equal numbers.
Otherwise, there will inevitably be an extra sum-sum/n*n at the end,
so in the end they can only be turned into n-1 equal numbers.

cpp
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h> 
#include<algorithm> 
using namespace std; 
int main() { 
    int T; 
    scanf("%d",&T); 
    while(T--) { 
        int n; 
        scanf("%d",&n); 
        int sum=0; 
        for(int i=0; i<n; i++) { 
            int t; 
            scanf("%d",&t); 
            sum+=t; 
        } 
        if(sum%n==0) 
            printf("%d\n",n); 
        else
            printf("%d\n",n-1); 
    } 
    return 0; 
}

Problem URL: ZZULI 1898 985 Number Problem

Loading...