北京理工大学2013年考研复试上机试题

在这里插入图片描述

#include<iostream>
#include<algorithm>
using namespace std;
int gys(int a,int b)
{
	int t;
	if(a<b)
	swap(a,b);
	while(t=a%b)
	{
		a=b;
		b=t;	
	}
	return b;
}
int main()
{
	int a,b;
	cout<<"请输入两个数,0 0作为程序结束标志"<<endl;
	while(cin>>a>>b)
	{
		if(a==0&&b==0)
		break; 
		cout<<"公约数为:"<<gys(a,b)<<endl;
		cout<<"请输入两个数,0 0作为程序结束标志"<<endl;
	}
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<ctype.h>
#include<algorithm>
using namespace std;
int main()
{
	vector<string>vcs;
	string s,str;
	while(getline(cin,str))
	{
		if(str=="0000")
		break;
		int st=-1,end=-1,n=0;
		bool flag=false;
		for(int i=0;i<str.length();i++)
		{
			if(!flag)
		    {
		    	if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z'))
			    {
				    st=i;
				    flag=true;
			    }
		    }
		    else
		    {
		    	if(str[i]==' ')
		    	{
		    		end=i;
		    		s=str.substr(st,end-st);
		    		vcs.push_back(s);
		    		n++;
		    		st=-1;
		    		flag=false;
				}
			}
		}
		s=str.substr(st,str.length()-st);
		vcs.push_back(s);
	    n++;
	    for(int i=0;i<n;i++)
		cout<<vcs[i]<<' ';
		cout<<endl;
	    for(int i=0;i<n;i++)
	    for(int j=i+1;j<n;j++)
	    {
	    	string a=vcs[i];
	    	string b=vcs[j];
	    	a[0]=toupper(a[0]);
	    	b[0]=toupper(b[0]);
	    	if(a>b)
	    	swap(vcs[i],vcs[j]);
	    	else if(a==b)
	    	{
	    		if(vcs[i]>vcs[j])
	    		swap(vcs[i],vcs[j]);
			}
		}
		for(int i=0;i<n;i++)
		cout<<vcs[i]<<' ';
		cout<<endl;
	}
	return 0;
}

在这里插入图片描述

#include<iostream>
#include<stack>
using namespace std;
int yxj(char op)
{
	int num;
	switch(op)
	{
		case '+':num=2;break;
		case '-':num=2;break;
		case '*':num=3;break;
		case '/':num=3;break;
	}
	return num;
}
int main()
{
	string str;
	cout<<"请输入一个表达式:"<<endl;
	while(cin>>str)
	{
		if(str=="0000")
		break;
		stack<char>op;
	    stack<char>st;
	    for(int i=str.length()-1;i>=0;i--)
	    {
	    	if(str[i]>='a'&&str[i]<='z')
	    	st.push(str[i]);
	    	else if(str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/')
	    	{
	    		while(true)
	    		{
	    			if(op.empty()||op.top()==')')
	    			{
	    				op.push(str[i]);
	    				break;
					}
	    			else if(yxj(str[i])>=yxj(op.top()))
	    			{
	    				op.push(str[i]);
	    				break;
					}
					else
					{
						char temp=op.top();
						op.pop();
						st.push(temp);
					}
				}
			}
			else
			{
				if(str[i]==')')
				op.push(str[i]);
				else
				{
					while(op.top()!=')')
					{
						char b=op.top();
						op.pop();
						st.push(b);
					}
					op.pop();
				}
			}
		}
		while(!op.empty())
		{
			char c=op.top();
			op.pop();
			st.push(c);
		}
		cout<<"该表达式的先序遍历结果为:"<<endl;
		while(!st.empty())
		{
			cout<<st.top();
			st.pop();
		}
		cout<<endl;
		cout<<"请输入一个表达式,0000表示程序结束"<<endl;
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值