北理复试上机题2009年

版权声明:作者:weizu_cool https://blog.csdn.net/qq_26460841/article/details/88376612

1、请输入字符串,最多输入4个字符串,要求后输入的字符串排在前面,例如:

输入:EricZ

输出:1=EricZ

输入:David

输出:1=David 2=EricZ

#include<iostream> 
#include<deque> 
using namespace std;  
/*
请输入字符串,最多输入4个字符串,要求后输入的字符串排在前面
*/
int main()  
{
	cout<<"请输入字符串, 以#结束:"; 
	deque<string> que; 
	string a;
	int count=1;
	while(count<=4){
		cin>>a;
		if(a=="#"){
			break;
		}
		que.push_front(a);
		count++;
	} 
	count=1;
	for(deque<string>::iterator it=que.begin();it!=que.end();it++){
		cout<<count<<"="<<*it<<" ";
		count++;
	}
	
    return 0;  
}  

2、把上述最后结果保存到Name.txt中

count=1;
ofstream fout("result.txt");
for(deque<string>::iterator it=que.begin();it!=que.end();it++){
	//cout<<count<<"="<<*it<<" ";
	fout<<count<<"="<<*it<<" ";
	count++;
}
fout.close();

部分代码如上。

3、先输入一组数,然后输入其分组,按照分组统计出现次数并输出。
例如,输入数据3,2,3,8,8,2,3 

输入对应分组    1,2,3,2,1,3,1

输出:

1={2=0,3=2,8=1}

2={2=1,3=0,8=1}

3={2=1,3=1,8=0}

即每组中各数据出现的次数。

由于现在还不熟悉map和set以及pair的用法,这里我就采用链表的形式来统计。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
/*
先输入一组数,然后输入其分组,按照分组统计出现次数并输出。
例如,输入数据3,2,3,8,8,2,3?

输入对应分组 ???1,2,3,2,1,3,1
*/
struct node{
	int data;
	int index;
	struct node *next;
	struct node *sibling;
};
//查看是否有这个节点 
struct node* hasIndex(struct node * a, int value){
	struct node* b = a;
	while(b!=NULL){
		if(b->index == value){
			return b;
		}else{
			b=b->sibling;
		}
	} 
	return NULL;
}
//遍历得到集合中的去重元素 
vector<int> distinct(vector<int>& a){
	vector<int> b;
	b.push_back(a[0]);
	bool flag=false;
	for(int i=0;i<a.size();i++){
		for(int j=0;j<b.size();j++){
			if(a[i]==b[j]){
				flag = true;				
			}
		}
		if(!flag){
			b.push_back(a[i]);
		}
		flag=false;
	}
	return b;
}

int main()
{
	vector<int> value, index, content;
    cout<<"请输入数据:"<<endl;
    int a;
    while(cin>>a)
    {
        if(a==0)
            break;
        value.push_back(a);
    }
 	content = distinct(value);
 	sort(content.begin(), content.end());
 	
    cout<<"请输入分组:"<<endl;
    while(cin>>a)
    {
        if(a==0)
            break;
        index.push_back(a);
    }
    struct node* head, *p;
    head=new struct node;
    head->data = value[0];
    head->index = index[0];
    head->next = NULL;
    head->sibling = NULL;
    p = head;
	for(int i=1;i<index.size();i++){
		struct node* find = hasIndex(head, index[i]);
		if(find!=NULL){
			struct node* s = new struct node;
			s->data = value[i];
			s->index = index[i];
			s->next = find->next;
			s->sibling = NULL;
			find->next=s;
		}else{
			find = new struct node;
			find->data = value[i];
			find->index = index[i];
			find->next = NULL;
			find->sibling = NULL;
			p->sibling = find;
			p=p->sibling;
		}
	}
	p = head;
	struct node  *s;
	while(p!=NULL){
		int count=0, index=p->index;
		struct datacount{
			int data;
			int count;
		};
		struct datacount dataC[content.size()];
		//初始化 
		for(int i=0;i<content.size();i++){
			dataC[count].data=content[count];
			dataC[count].count=0;
			count++;
		}
		
		for(int i=0;i<content.size();i++){
			if(dataC[i].data==p->data){
				dataC[i].count++;
			}
		} 
	
		s = p->next;
		while(s!=NULL){
			for(int i=0;i<content.size();i++){
				if(dataC[i].data==s->data){
					dataC[i].count++;
				}
			} 
			s=s->next;
		}
		while(count!=content.size()){
			dataC[count].data=content[count];
			dataC[count].count=0;
			count++;
		}
		cout<<index<<"={";
		for(int i=0;i<content.size();i++){
			if(i==content.size()-1){
				cout<<dataC[i].data<<"="<<dataC[i].count;
			}else{
				cout<<dataC[i].data<<"="<<dataC[i].count<<",";				
			}
		}
		cout<<"}"<<endl;
		p=p->sibling;
	}
    return 0;
}

4、输入一个英文句子,把句子中的单词按出现次数按从多到少把单词和次数在屏幕上输出来,要求能识别英文句号和逗号,暂不要求识别其他符号。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
/*
4、输入一个英文句子,把句子中的单词按出现次数按从多到少把单词和次数在屏幕上输出来,要求能识别英文句号和逗号,暂不要求识别其他符号。
*/ 
struct word{
	string str;
	int n;
};
/*
pos 就是逗号所在的位置 
*/
bool containComma(string a, int &pos){
	for(int i=0;i<a.length();i++){
		char s = a[i];
		if(s==','){
			pos=i;
			return true;
		} 
	}
	return false;
}
bool containFullstop(string a){
	if(a[a.length()-1]=='.')
		return true;
	return false;
}
bool isContainStr(struct word w, string str){
	if(w.str==str)
		return true;
	return false;
}
void insertWord(vector<struct word>& vc, string str){
	bool flag = false;
	int index;
	if(vc.size()==0){
		struct word nw;
		nw.str = str;
		nw.n = 1;
		vc.push_back(nw);
	}else{
		for(int i=0;i<vc.size();i++){
			struct word w = vc[i];
			if(isContainStr(w, str)){
				flag = true;
				index = i;
			}
		}
		if(flag){
			vc[index].n++;
		}else{
			struct word nw;
			nw.str = str;
			nw.n = 1;
			vc.push_back(nw);
		}
		flag = false;//重置 
	}
}
bool cmp(struct word w1, struct word w2){
	return w1.n>w2.n;
} 
int main()
{
	vector<struct word> vc;
	cout<<"输入一个英文句子"<<endl;
	string str;
	int pos;//记录逗号位置 
	while(cin>>str){
		if(str=="."||str==""){
			break;
		}else if(containComma(str, pos)){
			//插入数据 
			insertWord(vc, str.substr(0, pos));
			insertWord(vc, str.substr(pos+1));
		}else if(containFullstop(str)){
			insertWord(vc, str.substr(0, str.length()-1));
			break;
		}else{
			insertWord(vc, str);
		}
	}
	sort(vc.begin(), vc.end(), cmp);
	for(int i=0;i<vc.size();i++){
		cout<<"单词:"<<vc[i].str<<" 出现的次数是:"<<vc[i].n<<endl;
	}
    return 0;
}

作者:无涯明月

上篇:北理复试上机题2008年

猜你喜欢

转载自blog.csdn.net/qq_26460841/article/details/88376612