【ACM模式】牛客网ACM机试模式Python&Java&C++主流语言OJ输入输出案例代码总结

前言

前天做最后一批华为软件机试时,因为完全不清楚这种所谓的ACM模式,刚看到的时候就傻眼了,即使已经看到了这种区别,但是也没有充分的提前准备好应对,因此光输入输出都卡了好久,最后也有自己的原因基本等于交了白卷。

看网上评论说大家都会有这个问题,因此也算是正常。自己也要从哪里跌倒就从哪里爬起来,把这个难点给克服了,不然以后遇到同样的机试还是干瞪眼。

学习的过程顺便留下记录,以便日后翻阅。

题目链接:OJ在线编程常见输入输出练习场

提交记录 :ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛

分别采用Python3.9和Java1.8来解决,前者因为确实代码简单易懂,后者只是因为近期刷题都是Java,刷完剑指offer后应对机试也考虑主要用python,能免掉很多问题。

Python代码部分参考了B站up主以鹅爱折腾牛客网笔试不会数据导入??看这个视频就够了(1) ,感兴趣的同学也可以去学习。

目录

  • 前言
  • a+b的和
  • A. 多组空格分隔的两个正整数
  • 输入描述:
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • C++
  • B.第一行组数接空格分隔的两个正整数
  • **输入描述:**
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • C++
  • C.空格分隔的两个正整数为0 0 结束
  • **输入描述:**
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • C++
  • D.每行第一个为个数后带空格分割整数为0结束
  • **输入描述:**
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • C++
  • E.第一行组数接第一个个数接空格分开的整数
  • ** 输入描述:**
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • C++
  • F.每行第一个为个数后带空格分割整数
  • **输入描述:**
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • C++
  • G.多组空格分隔的正整数
  • **输入描述:**
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • C++
  • 字符串排序
  • H.第一行个数第二行字符串
  • **输入描述:**
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • print\println的区别 :
  • C++
  • I.多行空格分开的字符串
  • **输入描述:**
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • C++
  • J.多行逗号分开的字符串
  • **输入描述:**
  • **输出描述:**
  • **输入例子1:**
  • **输出例子1:**
  • Python
  • Java
  • C++
  • K.多组空格分隔的两个正整数
  • 输入描述:
  • 输出描述:
  • 输入
  • 输出
  • Python
  • Java
  • C++
  • ACM模式
  • 总结
  • a+b的和

    A. 多组空格分隔的两个正整数

    A题提交记录

    输入描述:
    输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。
    
    输出描述:
    输出a+b的结果
    
    输入例子1:
    1 5
    10 20
    
    输出例子1:
    6
    30
    
    Python
    while True:
        try:
            num = list(map(int,input().split(" ")))
            print(sum(num)) 
        except:
            break
    
    Java
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextInt()) { // 注意 while 处理多个 case
                int a = in.nextInt();
                int b = in.nextInt();
                System.out.println(a + b);
            }
        }
    }
    
    C++
    #include <iostream>
    using namespace std;
    
    int main() {
        int a, b;
        while (cin >> a >> b) { // 注意 while 处理多个 case
            // 64 位输出请用 printf("%lld")
            cout << a + b << endl;
       }
    }
    

    B.第一行组数接空格分隔的两个正整数

    B题提交记录

    输入描述:
    输入第一行包括一个数据组数t(1 <= t <= 100)
    接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)
    
    输出描述:
    输出a+b的结果
    
    输入例子1:
    2
    1 5
    10 20
    
    输出例子1:
    6
    30
    
    Python
    t = int(input())
    for i in range(t):
        num = list(map(int,input().split(" ")))
        print(sum(num))
    
    Java
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int num = in.nextInt();
            for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
                int a = in.nextInt();
                int b = in.nextInt();
                System.out.println(a + b);
            }
        }
    }
    
    C++
    #include<iostream>
    using namespace std;
    
    int main() {
        int a, b, c;
        cin >> a;
        while(cin >> b >> c) {
            cout << b + c << endl;
        }
    }
    

    C.空格分隔的两个正整数为0 0 结束

    C题提交记录

    输入描述:
    输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
    
    输出描述:
    输出a+b的结果
    
    输入例子1:
    1 5
    10 20
    0 0
    
    输出例子1:
    6
    30
    
    Python
    while True:
        try:
            num = list(map(int,input().split(" ")))
            if num[0] == num[1] == 0:
                break
            print(sum(num)) 
        except:
            break
    
    Java
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            
            while (in.hasNextInt()) { // 注意 while 处理多个 case
                int a = in.nextInt();
                int b = in.nextInt();
                if(a ==0 && b == 0) break;
                System.out.println(a + b);
            }
        }
    }
    
    C++
    #include<iostream>
    using namespace std;
    
    int main() {
        int a, b;
        while(cin >> a >> b) {
            if (a == 0 && b == 0) break;
            else cout << a + b << endl;
        }
    }
    

    D.每行第一个为个数后带空格分割整数为0结束

    D题提交记录

    输入描述:
    输入数据包括多组。
    每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
    接下来n个正整数,即需要求和的每个正整数。
    
    输出描述:
    每组数据输出求和的结果
    
    输入例子1:
    4 1 2 3 4
    5 1 2 3 4 5
    0
    
    输出例子1:
    10
    15
    
    Python
    while True:
        try:
            num = list(map(int,input().split(" ")))
            if num[0] == 0:
                break
            print(sum(num[1: ])) 
        except:
            break
    
    Java
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            
            while (in.hasNextInt()) { // 注意 while 处理多个 case
                int n = in.nextInt();
                if(n == 0) break;
                int sum = 0;
                for (int i = 0; i < n; i++) {
                    sum += in.nextInt();
                }
                System.out.println(sum);
            }
        }
    }
    
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            
            while (in.hasNextInt()) { // 注意 while 处理多个 case
                int n = in.nextInt();
                if(n == 0) break;
                int sum = 0;
                while (n > 0) {
                    sum += in.nextInt();
                    n--;
                }
                System.out.println(sum);
            }
        }
    }
    
    C++
    #include<iostream>
    using namespace std;
    
    int main() {
        int n;
        while(cin >> n) {
            int sum = 0;
            int temp;
            if (n == 0) break;
            for(int i = 0; i < n; i++) {
                cin >> temp;
                sum += temp;
            }
            cout << sum << endl;
        }
    }
    

    E.第一行组数接第一个个数接空格分开的整数

    E题提交记录

    ** 输入描述:**
    输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
    接下来t行, 每行一组数据。
    每行的第一个整数为整数的个数n(1 <= n <= 100)。
    接下来n个正整数, 即需要求和的每个正整数。
    
    输出描述:
    每组数据输出求和的结果
    
    输入例子1:
    2
    4 1 2 3 4
    5 1 2 3 4 5
    
    输出例子1:
    10
    15
    
    Python
    t = int(input())
    for i in range(t):
        num = list(map(int,input().split(" ")))
        print(sum(num[1:]))
    
    Java
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int num = in.nextInt();
            for(int i = 0; i < num; i++) { // 注意 while 处理多个 case
                int n = in.nextInt();
                if(n == 0) break;
                int sum = 0;
                while (n > 0) {
                    sum += in.nextInt();
                    n--;
                }
                System.out.println(sum);
            }
        }
    }
    
    C++
    #include<iostream>
    using namespace std;
    
    int main() {
        int num;
        cin >> num;
        
        while(num--) {
            int n;
            cin >> n;
            int sum = 0;
            int temp;
            for(int i = 0; i < n; i++) {
                cin >> temp;
                sum += temp;
            }
            cout << sum << endl;
        }
        return 0;
    }
    

    F.每行第一个为个数后带空格分割整数

    F题提交记录

    输入描述:
    输入数据有多组, 每行表示一组输入数据。
    每行的第一个整数为整数的个数n(1 <= n <= 100)。
    接下来n个正整数, 即需要求和的每个正整数。
    
    输出描述:
    每组数据输出求和的结果
    
    输入例子1:
    4 1 2 3 4
    5 1 2 3 4 5
    
    输出例子1:
    10
    15
    
    Python
    while True:
        try:
            num = list(map(int,input().split(" ")))
            print(sum(num[1:])) 
        except:
            break
    
    Java
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextInt()) { // 注意 while 处理多个 case
                int n = in.nextInt();
                int sum = 0;
                while (n > 0) {
                    sum += in.nextInt();
                    n--;
                }
                System.out.println(sum);
            }
        }
    }
    
    C++
    #include<iostream>
    using namespace std;
    
    int main() {
        int n;
        while(cin >> n) {
            int sum = 0;
            int temp;
            for(int i = 0; i < n; i++) {
                cin >> temp;
                sum += temp;
            }
            cout << sum << endl;
        }
    }
    

    G.多组空格分隔的正整数

    G题提交记录

    输入描述:
    输入数据有多组, 每行表示一组输入数据。
    
    每行不定有n个整数,空格隔开。(1 <= n <= 100)。
    
    输出描述:
    每组数据输出求和的结果
    
    输入例子1:
    1 2 3
    4 5
    0 0 0 0 0
    
    输出例子1:
    6
    9
    0
    
    Python
    while True:
        try:
            num = list(map(int,input().split(" ")))
            print(sum(num)) 
        except:
            break
    
    Java
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextLine()) { // 注意 while 处理多个 case
                String[] s = in.nextLine().split(" ");
                int sum = 0;
                for (int i = 0; i < s.length; i++) {
                    sum += Integer.parseInt(s[i]);
                }
                System.out.println(sum);
            }
        }
    }
    
    C++
    #include<iostream>
    using namespace std;
    
    int main() {
        int sum = 0;
        int temp;
        while (cin >> temp) {
            sum += temp;
            if (cin.get() == '\n') {
                cout << sum << endl;
                sum = 0;
            }
        }
    }
    

    字符串排序

    H.第一行个数第二行字符串

    H题提交记录

    输入描述:
    输入有两行,第一行n
    
    第二行是n个空格隔开的字符串
    
    输出描述:
    输出一行排序后的字符串,空格隔开,无结尾空格
    
    输入例子1:
    5
    c d a bb e
    
    输出例子1:
    a bb c d e
    
    Python
    t = int(input())
    num = list(input().split(" "))
    num.sort()
    print(" " .join(num))
    
    Java
    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            in.nextLine();
            while (in.hasNext()) { // 注意 while 处理多个 case
                String[] s = in.nextLine().split(" ");
                Arrays.sort(s);
                for (int i = 0; i < s.length; i++) {
                    System.out.println(s[i] + " ");
                }
                
            }
        }
    }
    

    执行结果:

    答案错误:您提交的程序没有通过所有的测试用例

    case通过率为 50.00%

    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            in.nextLine();
            while (in.hasNext()) { // 注意 while 处理多个 case
                String[] s = in.nextLine().split(" ");
                Arrays.sort(s);
                for (int i = 0; i < s.length; i++) {
                    System.out.print(s[i] + " ");
                }
                
            }
        }
    }
    

    执行结果:

    答案正确:恭喜!您提交的程序通过了所有的测试用例

    case通过率为 100.00%

    print\println的区别 :

    print将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后。

    println 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始。

    print意思是:打印

    而println是print+line的缩写,即:换行打印

    所以有了上面的错误

    C++
    #include<iostream>
    #include<set>
    using namespace std;
    
    int main(){
        int n;
        cin >> n;
        set<string> m;
        while(n--){
            string s;
            cin >> s;
            m.insert(s);
        }
        for(auto a:m){
            cout << a << " ";
        }
    }
    
    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        int n;
        cin>>n;
        vector<string> res(n);
        for(int i=0;i<n;i++){
            cin>>res[i];
        }
        sort(res.begin(),res.end());
        for(int i=0;i<n;i++){
            cout<<res[i]<<" ";
        }
    }
    

    I.多行空格分开的字符串

    I题提交记录

    输入描述:
    多个测试用例,每个测试用例一行。
    
    每行通过空格隔开,有n个字符,n<100
    
    输出描述:
    对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开
    
    输入例子1:
    a c bb
    f dddd
    nowcoder
    
    输出例子1:
    a bb c
    dddd f
    nowcoder
    
    Python
    while True:
        try:
            num = list(input().split(" "))
            num.sort()
            print(" " .join(num))
        except:
            break
    
    Java
    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextLine()) { // 注意 while 处理多个 case
                String[] s = in.nextLine().split(" ");
                Arrays.sort(s);
                for (String c:s) {
                    System.out.print(c + " ");
                }
                System.out.println();
            }
        }
    }
    
    C++
    #include <bits/stdc++.h>
    using namespace std;
     
    int main() {
        string s;
        vector<string> res;
        while(cin>>s){
            res.push_back(s);
            if(cin.get()=='\n'){
                sort(res.begin(),res.end());
                for(auto c:res) cout<<c<<" ";
                cout<<endl;
                res.clear();
            }
        }
    }
    

    J.多行逗号分开的字符串

    J题提交记录

    输入描述:
    多个测试用例,每个测试用例一行。
    每行通过,隔开,有n个字符,n<100
    
    输出描述:
    对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格
    
    输入例子1:
    a,c,bb
    f,dddd
    nowcoder
    
    输出例子1:
    a,bb,c
    dddd,f
    nowcoder
    
    Python
    while True:
        try:
            num = list(input().split(","))
            num.sort()
            print("," .join(num))
        except:
            break
    
    Java
    import java.util.*;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextLine()) { // 注意 while 处理多个 case
                String[] s = in.nextLine().split(",");
                Arrays.sort(s);
                int l = s.length;
                for (int i = 0; i < l - 1; i++) {
                    System.out.print(s[i] + ",");
                }
                System.out.println(s[l-1]);
            }
        }
    }
    
    C++
    #include <bits/stdc++.h>
    using namespace std;
     
    int main() {
        string s;
        vector<string> res;
        while(cin>>s) {
            stringstream ss(s);
            string temp;
            while(getline(ss,temp,',')) res.push_back(temp);
            sort(res.begin(),res.end());
            for(int i=0;i<res.size()-1;i++) cout<<res[i]<<',';
            cout<<res.back()<<endl;
            res.clear();
        }
    }
    

    K.多组空格分隔的两个正整数

    K题提交记录

    输入描述:
    输入有多组测试用例,每组空格隔开两个整数
    
    输出描述:
    对于每组数据输出一行两个整数的和
    
    输入
    1 1
    
    输出
    2
    
    Python
    while True:
        try:
            num = list(map(int,input().split(" ")))
            print(sum(num)) 
        except:
            break
    
    Java
    import java.util.Scanner;
    public class Main {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            while (in.hasNextLong()) { // 注意 while 处理多个 case
                Long a = in.nextLong();
                Long b = in.nextLong();
                System.out.println(a + b);
            }
        }
    }
    
    C++
    #include <iostream>
    using namespace std;
    
    int main() {
        int a, b;
        while (cin >> a >> b) { // 注意 while 处理多个 case
            // 64 位输出请用 printf("%lld")
            cout << a + b << endl;
       }
    }
    

    ACM模式

    在平时的刷题过程中一般会遇到题目要求写的是ACM模式代码,即自己写输入输出,下面参考了博主 一热爱摄影的 写输入输出模式的部分总结。

    1. Scanner类获取键盘输入

    Scanner 类是获取键盘输入的一个类,首先先创建 Scanner 对象

    Scanner sc = new Scanner(System.in);
    

    接下来通过Scanner 类的方法来获取输入,在调用方法之前一般可以采取has…方法判断是否有输入。

    next 和 nextLine 都是获取输入字符串的方法

    next( )方法 nextLine( )方法
    只能读取到空格之前的字符串 可以读取空格的字符串
    比如“你好 java”,只能读取“你好” 比如“你好 java”,可以读取“你好 java”
    在读取前可以使用 hasNext 与 hasNextLine 判断是否有输入的数据

    if ( sc.hasNext()) { String str1=sc.next(); }
    if ( sc.hasNextLine()) { String str2=sc.nextLine(); }
    

    此外,还可以接受整数和小数,方法如下:

    i = scan.nextInt(); // 接收整数
    f = scan.nextFloat(); // 接收小数
    

    2.Integer.parseInt和Integer.valueOf的区别

    parseInt( )://返回的是基本类型int
    valueOf( )://返回的是包装类Integer
    

    总结

    以上就是关于OJ和牛客网ACM模式输入输出的相关内容了,再接再厉,继续冲。

    欢迎讨论,共同进步。

    来源:JonnySu

    物联沃分享整理
    物联沃-IOTWORD物联网 » 【ACM模式】牛客网ACM机试模式Python&Java&C++主流语言OJ输入输出案例代码总结

    发表评论