牛客 acm输入输出模式练习

发布时间 2023-03-24 11:31:59作者: 思wu邪

https://www.nowcoder.com/exam/test/67432019/detail?pid=27976983#question

注意:只有部分个人觉得有意义的题目

  1. A+B(4)

计算一系列数的和

打开以下链接可以查看正确的代码

https:``//ac.nowcoder.com/acm/contest/5657#question

数据范围:数据组数满足 1≤�≤100 1≤t≤100 ,每组数据中整数个数满足 1≤�≤100 1≤n≤100 ,每组数据中的值满足 1≤���≤100 1≤val≤100

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 256M,其他语言512M

输入描述:

输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。

输出描述:

每组数据输出求和的结果

示例1

输入例子:

4 1 2 3 4
5 1 2 3 4 5
0

输出例子:

10
15

个人分析: 难点在于一行的个数是未知的,因此首先需要分行。

分行用到的函数或者结构有:getlinestringstream

#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;


int main() {
    ios::sync_with_stdio(false);
    string line;
    while (getline(cin,line)) {
        stringstream ss;
        ss << line;
        long long res = 0;
        int n = 0;
        ss >> n;
        if (n == 0) {
            break;
        }
        else {
            while (n--) {
                int tmp; ss >> tmp;
                res += tmp;
            }
        }
        cout << res << endl;;
    }
}
// 64 位输出请用 printf("%lld")

A+B(6)

计算一系列数的和

打开以下链接可以查看正确的代码

https:``//ac.nowcoder.com/acm/contest/5657#question

数据范围: 1≤�≤1000 1≤n≤1000 , 所有数都满足 1≤���≤1000 1≤val≤1000

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 256M,其他语言512M

输入描述:

输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

输出描述:

每组数据输出求和的结果

示例1

输入例子:

4 1 2 3 4
5 1 2 3 4 5

输出例子:

10
15

难点: 难点在于不知道有多少行。

解决:判断line是否为空即可。

getline函数会自己判断是否为空(和cin类似),因此直接将getline写在while中即可。

#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;


int main() {
    ios::sync_with_stdio(false);
    string line;
    while (getline(cin,line)) {
        stringstream ss;
        ss << line;
        int n; ss >> n;
        int res = 0;
        while (n--) {
            int t = 0; ss >> t;
            res += t;
        }
        cout << res << endl;
    }
}
// 64 位输出请用 printf("%lld")

字符串排序(1)

对输入的字符串进行排序后输出

打开以下链接可以查看正确的代码

https:``//ac.nowcoder.com/acm/contest/5657#question

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 256M,其他语言512M

输入描述:

输入有两行,第一行n

第二行是n个字符串,字符串之间用空格隔开

输出描述:

输出一行排序后的字符串,空格隔开,无结尾空格

示例1

输入例子:

5
c d a bb e

输出例子:

a bb c d e

难点:

  1. c++自带的string compare函数的比较规则
  2. 如何自定义比较规则。

解答:

  1. c++自带的string compare函数的比较规则

compare()比较时逐字符比较的,一旦能比较出结果,就不再比较了。 例如“abc”和“adf”,首先a和a比较,比不出结果;则b和d比较,结果就是“abc”小于“adf”,返回-1,即字典里“abc”在“adf”前面。例如“abc”和“abcd”比较,若“abc”都比完了,“abcd”还没完,说明“abc”小,返回值为-1,字典里“abc”靠前。总之记住这个比较规则和字典顺序一致即可。

  1. 自定义比较规则

见代码

使用的是lamda表达式

sort(vt.begin(), vt.end(), [](string a, string b)->bool {
        int i = 0; int j = 0;
        while (a[i] == b[j]) {
            i++; j++;
            if (i == a.size()) { return true; }
            if (j == b.size()) { return false; }
        }
        return a[i] < b[j];
        });

本题运行代码:

#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;


int main() {
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<string> vt;
    while (n--) {
        string tmp; cin >> tmp;
        vt.emplace_back(tmp);
    }
    sort(vt.begin(), vt.end(), [](string a, string b)->bool {
        return a.compare(b) < 0;
        });
    for (auto item : vt) {
        cout << item << " ";
    }
}
// 64 位输出请用 printf("%lld")

9.字符串排序(2)

#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;


int main() {
    ios::sync_with_stdio(false);
    string line;
    while (getline(cin, line)) {
        stringstream ss;
        ss << line;
        string tmp;
        vector<string> vt;
        while (ss >> tmp) {
            vt.emplace_back(tmp);
        }
        sort(vt.begin(), vt.end(), [](string a, string b)->bool {
            return a.compare(b) < 0;
            });
        for (auto& item : vt) {
            cout << item << " ";
        }cout << endl;
    }
}
// 64 位输出请用 printf("%lld")

字符串排序(3)

对输入的字符串进行排序后输出

打开以下链接可以查看正确的代码

https:``//ac.nowcoder.com/acm/contest/5657#question

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 256M,其他语言512M

输入描述:

多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100

输出描述:

对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格

示例1

输入例子:

a,c,bb
f,dddd
nowcoder

输出例子:

a,bb,c
dddd,f
nowcoder

难点:分割符不是常使用的 空格 、tab等如何操作

getline函数可以指定分隔符,默认是以\n为分割符的。

#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;


int main() {
    ios::sync_with_stdio(false);
    string line;
    while (getline(cin, line)) {

        stringstream ss;
        ss << line;
        vector<string> vt;
        string tmp;
        while (getline(ss, tmp, ',')) {
            vt.emplace_back(tmp);
        }
        sort(vt.begin(), vt.end(), [](string a, string b) ->bool {
            return a.compare(b) < 0;
            });
        string show;
        for (auto& item : vt) {
            show.append(item); show.push_back(',');
        }show.pop_back();
        cout <<show<< endl;
    }
}
// 64 位输出请用 printf("%lld")

11.自测本地通过提交为0

#include <vector>
#include <unordered_set>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <unordered_map>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <numeric>
using namespace std;


int main() {
    ios::sync_with_stdio(false);
    long long a, b;
    while (cin >> a >> b) {
        cout << a + b << endl;
    }
}
// 64 位输出请用 printf("%lld")