Description
This year, they decide to leave this lovely job to you.
Input
A test case with N = 0 terminates the input and this test case is not to be processed.
Output
Sample Input
Sample Output
分析:
(1)輸入字符串,每種分別計數(strcmp),輸出最大對應的字符串。
<span style="font-size:18px;">#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n,k[1000];
char a[1000][100];
while(cin>>n,n)
{
int i,j,t;
for(i=0; i<n; i++)cin>>a[i];
for(i=0; i<n; i++)
{
t=0;
for(j=0; j<n; j++)
if(strcmp(a[i],a[j])==0)
k[i]=t++;
}
t=0;
for(i=0; i<n; i++)
if(k[i]>k[t])
t=i;
cout<<a[t]<<endl;
}
return 0;
}</span>
(2)使用STL——<map>map中有一一映射的關係,建立color和count之間的關係即map<string,int>ball;
it->first是map中第一個元素,it->second是map中第二個元素
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std;
#define N 102
map<string,int>ball;///顏色和個數的映射
map<string,int>::iterator it;
int main()
{
int n;
string color;
while(scanf("%d",&n),n)
{
ball.clear();///先清0
for(int i=0;i<n;i++)
{
cin>>color;
ball[color]++;
}
int max=-1;
for(it=ball.begin();it!=ball.end();it++)///map
{
if(it->second>max){
color=it->first;
max=it->second;
}
}
cout<<color<<endl;
}
return 0;
}