杭电1004为什么不过?
Problem DescriptionContest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.
This year, they decide to leave this lovely job to you.
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.
A test case with N = 0 terminates the input and this test case is not to be processed.
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink
程序代码:
#include <stdio.h> #include <string.h> void find(char* str); int max(); struct Color { int num; char color[16]; } book[20]; int main() { int i = 0; int j = 0; char balloon[20][1000][16] = {0}; //输入数据 while (scanf("%d", &i) && i != 0) { for (int p = 0; p < i; p++) { scanf("%s", balloon[j][p]); } j++; } i = 0; while (balloon[i][0][0]) //输出 { for (int p = 0; p < 20; p++) { book[p].num = 0; book[p].color[0] = 0; } j = 0; while (balloon[i][j][0]) { find(balloon[i][j]); j++; } max(); i++; } return 0; } void find(char* str) // str[]) //统计气球每种颜色数量 { int k = 0; while (book[k].num ) { if (strcmp(str, book[k].color)) { k++; } else { book[k].num++; return; } } strcpy(book[k].color, str); book[k].num = 1; } int max() //输出数量最多的气球颜色 { int m = 0; int inMax = 0; while (book[m].num != 0) { if (book[inMax].num < book[m].num) { inMax = m; } m++; } printf("%s\n", book[inMax].color); return 0; }
[此贴子已经被作者于2023-8-11 09:32编辑过]