北京理工大学2012年计算机考研机试题

在这里插入图片描述

#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	vector<int>op;
	vector<int>::const_iterator it;
	int m=10,temp;
	while(m--)
	{
		cin>>temp;
		op.push_back(temp);
	}
	sort(op.begin(),op.end());
	for(it=op.begin();it!=op.end();it++)
	cout<<*it<<' ';
	cout<<endl;
}

在这里插入图片描述

#include<iostream>
#include<stdio.h>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
struct student
{
	int num;
	string name;
	string gender;
	int age;
};
bool cmp(student a,student b)
{
	return a.num<b.num;
}
int sum(string temp)
{
	int s=0;
	for(int i=0;i<temp.length();i++)
	s+=(temp[i]-'0')*pow(10,temp.length()-1-i);
	return s;
}
int main()
{
	student stu[1000];
	stu[0]={10,"wes","f",23};
	stu[1]={20,"ert","f",45};
	stu[2]={30,"str","t",89};
	char op;
	int i=2,m=0;
	string arr[4];
	cout<<"请输入操作"<<endl; 
	while(cin>>op)
	{
		if(op=='I')
		{
			string data;
			cin>>data;
			int st=0,end=-1,m=0;
			for(int j=0;j<data.length();j++)
			{
				if(data[j]==',')
				{
					end=j;
					string temp=data.substr(st,end-st);
					arr[m++]=temp;
					st=j+1;
				}
			}
			arr[m++]=data.substr(st,data.length()-st);
			stu[++i].num=sum(arr[0]);
			stu[i].name=arr[1];
			stu[i].gender=arr[2];
			stu[i].age=sum(arr[3]);
		}
		else if(op=='D')
		{
			int num;
			cin>>num;
			for(int j=0;j<=i;j++)
			if(stu[j].num==num)
			{
				for(int k=j;k<i;k++)
				{
					stu[k].num=stu[k+1].num;
					stu[k].name=stu[k+1].name;
					stu[k].gender=stu[k+1].gender;
					stu[k].age=stu[k+1].age;
				}
				i--;
				break;
			}
		}
		else
		break;
		sort(stu,stu+i+1,cmp);
		for(int j=0;j<=i;j++)
		cout<<"("<<stu[j].num<<","<<stu[j].name<<","<<stu[j].gender<<","<<stu[j].age<<") ";
		cout<<endl;
	}
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<string>
using namespace std;
struct TreeNode
{
	char data;
	TreeNode* leftNode;
	TreeNode* rightNode;
};
TreeNode* Build(string str1,string str2)
{
	if(str1.length()==0)
	return NULL;
	char c=str1[str1.length()-1];
	TreeNode* root=(TreeNode*)malloc(sizeof(TreeNode));
	root->data=c;
	int pos=str2.find(c);
	root->leftNode=Build(str1.substr(0,pos),str2.substr(0,pos));
	root->rightNode=Build(str1.substr(pos,str1.length()-pos-1),str2.substr(pos+1));
	return root;
}
void posOrder(TreeNode* root)
{
	if(root==NULL)
	return;
	cout<<root->data; 
	posOrder(root->leftNode);
	posOrder(root->rightNode);
	return;
}

int main()
{
	string str1,str2;
	cout<<"请输入两个字符串:"<<endl;
	while(cin>>str1>>str2)
	{
		if(str1=="00"&&str2=="00")
		break;
		cout<<"前序遍历结果为:";
		TreeNode* root=Build(str1,str2);
		posOrder(root);
		cout<<endl;
		cout<<"请输入两个字符串,00 00作为程序结束标志"<<endl;
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值