2013年北理复试上机题

#include <cstdio>


int gcd(int n,int m)            //这种方法更好
{
	if(m==0)
	{
		return n;
	}
	else
		return gcd(m,n%m);
}
int f(int n,int m)  //n>m
{
	int t;
	if(n%m==0)
		return m;
	
	t=n-m;
	if(t>m)
		return f(t,m);
	else
		return f(m,t);

	

}

int main()
{
	int n=0,m=0,result,flag=0;
	printf("请输入\n");
//-----------输入部分------------//由于,有中英文之分,所以通过字符串来得到两个数字
	char c;
	c=getchar();
	while(c!='\n')
	{
		if(c>='0' && c<='9')
		{
			if(flag==0)
				n=n*10+c-'0';
			if(flag==1)
				m=m*10+c-'0';
		}
		else
		{
			if(flag==0)
				flag=1;
		}
		c=getchar();
	}
//-----------输入部分------------
//	scanf("%d,%d",&n,&m);
	if(n>m)
		result=f(n,m);
	else
		result=f(m,n);
	printf("%d\n",result);
	return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct
{
	string str;
	double value;
}word;
/*
bool cmp(word a,word b)
{
	
	return a.value<b.value;
}
*/
bool cmp(string a,string b)
{
	int i;
	for(i=0;i<a.length();i++)
		if(a[i]>='A' && a[i]<='Z')
			a[i]+=('a'-'A');

	for(i=0;i<b.length();i++)
		if(b[i]>='A' && b[i]<='Z')
			b[i]+=('a'-'A');
	return a<b;
}
void getvalue(word &a)            //超范围哦,太大啦
{
	a.value=0;
	for(int i=0;i<a.str.length();i++)
	{
		if(a.str[i]>='A' && a.str[i]<='Z')
			a.value=a.value*26+a.str[i]-'A';

		if(a.str[i]>='a' && a.str[i]<='z')
			a.value=a.value*26+a.str[i]-'a';
		
	}
}



int main()
{
	string a;
	char ch;
	vector<string> vi;
	while(1)
	{
		cin>>a;
		vi.push_back(a);
		ch=getchar();  //接受每个string的字符,若是回车,则表明结束
		if(ch=='\n')
			break;
		

	}
	sort(vi.begin(),vi.end(),cmp);
	for(vector<string>::iterator it=vi.begin();it!=vi.end();it++)
	{
		cout<<*it<<" ";
	}
cout<<endl;
	return 0;

}
/*
int main()
{
	word a;
	char c;
	int max=0;
	vector<word> vi;
	while(1)
	{
		cin>>a.str;
		if(a.str.length()>max)
			max=a.str.length();
		getvalue(a);
		vi.push_back(a);
		c=getchar();
		if(c=='\n')
			break;
	}
	for(vector<word>::iterator it=vi.begin();it!=vi.end();it++)
	{
		for(int i=0;i<max-a.str.length();i++)
		{
			a.value=a.value*26;
		}
	}
	sort(vi.begin(),vi.end(),cmp);

	for(it=vi.begin();it!=vi.end();it++)
	{
		cout<<(*it).str<<" ";
	}

	return 0;

}
*/

有中序遍历和后序遍历得到前序遍历

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

using namespace std;

char post[100],in[100];  //post:后序,in:前序

typedef struct node
{
	char value;
	struct node *lchlid;
	struct node *rchild;
	
}TNode,*Tree;

int prio_out(char a)
{
	char ch[6]={'+','-','*','/','(',')'};
	int  priority[6]={1,1,2,2,3,0};
	for(int i=0;i<6;i++)
	{
		if(ch[i]==a)
			break;
			
	}
	return priority[i];
}
int prio_in(char a)
{
	char ch[6]={'+','-','*','/','(',')'};
	int  priority[6]={1,1,2,2,0,0};
	for(int i=0;i<6;i++)
	{
		if(ch[i]==a)
			break;
			
	}
	return priority[i];
}

void pre_order(Tree T )
{
	if(T==NULL)
		return ;
	printf("%c",T->value);
	pre_order(T->lchlid);
	pre_order(T->rchild);
}
Tree CreateTree(char post[],int n)
{
	stack<Tree> s;
	Tree T;
	Tree p;
	for(int i=0;i<n;i++)
	{
		p=(Tree)malloc(sizeof(TNode));
		p->value=post[i];
		p->lchlid=p->rchild=NULL;
		if(p->value=='+' || p->value=='-' || p->value=='*' || p->value=='/' )
		{
			if(s.empty()==true)
				printf("stack is empty\n");
			p->rchild=s.top();
			s.pop();
			if(s.empty()==true)
				printf("stack is empty\n");
			p->lchlid=s.top();
			s.pop();
			s.push(p);
		}
		else
		{
			s.push(p);
		}
		

	}
	if(s.empty()==true)
		printf("stack is empty\n");
	else
	{
		T=s.top();
		s.pop();
	}
	
	return T;
}

int main()
{
	char ch[100];
	
	stack<char> s;
	scanf("%s",ch);
	int i=0,n_i=0,n_p=0;
	while(ch[i]!='\0')
	{
		if(ch[i]!='('&& ch[i]!=')')          //获取中序表达式
			in[n_i++]=ch[i];
		if(ch[i]>='a' && ch[i]<='z')
			post[n_p++]=ch[i];
		else if(ch[i]=='+' ||ch[i]=='-' || ch[i]=='*' ||ch[i]=='/'||ch[i]=='(' ||ch[i]==')' )
		{
			if(s.empty())    //如果站空,直接入站
				s.push(ch[i]);
			else if(ch[i]==')')
			{
				while(s.top()!='(')
					{
						post[n_p++]=s.top();
						s.pop();
					}
				s.pop();  //左括号出zhan

			}
			else
			{
				if(prio_out(ch[i])>prio_in(s.top()))
					s.push(ch[i]);
				else
				{
					while(!s.empty() && prio_out(ch[i])<=prio_in(s.top()))
					{
						post[n_p++]=s.top();
						s.pop();
					}
					s.push(ch[i]);
				}
			}
		}

		i++;
	}
	while(!s.empty())
	{
		post[n_p++]=s.top();
		s.pop();
	}
	post[n_p]='\0';
	in[n_i]='\0';
	cout<<in<<endl;
	cout<<post<<endl;
	Tree T=CreateTree(post,n_p);
	
	pre_order(T);
	return 0;
}

 

#include <iostream>
#include <cstdio>
#include <string>
#include <stack>
using namespace std;

int priout(char c)
{
	char ch[6]={'+','-','*','/','(',')'};
	int p[6]={1,1,2,2,4,0};
	for(int i=0;i<6;i++)
	{
		if(c==ch[i])
			return p[i];
	}
}
int priin(char c)
{
	char ch[6]={'+','-','*','/','(',')'};
	int p[6]={1,1,2,2,0,0};
	for(int i=0;i<6;i++)
	{
		if(c==ch[i])
			return p[i];
	}
}
bool IsFuhao(char c)
{
	if(c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')')
		return true;
	else
		return false;
}

typedef struct node 
{
	char value;
	struct node *lchild;
	struct node *rchild;
}Node,*LNode;

LNode Create(string post)
{
	stack<LNode> s;
	for(int i=0;i<post.length();i++)
	{
		LNode T=new(Node);
		T->value=post[i];
		T->lchild=NULL;
		T->rchild=NULL;
		if(IsFuhao(post[i]))
		{
			LNode node1=NULL;
			LNode node2=NULL;
			if(!s.empty())
			{
				node2=s.top();
				s.pop();
			}
			if(!s.empty())
			{
				node1=s.top();
				s.pop();
			}
			T->lchild=node1;
			T->rchild=node2;
			s.push(T);
		}
		else
		{
			s.push(T);
		}
	}
	if(!s.empty())
		return s.top();


}
void pre(LNode T)
{
	if(T==NULL)
		return ;
	cout<<T->value;
	pre(T->lchild);
	pre(T->rchild);

}

int main()
{
	string str;
	cin>>str;
	stack<char> s;
	string post;
	for(int i=0;i<str.length();i++)
	{
		if(IsFuhao(str[i]))
		{
			if(s.empty())
				s.push(str[i]);
			else
			{
				if(str[i]==')')
				{
					if(s.empty())
					{
						cout<<"表达式有误"<<endl;
						return 0;
					}
					while(s.top()!='(')
					{
						post+=s.top();
						s.pop();
						if(s.empty())
						{
							cout<<"表达式有误"<<endl;
							return 0;
						}

					}
					if(s.top()=='(')
						s.pop();
				}
				else
				{
					while(!s.empty()&&(priout(str[i])<=priin(s.top())))
					{
						post+=s.top();
						s.pop();
					}
					s.push(str[i]);
				}
			}

		}
		else
		{
			post+=str[i];
		}
	}
	while(!s.empty())
	{
		post+=s.top();
		s.pop();
	}
	cout<<post;
	LNode T=Create(post);
	pre(T);
	cout<<endl;
	return 0;
	

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值