1. 藏书问题:
小明藏书真可谓汗牛充栋,现在有一道难题问:小明到底有多少本不一样的书,每样书的名字是什么,因为有的书名是样的,我们把他视为同样的书。

4
English
Math
Chinese
Chinese
样例输出
3
Chinese 2
English 1
Math 1
代码如下:
//map集合
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main() {
int n;
string name;
/*char name[101];*/
map<string,int> books;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> name;
books[name]++;
}
cout << books.size() << endl;
map<string, int>::iterator it;
for (it = books.begin(); it != books.end(); it++) {
cout << it->first << ":\t" << it->second << endl;
}
return 0;
}

2. 破案问题:

样例输入
3 2
166 50 30
178 60 23
132 40 15
167 50 30
178 60 23
样例输出
no
yes
代码如下:
//set集合,遍历criminal,man.count()
#include<iostream>
#include<set>
#include <string>
using namespace std;
int main() {
int m, n;
string people;
cin >> m >> n;
set<string> man, criminal;
getchar();//吸收缓冲区
for (int i = 0; i < m; i++) {
getline(cin, people);
man.insert(people);
}
for (int i = 0; i < n; i++) {
getline(cin, people);
criminal.insert(people);
}
for (auto it = criminal.begin(); it != criminal.end(); ++it) {
if (man.count(*it) == 1) {
cout << "yes" << endl;
}
else
{
cout << "no" << endl;
}
}
return 0;
}

3. 重复最大值
给定n个整数,求里面出现次数最多的数,如果有多个重复出现的数,求值最大的那个。
输入样例1
5
1 1 2 3 4
输出样例1
1 2

//map集合自动排序,小->大,出现次数
#include<iostream>
#include<map>
using namespace std;
int main() {
int n;
map <int, int> num;//默认情况下map根据键的大小采用升序排序
cin >> n;
for (int i = 0,x; i < n; ++i) {
cin >> x;
num[x]++;
}
int max = 0, sum = -1;
for (map<int,int>::iterator it = num.begin();it != num.end();it++) {
if (it->second >=sum) {
//因为map已经排好序,所以只需要注意数出现的次数
sum = it->second;
max = it->first;
}
}
cout << max << "\t" << sum << endl;
return 0;
}

4. 水果明细表
告诉你每一笔销售记录的水果名称、产地和销售的数量,请生产明细表。
输入样例:
5
apple shandong 3
pineapple guangdong 1
sugarcane guangdong 1
pineapple guangdong 3
pineapple guangdong 1
输出样例
guangdong
|----pineapple(5)
|----sugarcane(1)
shandong
|----apple(3)
//map<产地,map<水果,数量>>
#include<iostream>
#include<map>
#include<set>
#include <string>
using namespace std;
int main() {
int n;
set<string> poo;//place of origin
set<string> fruit;
map<string,map<string

最低0.47元/天 解锁文章
2232

被折叠的 条评论
为什么被折叠?



