HDOJ 1004:统计气球数
3 月,跳不动了?>>> 题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1004 一、题目要求 输入:给定一个数字和一组颜色数据,数字是这组颜色数据的数量。当输入的数字为0时结束。 输出:对于每一组给定的颜色数据, 统计其中出现频率最高的颜色。 二、实现思路 用链表统计颜色数,每发现一个新颜色都插入一个新结点。接收完所有数据后,遍历链表可以找出统计数量最多的颜色,该颜色即为所求。 三、程序代码 #include<iostream> using namespace std; //结构:结点 struct Node { string color; //颜色名 int count; //接收到该颜色的数量 Node *next; //下个结点指针 }; int main() { int count; while(cin >> count) { if(count == 0) { break; } string color; //接收输入 Node *head = NULL, *pointer; while(count--) { cin >> color; //首结点为空则插入首结点 if(head == NULL) { head = new Node(); head -> color = color; head -> count = 1;