2017年西工大计算机机试

  • 给定行数的不定长int型数据排序

这原本是道非常容易的题目,后来考虑到 不定长 多行 整形 数据的输入比较麻烦,定义char型数据比较容易,但是它只能输入个位数,故有了现在的方法

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

int len,i,j,temp;
void BubbleSort(int *st)
{
    len=sizeof(st);
    for(i=0; i<len; i++)
        for(j=len-1;j>i;j--)
        if (st[j-1]>st[j])
        {
            temp=st[j];
            st[j]=st[j-1];
            st[j-1]=temp;
        }
    for(i=0; i<len; i++)
        cout<<st[i]<<' ';
    cout<<endl;
}

int main()
{
	int n,cont;
	int temp;
	cout << "Input group number:";
	cin >> n;

    int ch[n][20];
    char c;
    for(int j=0;j<n;j++)
        for (int i=0; i<20; i++)
        {
            scanf("%d%c",&ch[j][i],&c);//回车,一行表一组
			if(c=='\n'){ //遇到回车退出
				break;
			}
		}
    for (int i=0; i<n; i++)
    {
        BubbleSort(ch[i]);
	}
	return 0;
}
  • {}[]()括号匹配的问题
    /*同2018
    全局变量和局部变量,要声明清楚*/
    #include <iostream>
    #include <cstring>
    #include <stack>
    
    using namespace std;
    
    stack <char> Stack;
    bool JudgeMatch(char *str)
    {
        bool flag=true;
        int len=strlen(str);
        for(int i=0;i<len;i++)
        {
            if(str[i]=='{'||str[i]=='['||str[i]=='(')
                Stack.push(str[i]);
            else if((str[i]=='}'&&Stack.top()=='{')||(str[i]==']'&&Stack.top()=='[')||(str[i]==')'&&Stack.top()=='('))
                Stack.pop();
            else
                continue;
        }
    
        if(!Stack.empty())
            flag=false;
        return flag;
    }
    
    int main()
    {
        int n,i;
        cin>>n;
        cin.get();
        char ch[n][20];
        for(i=0;i<n;i++)
            cin.getline(ch[i],20);
        for(i=0;i<n;i++)
            if(JudgeMatch(ch[i]))
                cout<<"Yes"<<endl;
            else
                cout<<"No"<<endl;
        return 0;
    }

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值