牛客网输入输出练习(python/C++)

本文主要针对牛客网上输入输出练习进行总结,各路大神的解答见 :https://ac.nowcoder.com/acm/contest/5657#question

1. A + B (输入无行数)

在这里插入图片描述

python解答

这里没有给输入的行数,因此用一个try except来判断停止输入。

while True:
    try:
        a, b = map(int, input().split())
        print(a + b)
    except:
        break

c++解答

c++这里主要需要用while cin来判断是否停止输入

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int a, b;
    while(cin >> a >> b)
        cout << a + b << endl;
    
    return 0;
}

2. A + B (给定行数的输入)

在这里插入图片描述

这里给了输入的行数,那就很简单了。

python解答

n = int(input())
while n:
    a, b = map(int, input().split())
    print(a + b)
    n -= 1

c++解答

#include<bits/stdc++.h>
using namespace std;

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

3. A + B (指定条件结束)

下面是题目的描述
在这里插入图片描述

这里的话,需要根据输入判断是否停止。

python解答

while True:
    a, b = map(int, input().split())
    if a != 0 and b != 0: print(a + b)
    else: break

c++解答

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int a, b;
    while(cin >> a >> b)
    {
    
    
        if(a == 0 && b == 0)  break; 
        cout << a + b << endl;
    }
    return 0;
}

4. A + B (给定长度的数组求和,行内)

在这里插入图片描述

python解答

while True:
    arr = list(map(int, input().split()))
    if arr[0] == 0: break
    else: print(sum(arr[1:]))

c++解答

说明了他的数组长度那就很舒服了。

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int n;
    while(cin >> n)
    {
    
    
        if (n == 0) break;
        int sum = 0, a;
        while(n --)
        {
    
    
            cin >> a;
            sum += a;
        }
        cout << sum << endl;
    }
    return 0;
}

5. A + B (单独给定数组长度, 行间)

在这里插入图片描述

python解答

人生苦短啊!

n = int(input())
while n:
    print(sum(list(map(int, input().split()))[1:]))
    n -= 1

c++解答

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int n;
    cin >> n;
    while(n --)
    {
    
    
        int len, sum = 0, a;
        cin >> len;
        while(len --)
        {
    
    
            cin >> a;
            sum += a;
        }
        cout << sum << endl;
    }

    return 0;
}

6. A + B (未指定行,行内指定数组长度)

在这里插入图片描述

python解答

while True:
    try:  # 对于没有行提示的,使用try except就很简单
        print(sum(list(map(int, input().split()))[1:]))
    except:
        break

c++解答

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int n;
    while(cin >> n)
    {
    
    
        int sum = 0, a;
        while(n --)
        {
    
    
            cin >> a;
            sum += a;
        }
        cout << sum << endl;
    }
    
    return 0;
}

7. A + B (不说行数和每行数组的长度)

在这里插入图片描述

python解答

老三样就:try exceptinput splitlist map

while True:
    try:
        print(sum(list(map(int, input().split()))))
    except:
        break

c++解答

这题做法主要有三种:

  • 做法1:可以把每行一个string转成一个流再进行求和,stingstream用到的头文件是#include <sstream>
  • 作法2: 使用cin.get() 判断回车
  • 做法3: 通用做法,强烈建议了解!!!!! 这里读行数据,我们使用getline(cin, s)不要加第三个分割参数,当我们拿到这一行的字符换之后,我们再使用geline(ss, res, ',')这里面的ss是你自己定义的stringstream,然后res是你需要split出来的子串,然后‘,’指的是你使用逗号分割流,当然也可以用别的,根据题目可以改变;
// 做法一:使用stringstream
#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    string s;
    while (getline(cin, s)) 
    {
    
    
        stringstream ss(s);
        int sum = 0, num;
        while (ss >> num) //这里可以改成while(getline(ss, num, ' ')), 但是num类型要换成string,另一方面,sum += stoi(num)
            sum += num;
        cout << sum << "\n";
    }
}

// 做法二:使用cin.get() 读取不用的字符,从而来帮助判断
#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int a;
    while (cin >> a) 
    {
    
    
        int sum = 0;
        while(true)
        {
    
    
        	/*
        	1.这个cin.get()的判断要放在运算后,不然最后一个数字都不进来;
        	2.cin会在空格或这换行的时候停止读取,因此,这个时候可以用cin.get()读取,从而帮助判断是否本行结束
        	*/
        	sum += a;
            if(cin.get() == '\n') break; 
            cin >> a;
        }
        cout << sum << "\n";
    }
}

// 做法三:ss的通用做法
#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    string s;
    while (getline(cin, s)) 
    {
    
    
        stringstream ss(s);
        int sum = 0;
        string num;
        while (getline(ss, num, ' ')) // geline函数第三个参数是按照那个字符来进行流的分割
            sum += stoi(num);
        cout << sum << "\n";
    }
}

8. 字符串排序(指定字符个数)

下面是题目的描述

在这里插入图片描述

python解答

直接读入就可以了,n其实都不需要

n = int(input())
arr = input().split()
arr.sort()
print(' '.join(arr))

c++解答

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int n;
    cin >> n;
    vector<string> ss;
    while(n --)
    {
    
    
        string str;
        cin >> str;
        ss.push_back(str);
    }
    sort(ss.begin(), ss.end());
    for(auto str: ss) cout << str << " ";
    return 0;
    
}

9. 字符串排序(未指定行数和字符串个数)

在这里插入图片描述

python解答

while True:
    try:
        arr = input().split()
        arr.sort()
        print(" ".join(arr))
    except:
        break

c++解答

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int n;
    string line;
    while(getline(cin, line))
    {
    
    
        vector<string> arr;
        stringstream ss(line);
        string item;
        while(getline(ss, item, ' '))
            arr.push_back(item);

        sort(arr.begin(), arr.end());
        for(auto str: arr) cout << str << " ";
        cout << endl;
    }

    return 0;
    
}

10. 字符串排序(未指定行数和字符串个数,逗号分割)

在这里插入图片描述

python解答

while True:
    try:
        arr = input().split(",")
        arr.sort()
        print(",".join(arr))
    except:
        break

c++解答

#include<bits/stdc++.h>
using namespace std;

int main()
{
    
    
    int n;
    string line;
    while(getline(cin, line))
    {
    
    
        vector<string> arr;
        stringstream ss(line);
        string item;
        while(getline(ss, item, ','))
            arr.push_back(item);

        sort(arr.begin(), arr.end());
        for(int i = 0; i < arr.size(); i++) 
        {
    
    
            cout << arr[i];
            if (i != arr.size() - 1) cout << ",";
        }
            
        cout << endl;
    }

    return 0;
    
}

11. 其他练习

这是一个符号匹配的问题,
样例如下

样例:
3
10
if
while
loop
end loop
end while
for
end for
end if
switch
end switch
4
if
while
end if
end while
3
if
end if
if

解答:
Yes
No
No
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>


using namespace std;

bool judge(vector<string> &arr)
{
    
    
    vector<string> stack;
    for(auto cmd: arr)
    {
    
    
        stringstream ss(cmd);
        string item;
        while(getline(ss, item, ' '))
        {
    
    
            if(item == "end") 
            {
    
    
                ss >> item;
                // cout << item << "ok?" << endl;
                if(stack.empty() | stack.back() != item) return false;
                else stack.pop_back();
            }
            else stack.push_back(item);
        }
    }
    if(stack.empty()) return true;
    else return false;
}

int main()
{
    
    
    int t;
    cin >> t;
    while(t --)
    {
    
    
        int n;
        cin >> n;
        vector<string> arr;
        cin.get(); // cin不会读这个回车,因此需要get()吃一下回车!!!!!!!!!
        while(n --)
        {
    
    
            string cmd;
            getline(cin, cmd);
            arr.push_back(cmd);
        }
        
        bool res = judge(arr);
        if(res) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36306288/article/details/124215430