Select the President and choose the President for Crosstalk
Description
James wants to be the president of the ugly nation. The Ugly nation election determines the final result based on the voting results of the States. If more than half of the States support the election, he can be elected, the voting results of each State are produced by the state's voters. If more than half of the state's voters support James, he will win the State's support. The number of voters in each State is provided. How many voters does James need to win to be elected?
Input
The input contains multiple groups of test data.
The first row of each group of data is an integer N (1 <= N <= 101), indicating the number of states in the ugly country. When N = 0, the input ends.
The next line contains N positive integers, indicating the number of voters in each state. The number of voters in each State cannot exceed 100.
Output
Output a row of data in each group, indicating at least the number of voters James needs to win.
Sample Input
3
5 7 5
0
Sample output
6
Solution:
N States are given, with m members in each state. The n states are sorted by number of people from small to large. Each time, the m half is added to 1, and the n half is added to 1.
The Code is as follows:
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 int main() 6 { 7 int a,b[110],i,j,k,q; 8 while(scanf("%d",&a),a!=0) 9 {10 memset(b,0,sizeof(b));11 for(i=0;i<a;i++)12 scanf("%d",&b[i]);13 sort(b,b+a);14 q=0;15 for(i=0;i<a/2+1;i++)16 q+=b[i]/2+1;17 printf("%d\n",q);18 }19 return 0;20 }