输入输出模板(Java版)|ACM模式

2,480 阅读1分钟

引言

Java刷题的输入输出模版,目前大多数笔试都是ACM模式,如果习惯了力扣的核心代码模式,需要笔试前多加练习! Java主要掌握scan.hasNext()scan.hasNextInt()scan.nextInt()scan.hasNextLine()scan.nextLine()的使用。

OJ在线编程常见输入输出练习场

计算a+b

1.

输入: 包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。

输出: a+b的结果

import java.util.*;

public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        while (scan.hasNext())
        {
            int a = scan.nextInt();
            int b = scan.nextInt();
            System.out.println(a + b);
        }
    }
}

2.

输入:
第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)

输出: a+b的结果

import java.util.*;

public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        int T = scan.nextInt();
        while (T -- > 0)
        {
            int a = scan.nextInt();
            int b = scan.nextInt();
            System.out.println(a + b);
        }

    }
}

3.

输入: 包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入

输出: a+b的结果

import java.util.*;

public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        while (scan.hasNextInt())
        {
            int a = scan.nextInt();
            int b = scan.nextInt();
            if (a == 0 && b == 0)
                break;
            System.out.println(a + b);
        }

    }
}

4.大数求和

输入: 输入数据包括多组。
每组数据一行,包含两个字符串形式的非负整数

输出: 对于每组测试数据,计算它们的和,输出字符串。

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) { 
            String [] words = scan.nextLine().split(" "); 
            String s1 = words[0];
            String s2 = words[1];
            System.out.println(bigNumberAdd(s1,s2)); 
        }  
    }

    public static String bigNumberAdd(String s1, String s2) {
        char[] a1 = s1.toCharArray();
        char[] a2 = s2.toCharArray();
        int len1 = a1.length;
        int len2 = a2.length;
        int i = len1 - 1, j = len2 - 1;
        int m = 0; //当前位上要相加的数
        int n = 0; //余数
        int num1 = 0, num2 = 0;
        StringBuffer s = new StringBuffer();
        while (i >= 0 || j >= 0) {
                if (i >= 0) {
                        num1 = a1[i] - '0';
                } else {
                        num1 = 0;
                }
                if (j >= 0) {
                        num2 = a2[j] - '0';
                } else {
                        num2 = 0;
                }

                int sum = num1 + num2 + n;
                m = sum % 10;
                n = sum / 10;
                char c = (char)(m + '0');
                s.append(c);
                i--;
                j--;
        }
        if (n != 0) {
                s.append('1');
        }

        s.reverse();
        return s.toString();
    }
}

利用库函数:

import java.util.Scanner;
import java.math.BigInteger;
public class Main {	
    public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    while (scan.hasNextLine()) { 
        String [] words = scan.nextLine().split(" "); 
        String s1 = words[0];
        String s2 = words[1];
        BigInteger a = new BigInteger(s1);
        BigInteger b = new BigInteger(s2);
        a = a.add(b);
        System.out.println(a); 
    }
    }
}

一行多个数字求和

1.

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

输出: 每组数据输出求和的结果

import java.util.*;

public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        int n;
        while (scan.hasNextInt())
        {
            n = scan.nextInt();
            if (n == 0)
                break;

            int cur_sum = 0;
            while (n -- > 0)
            {
                cur_sum += scan.nextInt();
            }

            System.out.println(cur_sum);
        }        

    }
}

2.

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

输出: 每组数据输出求和的结果

import java.util.*;

public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        int T = scan.nextInt();
        while (T -- > 0)
        {
            int n = scan.nextInt();
            int cur_sum = 0;
            int x;
            while (n -- > 0)
            {
                x = scan.nextInt();
                cur_sum += x;
            }
            System.out.println(cur_sum);
        }

    }
}

3.

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

输出:每组数据输出求和的结果

import java.util.*;

public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        int n;
        while (scan.hasNextInt())
        {
            n = scan.nextInt();
            int cur_sum = 0;
            int x;
            while (n -- > 0)
            {
                x = scan.nextInt();
                cur_sum += x;
            }
            System.out.println(cur_sum);
        }

    }
}

4.

输入:
输入数据有多组, 每行表示一组输入数据。
每行不定有n个整数,空格隔开。(1 <= n <= 100)。\

输出: 每组数据输出求和的结果

import java.util.* ;

public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);

        while (scan.hasNextLine())
        {
            String [] nums = scan.nextLine().split(" ");

            int cur_sum = 0;
            for (String x : nums)
                cur_sum += Integer.parseInt(x);

            System.out.println(cur_sum);
        }

    }
}

字符串排序

1.

输入:
输入有两行,第一行n
第二行是n个空格隔开的字符串

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

import java.util.* ;

public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        int n = Integer.parseInt(scan.nextLine());
        String [] words = scan.nextLine().split(" ");

        Arrays.sort(words);
        for (int i = 0; i < n; i ++)
            System.out.print(words[i] + " ");
        System.out.println();

    }
}

2.

输入:
多个测试用例,每个测试用例一行。
每行通过空格隔开,有n个字符串,n<100

输出:
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开

import java.util.* ;

public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        while (scan.hasNextLine())
        {
            String [] words = scan.nextLine().split(" ");
            Arrays.sort(words);
            for (String word : words)
                System.out.print(word + " ");
            System.out.println();
        }
    }

}

3.

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

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

import java.util.*;
public class Main
{
    public static void main(String [] args)
    {
        Scanner scan = new Scanner(System.in);

        while (scan.hasNextLine())
        {
            String[] words = scan.nextLine().split(",");
            Arrays.sort(words, (w1, w2) -> w1.compareTo(w2));
            int wn = words.length;
            for (int i = 0; i < wn - 1; i ++)
            {
                System.out.print(words[i] + ',');
            }
            System.out.println(words[wn-1]);
        }

    }
}