2018_西工大机试_C++

2018

要求 : 达到输入一行 输出一行即可


1.求积:给定n组数,每组两个整数,输出这两个整数的乘积.
Input:
2
1 1
2 3
 
Output:
1
6
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    while(n--){
        int a, b;
        cin>>a>>b;
        cout<<a*b;
    }
    return 0;
}

2.阶乘:给定n组数,每组一个整数,输出该组数的阶乘。

Input:
2 
3 
5
 
Output:
6
120
#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    while(n--){
        int m, sum;
        cin>>m;
        sum = m;
        while(--m){
            sum*=m;
        }
        cout<<sum;
    }
    return 0;
}

3.C(n,m):求出n个数中任取m个数的不同取法个数:

Input:
10 3
 
Output:
120
#include <iostream>

using namespace std;

int Facorial(int x){
    int sum = x;
    while(--x){
        sum *= x;
    }
    return sum;
}

int main()
{
    int n, m;
    cin>>n>>m;
    cout<<Facorial(n)/Facorial(n-m)/Facorial(m);
    return 0;
}

4.给定n组数,每组m个,对每组数进行从小到大排序

Input:
2 4
3 5 2 8
2 7 9 8
 
Output:
2 3 5 8
2 7 8 9
#include <iostream>
#include <set>
using namespace std;

int main()
{
    multiset<int> s;
    int n, m, p;
    cin>>n>>m;
    while(n--){
        p = m;
        while(m--){
            int a;
            cin>>a;
            s.insert(a);
        }
        for(set<int>::iterator it = s.begin(); it != s.end(); it++){
			cout<<(*it)<<" ";
		}
		s.clear();
		cout<<endl;
        m = p;
    }
	return 0;
}

5.字符串反转:给定n组字符串,每组字符串不超过20,输出每组字符串的反串:

Input:
3
nwpu
china
xi an
 
Output:
upwn
anihc
na ix
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n;
    string a, b;
    cin>>n;
    cin.ignore();
    while(n--){
        getline(cin, a);
        while(!a.empty()){
            b.push_back(a.at(a.length()-1));
            a.erase(a.length()-1, 1);
        }
        cout<<b<<endl;
        b.clear();
    }

    return 0;
}

6.判断是否回文:(包括空格 数字 英文 符号) ,若是输出yes,否则输出no;

Input:
4
nwpu
madam
1001
xi ix
 
Output:
no
yes
yes
yes
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n;
    string s, t;
    cin>>n;
    cin.ignore();
    while(n--){
        getline(cin, s);
        if(!s.length()%2){
            s.erase(s.length()/2);
        }
        for(int i = 0; i <= (s.length()/2); i++){
            if(s.at(i) != s.at(s.length() - 1 - i)){
                t = "no";
                break;
            }
        }
        cout<<t<<endl;
        t = "yes";
    }

    return 0;
}

7.判断括号是否匹配:给定n组数,每组为一个字符串,测试三种括号:{}()[],且顺序都是前左括号,后右括号,括号之间可以嵌套。若匹配则输出yes,否则输出no;

Input:
2
{9}[00](tt)
{[](}
 
Output:
yes
no
#include <iostream>
#include <stack>
#include <string>

using namespace std;

bool filter(string s){
    stack<char> st;
    string left = "{[(";
    string right = ")]}";
    for(string::iterator it = s.begin(); it != s.end(); ++it){
        if(left.find((*it)) != string:: npos){
            st.push(*it);
        }else if(right.find((*it)) != string:: npos){
            char c = st.top();
            if((c=='('&&(*it)==')')||(c=='{'&&(*it)=='}')||(c=='['&&(*it)==']')){
                st.pop();
            }else{
                return false;
            }
        }
    }
    if(st.empty()){
        return true;
    }else{
        return false;
    }
}

int main()
{
    int n;
    string s;
    cin>>n;
    cin.ignore();
    while(n--){
        getline(cin, s);
        if(filter(s)){
            cout<<"yes"<<endl;
        }else{
            cout<<"no"<<endl;
        }
        s.clear();
    }
	return 0;
}


参考资料:

cin与getline混用需谨慎
getline
c++ String
c++ String 三种遍历方式

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值