沐里纷纷


私信TA

用户名:Epoch

访问量:62733

签 名:

我不会算法

等  级
排  名 37
经  验 12808
参赛次数 1
文章发表 172
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

不会算法

解题思路:

注意事项:

   对于char s[20],由于cin >> s是读取到空格处便会结束,也就是对于light red,用cin只能输入light。如果要整个输入一行,则使用cin.getline(s, 20),其中20为这一行的最大长度,也就是你的s的容量,如果容量为30,则cin.getline(s, 30)。
  另外,你在cin>>n以后cin.getline(s,30)应该会得到一个空字符串,这是因为整数n后面的换行符还未被输入。


参考代码:

#include <iostream>

#include <vector>

#include <algorithm>

#include <string>

#include <stdio.h>


using namespace std;


vector< pair<string, int> > colourList;


bool checkUnique(const string& s)

{

bool unique = true;

for (int i = 0; i < colourList.size(); i++)

{

if (colourList[i].first == s)

{

unique = false;

break;

}

}

return unique;

}


void addColorCount(const string& color)

{

for (vector< pair<string, int> >::iterator it = colourList.begin(); it < colourList.end(); it++)

{

if ((*it).first == color)

{

(*it).second += 1;

}

}

}


bool cmp(pair<string, int> a, pair<string, int> b)

{

bool ret = false;

if (a.second > b.second)

ret = true;

else if (a.second == b.second)

{

if (a.first < b.first)

{

ret = true;

}

}

return ret;

}


void output()

{

int maxCount = colourList[0].second;

for (vector< pair<string, int> >::iterator it = colourList.begin(); it < colourList.end(); it++)

{

if ((*it).second == maxCount)

{

cout << (*it).first << endl;

}

}

}


int main(int argc, char** argv)

{

freopen("input10.txt", "r", stdin);

int n = 0;

cin >> n;

getchar();

string temp;

for (int i = 0; i < n; i++)

{

getline(cin, temp);

if (checkUnique(temp))

{

pair<string, int> newColor(temp, 1);

colourList.push_back(newColor);

}

else

{

addColorCount(temp);

}

}


sort(colourList.begin(), colourList.end(), cmp);


output();


fclose(stdin);

return 0;

}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答

代码解释器

  评论区