2009年北理计算机复试上机

题目来源:https://blog.csdn.net/u014552756/article/details/56664109
代码原创
1.倒叙输入字符串

#include
using namespace std;
typedef pair<string, int> PAIR;
bool cmp(PAIR a, PAIR b){
	return a.second > b.second;
}
int main(){
	map<string, int> mmp;
	string tmp;
	while(cin>>tmp){
		if(getchar()=='\n')
			break;
		if(tmp[tmp.size()-1] == ',' || tmp[tmp.size()-1] == '.'){
			if(mmp.find(tmp.substr(tmp.size()-1,1)) == mmp.end()){
				mmp[tmp.substr(tmp.size()-1,1)] = 1;
			}else{
				mmp[tmp.substr(tmp.size()-1,1)] ++;
			}
			tmp.erase(tmp.end()-1);
			if(!tmp.empty()){
				if(mmp.find(tmp) == mmp.end()){
					mmp[tmp] = 1;
				}else{
					mmp[tmp] ++;
				}
			}
		}else{
			if(mmp.find(tmp) == mmp.end()){
				mmp[tmp] = 1;
			}else{
				mmp[tmp] ++;
			}
		}
	}
	vector<PAIR> lst(mmp.begin(),mmp.end());
	sort(lst.begin(),lst.end(),cmp); 
	for(int i = 0; i < lst.size(); i++){
		cout << lst[i].first << " : "<<lst[i].second << endl;
	}
	/*
	for(map::iterator it = mmp.begin(); it != mmp.end(); it++){
		cout << it->first << " : " << it->second << endl;
	}*/
	return 0;
} 

2009年北理计算机复试上机_第1张图片
2.倒叙输入字符串写文件

#include
using namespace std;
ofstream fout("Name.txt");
void print(vector<string> lst){
	int j = 1;
	for(int i = lst.size()-1; i > 0; i--,j++){
		fout<<j<<"="<<lst[i]<<" ";
	}
	fout <<j<<"="<< lst[0]<<endl;
}
int main(){
	vector<string> lst;
	string tmp;
	int cnt = 1;
	while(cin >> tmp){
		cnt++;
		lst.push_back(tmp);
		print(lst);
	}
	
	return 0;
} 

2009年北理计算机复试上机_第2张图片
3.统计分组数字个数

#include
using namespace std;
vector<int> data, group;
map<int, map<int, int> > mp;
int main(){
	char buf[200];
	cout << "input data: ";
	//getline(cin, buf);
	gets(buf); 
	char *p = strtok(buf, ",");
	while(p){
		int tmp;
		sscanf(p,"%d",&tmp);
		p = strtok(NULL, ",");
		data.push_back(tmp);
	}
	cout << "input group: ";
	//getline(cin, buf);
	gets(buf);
	p = strtok(buf, ",");
	while(p){
		int tmp;
		sscanf(p,"%d",&tmp);
		p = strtok(NULL, ",");
		group.push_back(tmp);
	}
	for(int i = 0; i < data.size(); i++){
		for(int j = 0; j < group.size(); j++){
			mp[group[j]][data[i]] = 0;
		}
	}
	for(int i = 0; i < data.size(); i++){
		mp[group[i]][data[i]] ++;
	}
	for(map<int, map<int, int> >::iterator it = mp.begin(); it != mp.end(); it++){
		cout << it->first <<"={";
		//show(mp->second);
		for(map<int, int>::iterator itt = it->second.begin(); itt != (it->second.end()); itt++){
			cout<<itt->first<<"="<<itt->second;
			//if(itt != it->second.end() -1){
				cout << "," ; 
			//}
		}
		//cout<<(it->second.end()-1)->fisrt<<"="<<(it->second.end()-1)->second;
		cout << "}" << endl;
	}
	return 0;
} 

2009年北理计算机复试上机_第3张图片

你可能感兴趣的:(北理复试上机)