Elections
Time limit: 2000 ms
Memory limit: 65536 KB
Description
There are two candidates campaigning to be president of a country. From newspaper polls, it is clear what percentages of people plan to vote for each candidate in each state. Candidate 1 wants to campaign in one last state, and needs to figure out which state that should be.
Given some states and votes in each state, find one city with lowest percentage of people planning on voting for candidate 1.
Input Format
First line consists of an integer N (1 <= N <=50), the number of states. Each of the next N lines is a string (1 to 50 characters long, inclusive) consisting of the characters ‘1’ and ‘2’, where ‘1’ represents some number of votes for candidate 1, and ‘2’ represents votes for candidate 2 (in each state, every character represents the same number of votes).
Output Format
An integer, the 0-based index of the state where the lowest percentage of people are planning on voting for candidate 1 (lowest percentage of ‘1’ characters in that element of the input). If there are multiple such states, return one with the lowest index
Sample Input 1
3 1222 1122 1222
Sample Output 1
0
Note
In the first state only 25% of people prefer candidate 1, while in the second and third, 50% and 25% prefer him, respectively.
Sample Input 2
3 1222111122 2222222111 11111222221222222222
Sample Output 2
1
Note
The percentages of people, prefering candidate 1 to candidate 2 are (in order): 50%, 30%, 30%
Sample Input 3
8 111 112 121 122 211 212 221 222
Sample Output 3
7
Sample Input 4
6 1122 1221 1212 2112 2121 2211
Sample Output 4
0
Sample Input 5
6 11112222111121 1211221212121 112111222 11122222222111 112121222 1212122211112
Sample Output 5
3
Solution
#include <stdio.h> #include <stdlib.h> int main() { int N,i,j; double tempVal; int index; scanf("%d",&N); if(N>=1 && N <=50){ char temp[N][250]; double length,counter1; double result[N]; for(i=0;i<N;i++){ fflush(stdin); scanf("%s",&temp[i]); } for(i = 0;i<N;i++){ length=0; counter1 = 0; result[i] = 0; length = strlen(temp[i]); for(j = 0;j<length;j++){ if(temp[i][j] == '1'){ counter1++; } } result[i] = counter1/length; if(i==0){ tempVal = result[0]; index = 0; } if(tempVal > result[i]){ tempVal = result[i]; index = i; } } printf("%d\n",index); } return 0; }