题目1:字符分类
// 20200701-题目1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { string str; string strg = "bdfhkl"; string strz = "aceimnorstuvwxz"; string strd = "gjpqy"; while (cin >> str) { vector<char> vecg, vecz, vecd; for (int i = 0; i < str.length(); i++) { if (strg.find(str[i]) != strg.npos) { vecg.push_back(str[i]); } else if (strz.find(str[i]) != strz.npos) { vecz.push_back(str[i]); } else { vecd.push_back(str[i]); } } sort(vecg.begin(),vecg.end()); sort(vecz.begin(), vecz.end()); sort(vecd.begin(), vecd.end()); if (vecg.size() == 0) { cout << "null"<<endl; } else { for (auto x : vecg) { cout << x; } cout << endl; } if (vecz.size() == 0) { cout << "null" << endl; } else { for (auto x : vecz) { cout << x; } cout << endl; } if (vecd.size() == 0) { cout << "null" << endl; } else { for (auto x : vecd) { cout << x; } cout << endl; } } return 0; }
题目2:
输入歌曲名字,风格;
听完(P)歌曲A,则歌曲A喜好度+3;如果听完歌曲B,且B与歌曲A都属于同一风格(比如POP)风格,则对B以外的所有POP歌曲喜好度+1;
没听完(B)歌曲C,则对歌曲C的喜好度-2;如果未听完歌曲D,且D与歌曲C都属于同一风格(比如Blue),则对D以外的所有Blue歌曲喜好度-1;
如果是位置风格的歌曲(比如E),则只对当前歌曲喜好度+3或-2;
输入:I MusicA Pop
I MusicB Pop
I MusicC Blue
I MusicD Blue
I MusicE UnkownStyle
I MusicF Pop
P MusicA
P MusicB
B MusicC
P MusicF
B MusicD
B MusicE
输出:
MusicA Pop
MusicB Pop
MusicF Pop
MusicD Blue
MusicE UnkownStyle
MusicC Blue
//代码如下,其实构建结构体{name,style,xihaodu}结合vector更简洁明了。
// 20200701-题目2.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 // #include <iostream> #include <string> #include <sstream> #include <map> #include <vector> #include <algorithm> using namespace std; bool paixu(pair<string,int> p1, pair<string, int> p2) { if (p1.second == p2.second) { return p1.first < p2.first; } else { return p1.second > p2.second; } } int main() { string str; map<string, string,less<string>> mapm_s; map<string, int,less<string>> mapm_v; vector<pair<string, int>> vecout; string last_p=""; string last_pmusic=""; string last_b = ""; string last_bmusic = ""; while (getline(cin,str)) { if (str == "") break; if (str[0] == 'I') { vector<string> vec; stringstream ss(str); string temp; while (getline(ss, temp, ' ')) { vec.push_back(temp); } if (vec.size() == 3) { mapm_s.insert(make_pair(vec[1], vec[2])); mapm_v.insert(make_pair(vec[1], 0)); } else { mapm_s.insert(make_pair(vec[1], "UnkownStyle")); mapm_v.insert(make_pair(vec[1], 0)); } } else if (str[0] == 'P') { vector<string> vec; stringstream ss(str); string temp; while (getline(ss, temp, ' ')) { vec.push_back(temp); } mapm_v[vec[1]] += 3; if (mapm_s[vec[1]] != "UnkownStyle"&&mapm_s[vec[1]]== last_pmusic) { for (auto &x:mapm_s) { if (x.second == mapm_s[vec[1]]) { if (x.first != vec[1]) { mapm_v[x.first] += 1; } } } /*for (auto x=mapm_s.begin();x!=mapm_s.end();x++) { if (x->second == mapm_s[vec[1]]) { if (x->first != vec[1]) { mapm_v[x->first] += 1; } } } */ } last_pmusic = mapm_s[vec[1]]; } else { vector<string> vec; stringstream ss(str); string temp; while (getline(ss, temp, ' ')) { vec.push_back(temp); } mapm_v[vec[1]] -= 2; if (mapm_s[vec[1]] != "UnkownStyle"&&mapm_s[vec[1]] == last_bmusic) { for (auto &x : mapm_s) { if (x.second == mapm_s[vec[1]]) { if (x.first != vec[1]) { mapm_v[x.first] -= 1; } } } } last_bmusic = mapm_s[vec[1]]; } } vecout.assign(mapm_v.begin(), mapm_v.end()); //sort(mapm_v.begin(), mapm_v.end(), paixu); sort(vecout.begin(), vecout.end(), paixu); for (auto x : vecout) { cout << x.first << ' ' << mapm_s[x.first] << endl; } return 0; }
全部评论
(0) 回帖