Java 的 ACM 输入输出格式

ACM 格式

  1. 类名称必须采用 public class Main 方式命名

  2. 多组输入,读取到文件尾

Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
	// 读整数
	int n = scan.nextInt();
	// 读小数
	double d = scan.nextDouble();
	// 读字符串
	String s = scan.next();
	// 读一整行
	String s = scan.netxLine();
	// 读大数
	BigInteger bi = scan.nextBigInteger();
} 
// 或者 scan.hasNextInt()
// 或者 scan.hasNextDouble()
// 或者 scan.hasNextLine()
  1. 当数据较多时,可以采用更快的方式
import java.io.BufferedInputStream;
Scanner scan  = new Scanner(new BufferedInputStream(System.in));
  1. Java 运算大数
import java.math.BigInteger;//操作大整数
import java.math.BigDecimal;//操作大小数
import java.io.BufferedInputStream;
import java.util.Scanner;
import java.util.Arrays;
public class Main
{
    public static void main(String []args)
    {    
        BigInteger a,b,ans;
        Scanner scan=new Scanner(System.in);
        a=scan.nextBigInteger();
        b=scan.nextBigInteger();
        ans=a.add(b);//ans=a+b;
        ans=a.subtract(b);//ans=a-b
        ans=a.mod(b);//ans=a%b
        ans=a.divide(b);//ans=a/b
        ans=a.max(b);
        ans=a.min(b);    
        ans=a.multiply(b);
    }
}

next()nextLine() 的区别:

  1. next() 一定要读取到有效字符后才可以结束输入,对输入有效字符之前遇到的空格键、Tab 键或 Enter 键等结束符,next() 方法会自动将其去掉,只有在输入有效字符之后,next() 方法才将其后输入的空格键、Tab 键或 Enter 键等视为分隔符或结束符
  2. nextLine() 方法的结束符只是 Enter 键。

总结,next() 方法不能得到带空格的字符串,而 nextLine() 方法返回的是Enter键之前的所有字符

示例——计算 a+b

来自牛客网 : 输入输出练习

输入格式 1

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

  • 输出描述:
    输出 a+b 的结果

  • 输入例子1:

    1 5
    10 20
    
  • 输出例子1:

    6
    30
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) {
            String str = scan.nextLine();
            String[] arr = str.split(" ");
            int a = Integer.parseInt(arr[0]);
            int b = Integer.parseInt(arr[1]);
            System.out.println(a + b);
        }
    }
}

输入格式2

  • 输入描述:

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

  • 输出描述:

    输出 a+b 的结果

  • 输入例子1:

    2
    1 5
    10 20
    
  • 输出例子1:

    6
    30
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s1 = scan.nextLine().trim();
        int t = Integer.parseInt(s1);
        while (scan.hasNextLine()) {
            String sTemp = scan.nextLine().trim();
            String[] arr = sTemp.split(" ");
            int a = Integer.parseInt(arr[0]);
            int b = Integer.parseInt(arr[1]);
            int sum = a + b;
            System.out.println(sum);
        }
    }
}

输入格式3

  • 输入描述:

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

  • 输出描述:

    输出 a+b 的结果

  • 输入例子1:

    1 5
    10 20
    0 0
    
  • 输出例子1:

    6
    30
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) {
            String str = scan.nextLine();
            String[] arr = str.split(" ");
            int a = Integer.parseInt(arr[0]);
            int b = Integer.parseInt(arr[1]);
            if (a == 0 && b == 0) {
                break;
            } else {
                System.out.println(a + b);
            }
        }
    }
}

示例——计算一系列数的和

来自牛客网 : 输入输出练习

输入格式1

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

  • 输出描述:

    每组数据输出求和的结果

  • 输入例子1:

    4 1 2 3 4
    5 1 2 3 4 5
    0
    
  • 输出例子1:

    10
    15
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new  Scanner(System.in);
        while (scan.hasNextLine()) {
            String[] arr = scan.nextLine().trim().split(" ");
            int n = Integer.parseInt(arr[0]);
            if (n == 0) {
                break;
            }
            int sum = 0;
            for (int i = 1; i <= n; i++) {
                sum += Integer.parseInt(arr[i]);
            }
            System.out.println(sum);
        }
    }
}

输入格式2

  • 输入描述:
    输入的第一行包括一个正整数 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
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String s1 = scan.nextLine().trim();
        int t = Integer.parseInt(s1);
        while (scan.hasNextLine()) {
            String[] arr = scan.nextLine().trim().split(" ");
            int n = Integer.parseInt(arr[0]);
            int sum = 0;
            for (int i = 1; i <= n; i++) {
                sum += Integer.parseInt(arr[i]);
            }
            System.out.println(sum);
        }
    }
}

输入格式3

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

  • 输出描述:

    每组数据输出求和的结果

  • 输入例子1:

    4 1 2 3 4
    5 1 2 3 4 5
    
  • 输出例子1:

    10
    15
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) {
            String[] arr = scan.nextLine().trim().split(" ");
            int n = Integer.parseInt(arr[0]);
            int sum = 0;
            for (int i = 1; i <= n; i++) {
                sum += Integer.parseInt(arr[i]);
            }
            System.out.println(sum);
        }
    }
}

输入格式4

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

  • 输出描述:

    每组数据输出求和的结果

  • 输入例子1:

    1 2 3
    4 5
    0 0 0 0 0 
    
  • 输出例子1:

    6
    9
    0
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) {
            String[] arr = scan.nextLine().trim().split(" ");
            int n = arr.length;
            int sum = 0;
            for (int i = 0; i < n; i++) {
                sum += Integer.parseInt(arr[i]);
            }
            System.out.println(sum);
        }
    }
}

示例——字符串排序

来自牛客网 : 输入输出练习

输入格式1

  • 输入描述:

    输入有两行,第一行 n

    第二行是 n 个空格隔开的字符串

  • 输出描述:

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

  • 输入例子1:

    5
    c d a bb e
    
  • 输出例子1:

    a bb c d e
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
// 很奇怪,不能使用下面两行替换第一个输入 for 循环
//        String[] strs = scan.nextLine().trim().split(" ");
//      Arrays.sort(strs);
        String[] strs = new String[n];
        for(int i = 0; i < n; i++){
            strs[i] = scan.next();
        }
        Arrays.sort(strs);
        for (int i = 0; i < n - 1; i++) {
            System.out.print(strs[i]);
            System.out.print(' ');
        }
        System.out.print(strs[n - 1]);
    }
}

输入格式2

  • 输入描述:

    多个测试用例,每个测试用例一行。

    每行通过空格隔开,有 n 个字符,n<100

  • 输出描述:

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

  • 输入例子1:

    a c bb
    f dddd
    nowcoder
    
  • 输出例子1:

    a bb c
    dddd f
    nowcoder
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) {
            String[] strs = scan.nextLine().trim().split(" ");
            Arrays.sort(strs);
            for (int i = 0; i < strs.length - 1; i++) {
                System.out.print(strs[i]);
                System.out.print(' ');
            }
            System.out.println(strs[strs.length - 1]);
        }
    }
}

输入格式3

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

  • 输出描述:

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

  • 输入例子1:

    a,c,bb
    f,dddd
    nowcoder
    
  • 输出例子1:

    a,bb,c
    dddd,f
    nowcoder
    

示例代码:

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNextLine()) {
            String[] strs = scan.nextLine().trim().split(",");
            Arrays.sort(strs);
            for (int i = 0; i < strs.length - 1; i++) {
                System.out.print(strs[i]);
                System.out.print(',');
            }
            System.out.println(strs[strs.length - 1]);
        }
    }
}
  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值