java牛客网输入输出

本文介绍了在牛客网笔试中如何处理不同类型的输入输出问题,包括单组、多组数据读取,整数数组、字符串数组的转换,以及二维数组的读取。还讨论了`nextInt()`和`nextLine()`的使用注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

牛客网笔试输入输出总结:
https://blog.youkuaiyun.com/weixin_43431182/article/details/108423023?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control

1.对于那种只需要写一个方法的题目,只要注意方法中参数的类型和返回值类型即可

2.对于需要自己考虑输入和输出的题目,自己主要遇到过以下几种情况,并自己尝试了进行读取:

import java.util.*;
public class Main{
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		while(in.nextInt()){
		//读入一组数据
		//处理
		//输出
		}
	}
 }

while用来读入多组(一组一般为一行)数据,要读多组(行)时套上while,只用读单组则不套。

  1. 读入确定个数的一组输入(用nextInt(),读入int变量。)
    使用nextInt(),一行有几个数就用几次
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        while(in.hasNext()){
            int a=in.nextInt();
            int b=in.nextInt();
            System.out.println(a+b);
        }
    }
}
  1. 读入未知个数的一组输入(用nextLine(),读入一维数组。)
    使用nextLine(),存入String[ ], 再用int = Integer.parseInt( string )把String[ ]中每一个string转化为int
public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            //读入字符串数组
            String[] temp = sc.nextLine().split(" ");
            //字符串数组转整数数组
            int[] arr = new int[temp.length];
            for (int i = 0; i < arr.length; i++){
                arr[i] = Integer.parseInt(temp[i]);
            }
            //打印整数数组
            System.out.println(Arrays.toString(arr));
            //打印数组中的每一个元素,用逗号隔开
            for (int i: arr) {
                System.out.print(i + ",");
            }
            //下标法打印数组中的每一个元素,用逗号隔开
            for (int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + ", ");
            }
        }
    }
  1. 读入一个数据的行数N,再读入这N行数据(双层循环。用nextLine()读入每一行。读入二维数组)。我们知道每行几个数。
import java.util.Scanner;

//不套while处理一组数据:
public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int c = Integer.parseInt(sc.nextLine()); //读入N
        String[][] steps = new String[c][3]; //假设每行3个数
        for (int i = 0; i < c; i++){
            String[] temp = sc.nextLine().split(" ");
            for (int j = 0; j < 3; j++){
                steps[i][j] = temp[j];
            }
        }

    }
}

//套上while处理多组数据:
public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int c = Integer.parseInt(sc.nextLine());
            String[][] steps = new String[c][3];
            for (int i = 0; i < c; i++) {
                String[] temp = sc.nextLine().split(" ");
                for (int j = 0; j < 3; j++) {
                    steps[i][j] = temp[j];
                }
            }
            //打印二维数组
            for (int i = 0; i < steps.length; ++i){
                for (int j = 0; j < steps[0].length; ++j) {
                    System.out.print(steps[i][j] + " ");

                }
                System.out.println();
            }

        }
    }
}
//输入输出样例
input1:
1
12 3 45
output1:
12 3 45 
input2:
2
3 2 1
1 2 3
output2:
3 2 1 
1 2 3 

Integer.parseInt(s)与Integer.valueOf(s)的区别详解:
https://blog.youkuaiyun.com/u010502101/article/details/79162587

牛客网输入输出练习场:
https://ac.nowcoder.com/acm/contest/5647

Note:
排序List<>: Collections.sort(list)
排序array:Arrays.sort(arr)

因为sc.nextInt()方法只读取空白符前面的值,会把空白符继续留在缓存区,而sc.nextLine()会把空白符也读取并清除,所以每次用完sc.nextInt()方法最好在后面加个sc.nextLine(),但最好舍弃这个方法,每行都采用sc.nextLine()方法读取。【在这里两个方法混用,提交代码的时候很容易经常出现数组越界的问题】

### 关于牛客网 Java 输入输出训练 在 Java 中,`BufferedReader` 是一种高效的字符流读取工具,能够通过缓冲机制提高性能。下面是一个基于 `BufferedReader` 的典型输入输出示例代码及其解析: #### 示例代码 以下是实现字符串单词倒置功能的一个完整代码示例[^2]。 ```java import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { // 创建 BufferedReader 对象用于从标准输入读取数据 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 读取一行输入 String inputLine = br.readLine(); // 调用方法处理并打印结果 System.out.println(reverseWords(inputLine)); } private static String reverseWords(String sentence) { if (sentence == null || sentence.isEmpty()) { return ""; } StringBuilder result = new StringBuilder(); String[] words = sentence.split(" "); int length = words.length; for (int i = length - 1; i >= 0; i--) { result.append(words[i]); if (i != 0) { result.append(" "); } } return result.toString(); } } ``` --- #### 解析 上述代码实现了将输入的一行字符串中的单词顺序反转的功能,具体逻辑如下: - **创建 BufferedReader 实例**: 使用 `new BufferedReader(new InputStreamReader(System.in))` 将字节流转换为带缓冲的字符流[^1]。 - **读取用户输入**: 利用 `br.readLine()` 方法获取用户输入的一整行字符串。 - **拆分字符串**: 使用 `split(" ")` 方法按空格分割字符串成数组形式。 - **逆序拼接**: 遍历分割后的数组,按照反向顺序重新组合字符串,并保留原始标点位置不变。 --- #### 牛客网上的常见练习题型 牛客网上针对 Java 输入输出设计了许多经典题目,常见的有以下几类: 1. 单词倒置(如本例所示)。 2. 数组元素操作(例如求最大值、最小值等)。 3. 多行输入处理(逐行读取直到结束标志)。 4. 文件读写操作(结合 `FileReader` 和 `FileWriter` 进行文件内容修改)。 这些题目通常会考察以下几个方面的能力: - 掌握基本 IO 流的操作方式。 - 理解如何高效利用缓冲技术提升程序运行效率。 - 学习正则表达式的应用技巧来完成复杂的数据提取需求。 --- ### 注意事项 当使用 `BufferedReader` 或其他类似的 IO 类时需要注意异常捕获以及资源关闭等问题。虽然现代 JVM 提供了自动管理部分资源的方法(如 try-with-resources),但在实际开发过程中仍需养成良好的编码习惯以避免潜在隐患。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值