代碼
@2018-01-04
以下是在線執行系統的結果,不代表複雜度
#include <iostream>
#include <map>
#include <string>
using namespace std;
map<string, int> mp;
int main()
{
int n;
while (cin>>n, n!=0)
{
string s,p;
int mx=0;
for (int i=0;i<n;i++)
{
cin>>s;
if (mp.find(s)==mp.end())
{
mp[s]=1;
if (1>mx)
{
mx=1;
p=s;
}
}
else
{
mp[s]++;
if (mp[s]>mx)
{
mx=mp[s];
p=s;
}
}
}
cout << p << endl;
}
}
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n,i,j,t;
char str[20],color[10000][20];
while(cin>>n&&n!=0)
{
int num[10000]={0},max=0;
for( i=1;i<=n;i++)
{
cin>>str;
for(j=1;j<=i;j++)
{
if(strcmp(str,color[j])==0)
{
num[j]++;
if(num[j]>max)
{
max=num[j];
t=j;
}
break;
}
}
if(j>i)
memcpy(color[j],str,sizeof(str));
}
cout<<color[t]<<endl;
}
return 0;
}
過程
以上全是抄的,自己嘗試寫過代碼。思路上出現錯誤,然後卡殼了。
模擬的是人觀測的方式,打眼一看,哪個顏色多少個球,然後輸出,完工。
思路1,每次接收輸入的時候,記錄每種顏色球的個數。輸入結束後,找到個數最多的,然後輸出。
思路1的問題,如何在每次接收的時候統計個數,每接收一個就要遍歷一次顏色是否重複麼?相關的中間數據以什麼樣的形式存儲呢。
思路2,先接收全部輸入,然後再根據輸入,進行個數統計。
思路2的問題,怎麼遍歷,還是要想辦法存中間數據啊。感覺數組比vector要清晰啊。(實際上是我不熟悉vector的[])
#include<stdio.h>
#include<string>
#include<iostream>
#include <vector>
using namespace std;
struct ColorNum
{
string Color;
int n =0;
};
int main()
{
int Num = -1;
string strColor;
ColorNum strColorMost;
vector<string> tmp;
while (scanf("%d",&Num))
{
for (size_t i = 0; i < Num; i++)
{
cin >> strColor;
tmp.push_back(strColor);
}
}
return 0;
}