/*
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 133075 Accepted Submission(s): 52551
Problem Description
Contest 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*/
//统计出现次数最多的字符串
//strcmp(str1,str2) str1>str2 返回正数,str1==str2 返回0,str1<str2 返回负数
//strcpy(str1,str2) 把str2复制到str1 返回指向str1的指针
#include<iostream>
#include<cstring>
#define MAX 16
using namespace std;
int main(){
char color[1001][MAX];//储存所有的输入的数组
int bowl[1001];//如果出现一个颜色就把他出现的次数计入bowl
int count=0;//记录出现了多少种颜色
int N;
bool flag=false;
char temp[16];
while(cin>>N&&N){
count=0;
memset(bowl,0,sizeof(bowl));//初始化bowl
for(int i=0;i<N;i++){
flag=false;//这个颜色先设定为没出现过
cin>>temp;
//把他与已经有的颜色进行比较,如果存在就加,如果不存在就存入并为1
//if(strcmp(color[i],temp)==1) string 是一个类,不是char *类型,所以报错
//在所有颜色里找一遍看有没有一样的
for(int j=0;j<i;j++){
if(strcmp(color[j],temp)==0){
bowl[j]++;
flag=true;
}
}
//如果temp里的颜色没有出现过
if(!flag){
strcpy(color[i],temp);
bowl[i]++; count++;
}
}
// cout<<"bowl[]:";
// for(int i=0;i<count;i++) cout<<bowl[i]<<" "; cout<<endl;
int max=0;//统计出现的最多次数
int j;//记录下标
for(int i=0;i<count;i++){
if(max<bowl[i]) {
max=bowl[i];
j=i;// cout<<"j:"<<j;
}
}
// cout<<j<<endl;
cout<<color[j]<<endl;
}
return 0;
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 133075 Accepted Submission(s): 52551
Problem Description
Contest 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*/
//统计出现次数最多的字符串
//strcmp(str1,str2) str1>str2 返回正数,str1==str2 返回0,str1<str2 返回负数
//strcpy(str1,str2) 把str2复制到str1 返回指向str1的指针
#include<iostream>
#include<cstring>
#define MAX 16
using namespace std;
int main(){
char color[1001][MAX];//储存所有的输入的数组
int bowl[1001];//如果出现一个颜色就把他出现的次数计入bowl
int count=0;//记录出现了多少种颜色
int N;
bool flag=false;
char temp[16];
while(cin>>N&&N){
count=0;
memset(bowl,0,sizeof(bowl));//初始化bowl
for(int i=0;i<N;i++){
flag=false;//这个颜色先设定为没出现过
cin>>temp;
//把他与已经有的颜色进行比较,如果存在就加,如果不存在就存入并为1
//if(strcmp(color[i],temp)==1) string 是一个类,不是char *类型,所以报错
//在所有颜色里找一遍看有没有一样的
for(int j=0;j<i;j++){
if(strcmp(color[j],temp)==0){
bowl[j]++;
flag=true;
}
}
//如果temp里的颜色没有出现过
if(!flag){
strcpy(color[i],temp);
bowl[i]++; count++;
}
}
// cout<<"bowl[]:";
// for(int i=0;i<count;i++) cout<<bowl[i]<<" "; cout<<endl;
int max=0;//统计出现的最多次数
int j;//记录下标
for(int i=0;i<count;i++){
if(max<bowl[i]) {
max=bowl[i];
j=i;// cout<<"j:"<<j;
}
}
// cout<<j<<endl;
cout<<color[j]<<endl;
}
return 0;
}
一开始的时候想用string数组 一直以为string一维数组就是和char二维数组一样的东西,后来发现,原来string是一个类的名字,所以用不了strcmp和strcpy函数,不然会报错。
一开始想用char *temp=NULL;后来还是觉得指针有点危险……
还有后面选最大数的时候忘记更新max了……调试半天,我真傻……真的……