欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2012年北理复试上机题

程序员文章站 2022-06-07 23:49:49
...

2012年北理复试上机题

#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
	int i,a[10];
	for(i=0;i<10;i++)
	{
		scanf("%d",&a[i]);

	}
	sort(a,a+10);
	for(i=0;i<10;i++)
	{
		printf("%d ",a[i]);

	}
	printf("\n");


}

2012年北理复试上机题

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#define MaxSize 100

using namespace std;
typedef struct student
{
	int num;
	string name;
	string sex;
	int age;
}student;

bool cmp(student a,student b)
{
	return a.num<=b.num;
}
void Print(vector<student> stu)
{
	vector<student>::iterator it;
	for(it=stu.begin();it!=stu.end();it++)
	{
		if(it!=stu.begin())
			cout<<" , ";
		cout<<"("<<it->num<<","<<it->name<<","<<it->sex<<","<<it->age<<")";

	}
}



int main()
{
	
	vector<student> stu;
	student a;
	a.num=10;a.name="wes";a.sex="f";a.age=23;
	stu.push_back(a);
	a.num=20;a.name="ert";a.sex="f";a.age=45;
	stu.push_back(a);
	a.num=30;a.name="str";a.sex="t";a.age=89;
	stu.push_back(a);
	Print(stu);
	char order[100];
	cout<<"\n请输入指令"<<endl;
	cin>>order;
	int temp;
	while(order!="#")
	{
		if(order[0]=='I')
		{
			char name[30];
			char sex[30];
			sscanf(order,"I%d,%[^,],%[^,],%d",&a.num,name,sex,&a.age);
			a.name=name;
			a.sex=sex;
//			sscanf(order,"[^,]",b);
//			cout<<b<<"\n"<<endl;
			stu.push_back(a);
		}
		else if(order[0]=='D')
		{
			sscanf(order,"D%d",&temp);
			vector<student>::iterator it;
			for(it=stu.begin();it!=stu.end();)
			{
				if(it->num==temp)
					stu.erase(it);
				else
					it++;
			}
		}
		sort(stu.begin(),stu.end(),cmp);
		Print(stu);
		cout<<"\n请输入指令"<<endl;
		cin>>order;

	}
	
	return 0;
}

 这题遇到了很多问题,首先是sscanf(),在使用这个时,无论是前面的字符串还是后面用来接受的变量,注意都是字符数组,不要用string,但可以接收后,直接赋值给string。

(char []类型给string:直接赋值,反之不可,用for(i=0;i<str.length();i++) ch[i]=str[i];   结尾加上'\0')

当结构体类型是  迭代器 it,,访问它内容的时候就不要*,,,直接it->first.

vi.begin(),,括号勿忘。

2012年北理复试上机题

#include <cstdio>
#include <iostream>
#include <string>
#define MaxSize 100010;

using namespace std;

string str1;
string str2;

void pre_order(int af,int ae,int bf,int be)   //af:后续起点,ae:后续终点,bf:终须起点,be:中序终点
{
	if(af>ae || bf>be )
		return ;
	if(af==ae && bf==be)
	{
		printf("%c",str1[af]);
		return ;
	}
	printf("%c",str1[ae]);
	for(int i=bf;i<=be;i++)
	{
		if(str2[i]==str1[ae])
			break;
	}
	pre_order(af,af+(i-bf)-1,bf,i-1);
	pre_order(af+(i-bf),ae-1,i+1,be);

}

int main()
{
	cout<<"输入(按后序,中序)"<<endl;
	cin>>str1>>str2;
	pre_order(0,str1.length()-1,0,str2.length()-1);
	cout<<endl;

	return 0;


}

 

#include <cstdio>
#include <iostream>
#include <string>
#define MaxSize 100010;

using namespace std;

string str1;
string str2;

void pre_order(int af,int ae,int bf,int be)   //af:后续起点,ae:后续终点,bf:终须起点,be:中序终点
{
	if(af>ae || bf>be )
		return ;
	if(af==ae && bf==be)
	{
		printf("%c",str1[af]);
		return ;
	}
	printf("%c",str1[ae]);
	for(int i=bf;i<=be;i++)
	{
		if(str2[i]==str1[ae])
			break;
	}
	pre_order(af,af+(i-bf)-1,bf,i-1);
	pre_order(af+(i-bf),ae-1,i+1,be);

}

void post_order(int af,int ae,int bf,int be) //a:先序,b:中序
{
	if(af>ae || bf > be)
		return ;
	for(int i=bf;str2[i]!=str1[af];i++);
	post_order(af+1,af+i-bf,bf,i-1);
	post_order(af+i-bf+1,ae,i+1,be);

	printf("%c",str1[af]);

}

int main()
{
/*	cout<<"输入(按后序,中序)"<<endl;
	cin>>str1>>str2;
	pre_order(0,str1.length()-1,0,str2.length()-1);
*/
	cout<<"输入(按先序,中序)"<<endl;
	cin>>str1>>str2;
	post_order(0,str1.length()-1,0,str2.length()-1);
	cout<<endl;

	return 0;


}

 

#include <cstdio>
#include <iostream>
#include <string>


using namespace std;

typedef struct node
{
	char ch;
	struct node* l;
	struct node* r;
}TNode,*Tree;

string str1;
string str2;

void DFS(Tree T)
{
	if(T==NULL)
	{
		return;
		cout<<"NULL";
	}
	printf("%c",T->ch);
	DFS(T->l);
	DFS(T->r);
}

Tree create(int af,int ae,int bf,int be)
{
	if(af>ae || bf>be )
		return NULL;
	Tree T=(Tree)malloc(sizeof(TNode));
	T->l=NULL;
	T->r=NULL;
	T->ch=str1[ae];
	for(int i=bf;i<=be;i++)
	{
		if(str2[i]==str1[ae])
			break;
	}
	T->l=create(af,af+(i-bf)-1,bf,i-1);
	T->r=create(af+(i-bf),ae-1,i+1,be);
	return T;

}


int main()
{
	Tree T;
	cout<<"输入(按后序,中序)"<<endl;
	cin>>str1>>str2;
	T=create(0,str1.length()-1,0,str2.length()-1);
	DFS(T);


	return 0;


}