HDOJ 1004 Let the Balloon Rise(C语言)

it2023-07-21  67

之前被卡住了一段时间,在看了其他人的代码后发现这其实是很简单的一道题 思路是用二维数组存储字符串,随后用strcmp函数确定字符串是否相等,若相等则指针++,同时比较是否大于前一个字符串的出现次数,如果大于,覆写次数和index,最后输出下标为index的字符串,接着清空二维数组。

#include <stdio.h> #include <string.h> int main(void){ char map[10001][20]; int max = 0; int index = 0; int temp = 0; int n; while(~scanf("%d", &n) && n) { if(n == 0) break; for(int i = 0; i < n; i++) scanf("%s", &map[i]); for(int i = 0; i < n; i++) { for(int j = 0; j < n; j++) { if(strcmp(map[i], map[j]) == 0) temp++; //标记当前字符串的出现次数 if(max < temp) { max = temp; index = i; //作为指针标记出现次数最多的字符串 } } temp = 0; } printf("%s\n",map[index]); max = 0; memset(map,0,sizeof(map)); //归零,为下一组做准备 } return 0; }
最新回复(0)