小明藏书真可谓汗牛充栋,现在有一道难题问:小明到底有多少本不一样的书,每样书的名字是什么,因为有的书名是样的,我们把他视为同样的书。
4
English
Math
Chinese
Chinese
样例输出
3
Chinese 2
English 1
Math 1
代码如下:
//map集合
#include
#include
#include
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;
}
样例输入
3 2
166 50 30
178 60 23
132 40 15
167 50 30
178 60 23
样例输出
no
yes
代码如下:
//set集合,遍历criminal,man.count()
#include
#include
#include
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;
}
给定n个整数,求里面出现次数最多的数,如果有多个重复出现的数,求值最大的那个。
输入样例1
5
1 1 2 3 4
//map集合自动排序,小->大,出现次数
#include
#include
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;
}
告诉你每一笔销售记录的水果名称、产地和销售的数量,请生产明细表。
输入样例:
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
#include
#include
#include
using namespace std;
int main() {
int n;
set<string> poo;//place of origin
set<string> fruit;
map<string,map<string, int>> table;
cin >> n;
getchar();
for (int i = 0, m; i < n; i++) {
string p, f;
cin >> f >> p >> m;
poo.insert(p);
fruit.insert(f);
if (!table.count(p)) table[p][f] = 0; //产地不存在,初始化
else if (!table[p].count(f)) table[p][f] = 0;//产地的水果不存在,初始化
table[p][f] += m;//累加
}
for (set<string>::iterator it1 = poo.begin(); it1 != poo.end(); ++it1) {
cout << *it1 << endl;
for (set<string>::iterator it2 = fruit.begin(); it2 != fruit.end(); ++it2) {
if (table[*it1].count(*it2)) cout << " |----" << *it2 << "\t(" << table[*it1][*it2] << ")" << endl;
}
}
return 0;
}
#include
#include
using namespace std;
bool fun(map<int, int> sum, int n) {
for (map<int, int>::iterator it = sum.begin(); it != sum.end(); it++) {
if (it->second > n / 2) return true;
}
return false;
}
int main() {
int n;
map<int, int> sum;
cin >> n;
for (int i = 0,m; i < n; i++) {
cin >> m;
sum[m]++;
}
if (fun(sum, n)) {
cout << "存在" << endl;
}
else {
cout << "不存在" << endl;
}
return 0;
}
//双指针
#include
#include
using namespace std;
string fun(string s) {
int n = s.length();
const char* ch = s.c_str();
int i = 0;
while (i <= n / 2) {
if (ch[i] != ch[n - 1 - i]) {
return "false";
}
i++;
}
return "true";
}
int main() {
string a = "abcba", b = "abccba", c = "abcabc";
cout << "a:" << fun(a) << endl;
cout << "b:" << fun(b) << endl;
cout << "c:" << fun(c) << endl;
return 0;
}
//双指针
#include
#include
#include
using namespace std;
string fun(vector<int> vec,int k) {
sort(vec.begin(), vec.end());
for (int i = 0, j = vec.size() - 1; i < j;) {
int sum = vec[i] + vec[j];
if (k == sum) {
cout << vec[i] << " " << vec[j] << endl;
return "true";
}
else if (sum > k) {
j--;
}
else {
i++;
}
}
return "false";
}
int main() {
int k = 9;
vector<int> a{ 6, 1, 4, 2 };
vector<int> b{ 1,8,3,4,5,10,9 };
cout <<"a:" << fun(a, k) << endl;
cout <<"b:" << fun(b, k) << endl;
return 0;
}
//排序,比较,
#include
#include
using namespace std;
void fun(int *a,int *b,int m,int n) {
int i = 0, j = 0;
sort(a, a+m);//先排序
sort(b, b+n);
while (i < m && j < n) {
if (a[i] == b[j]) {
cout << a[i] << " ";
i++; j++;
}
else if (a[i] > b[j]) {
j++;
}
else {
i++;
}
}
cout << endl;
}
int main() {
int a[] {23,32,25,12,34,26,67,54,53,31};
int b[] {32,53,65,61,13,23,57,76,35,24};
int m = std::end(a) - std::begin(a);
int n = std::end(b) - std::begin(b);
fun(a, b, m, n);
return 0;
}
#include
#include
using namespace std;
struct MyStruct
{
int p; //质因数
int s; //次数
};
vector<MyStruct> fun(int n) {
vector<MyStruct> vec;
int m = 0;
MyStruct e;
for (int i = 2; i <= n; i++) {
m = 0;
while(n % i == 0) {
m++;
n /= i;
}
if (m > 0) {
e.p = i;
e.s = m;
vec.push_back(e);
}
}
return vec;
}
int main() {
int n = 20;
vector<MyStruct> res;
res = fun(n);
cout << n << endl;
for (int i = 0; i < res.size(); i++) {
cout << res[i].p << ":" << res[i].s << endl;
}
return 0;
}
例如{4,1,2,3}相差最小的元素对的个数为3, 元素对为(1,2), (2,3), (3,4).
//排序,直接遍历计算差值,map存储出现次数
#include
#include
#include
#include
using namespace std;
int main() {
vector<int> vec{ 9,1,5,6,2,3,8 };
map<int,int> res;
sort(vec.begin(),vec.end());
for (vector<int>::iterator it = vec.begin()+1; it != vec.end(); it++) {
int c = *it - *(it - 1);
res[c]++;
}
cout <<"最小差值:"<< res.begin()->first << " 元素对个数:" << res.begin()->second << endl;
return 0;
}
#include
#include
#include
using namespace std;
void main(){
map<string, int> m1;
map<int, int> m2;
m1.insert(pair<string, int>("s1", 4));
m1.insert(pair<string, int>("s2", 3));
m1.insert(pair<string, int>("s3", 8));
m1.insert(pair<string, int>("s4", 4));
m1.insert(pair<string, int>("s5", 3));
for (map<string, int>::iterator it = m1.begin(); it != m1.end(); it++) {
m2[it->second]++;
}
for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++) {
if (it->second > 1) {
cout << it->first << ":" << it->second << endl;
}
}
}
#include
#include
#include
using namespace std;
map<int, int> fun(int* a, int* b, int m, int n) {
int i = 0, j = 0;
map<int, int> m1;
while (i < m) {
m1[a[i]]++;
i++;
}
while (j < n) {
m1[b[j]]++;
j++;
}
return m1;
}
int main() {
int a[]{ 23,32,25,12,34,26,67,54,53,31 };
int b[]{ 32,53,65,61,13,23,57,76,35,24 };
int m = std::end(a) - std::begin(a);
int n = std::end(b) - std::begin(b);
map<int, int> res = fun(a, b, m, n);
for (map<int, int>::iterator it = res.begin(); it != res.end(); it++) {
if (it->second > 1) {
cout << it->first << " " << endl;
}
}
return 0;
}
#include
#include
using namespace std;
int fun(stack<int> &st ,int k) {
stack<int> st1;
int a;
for (int i = 0; i < k; i++) {//出栈到st1
a = st.top();
st.pop();
st1.push(a);
}
a = st1.top();//获取第k个元素
st1.pop();
while (!st1.empty()) {
st.push(st1.top());
st1.pop();
}
return a;
}
//st出栈
void display(stack<int> st) {
while (!st.empty()) {
cout << st.top() << " ";
st.pop();
}
cout << endl;
}
int main() {
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
st.push(6);
st.push(7);
int k = 3;
cout << "第" << k << "个元素:" << fun(st, k) << endl;
display(st);
return 0;
}