[蓝桥杯]字符串的输入输出处理

问题 1094: 字符串的输入输出处理

题目描述

字符串的输入输出处理。

输入

第一行是一个正整数N,最大为100。之后是多行字符串(行数大于N), 每一行字符串可能含有空格,字符数不超过1000。

输出

先将输入中的前N行字符串(可能含有空格)原样输出,再将余下的字符串(不含有空格)以空格或回车分割依次按行输出。每行输出之间输出一个空行。

样例输入

2
www.dotcpp.com DOTCPP
A C M
D O T CPP

样例输出

www.dotcpp.com DOTCPP

A C M

D

O

T

CPP

方法一:

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: 76147
 * Date: 2020-01-27
 * Time: 13:58
 * Description:
 */
public class 字符串的输入输出处理 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        for (int i = 0; i < n; i++) {
            String str = sc.nextLine();
            System.out.println(str + "\n");
        }
        while (sc.hasNext()) {
            String str = sc.next();
            System.out.println(str + "\n");
        }
    }
}

方法二:

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int i = 0;
        int k = 1;
        String str[] = new String[1000];
        while (scanner.hasNext()) {
            for (; i < n + 1; i++) {
                str[i] = scanner.nextLine();
            }
            str[i] = scanner.next();
            i++;
            while (k < i) {
                System.out.println(str[k] + "\n");
                k++;
            }
        }
    }

你可能感兴趣的:([蓝桥杯]字符串的输入输出处理)