北京理工大学复试上机--2011
Posted ache
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了北京理工大学复试上机--2011相关的知识,希望对你有一定的参考价值。
1、输入一组单词(区分大小写),统计首字母相同的单词的个数,相
同的单词不累加,输出格式:“字母,个数”
input: I am a boy,you are a boy.
output:
I,1
a,3
b,1
y,1
#include <iostream> #include <map> #include <set> using namespace std; int main() { string s; while(getline(cin, s)) { map<char, int> mmp; set<string> ss; string str; for(int i = 0; i < s.length(); i++) { if(isalpha(s[i])) str += s[i]; if(s[i] == ‘ ‘ || s[i] == ‘,‘ || s[i] == ‘.‘ || i == s.length() - 1) { if(str != "") { ss.insert(str); str = ""; } } } for(auto i = ss.begin(); i != ss.end(); i++) { string word = *i; mmp[word[0]]++; } for(auto i = mmp.begin(); i != mmp.end(); i++) { cout << i ->first << "," << i -> second << endl; } } return 0; }
2、输入一组单词,(区分大小写),输出其字典排序。
input: Information Info Inform info Suite suite suit
output: Info Inform Information Suite info suit suite
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { string s; while(getline(cin, s)) { vector<string> ss; string str; for(int i = 0; i < s.length(); i++) { if(isalpha(s[i])) str += s[i]; if(s[i] == ‘ ‘ || s[i] == ‘,‘ || s[i] == ‘.‘ || i == s.length() - 1) { if(str != "") { ss.push_back(str); str = ""; } } } sort(ss.begin(), ss.end()); for(int i = 0; i < ss.size(); i++) { cout << ss[i] << " "; } } return 0; }
3、给一个字符串(aaaa(bbbb(cccc,dddd),eeee(ffff)))该字符串
表明的是各个人的层次关系。
比如aaaa是bbbb和eeee的领导,bbbb是cccc和dddd的领导。
现输入一个名称,比如ffff,要求输出其领导关系
input: (aaaa(bbbb(cccc,dddd),eeee(ffff)))
output: aaaa>eeee>ffff
#include <iostream> #include <vector> #include <stack> #include <map> using namespace std; struct person { string name = ""; string leadername = ""; }; int main() { string s, s2; while (cin >> s >> s2) { vector<person> vp; vector<vector<string>> v; stack<string> sp; map<string, string> mmp; string str = ""; for (int i = 0; i < s.length(); i++) { if (isalpha(s[i])) str += s[i]; else { person p; if (str != ""){ p.name = str; if(!sp.empty()) { p.leadername = sp.top(); mmp[p.name] = sp.top(); } str = ""; } if(s[i] == ‘(‘) { if(p.name != "") sp.push(p.name); } if (s[i] == ‘)‘ && !sp.empty()) sp.pop(); if(p.name != "") vp.push_back(p); } } stack<string> ss; while(mmp[s2] != "") { ss.push(s2); s2 = mmp[s2]; } ss.push(s2); int num = ss.size(), cnt = 0; while(!ss.empty()) { cout << ss.top(); ss.pop(); cnt++; if(cnt < num) cout << ">"; } } return 0; }
PS:好几天都没状态了,得赶快进入状态啊!!!
以上是关于北京理工大学复试上机--2011的主要内容,如果未能解决你的问题,请参考以下文章