Nicole Zhang

East China Normal University | Software Engineering | 2000.5.30

Java input&ouput

java acm模式输入输出

之前刷leetcode都是核心代码模式,最近在准备面试发现不光要给出实现功能的函数,也要自己构造测试用例输入输出,稍微总结一下用法

输入格式

两种输入格式,在读入数据量大的情况下,格式1的速度会快些

import java.io.BufferedReader;
import java.io.InputStreamReader;

Scanner sc = new Scanner (new BufferedInputStream(System.in));
import java.util.Scanner;
Scanner sc = new Scanner (System.in);

下面开始读取

int n = sc.nextInt(); 
String s = sc.next(); //相当于 cin >> s;
double t = sc.nextDouble();
String s = sc.nextLine(); //相当于cin.getline(...);

判断是否有下一个输入

sc.hasNext()
sc.hasNextInt()
sc.hasNextDouble()
sc.hasNextLine()

最后记得关闭输出流

sc.close();

输出格式

System.out.print(); 
System.out.println();
System.out.format();
System.out.printf();

读入多个整数

Input 输入数据有多组,每组占一行,由一个整数组成

Sample Input
56
67
100
123

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
while(sc.hasNext()){ //判断是否结束
int score = sc.nextInt();//读入整数
}
}
}

输入多组整数

输入数据有多组,每组占2行,第一行为一个整数N,指示第二行包含N个实数

Sample Input
4
56.9 67.7 90.5 12.8
5
56.9 67.7 90.5 12.8

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
for(int i=0;i<n;i++){
double a = sc.nextDouble();
。。。。。。
}
}
}
}

输入字符串

输入数据有多行,第一行是一个整数n,表示测试实例的个数,后面跟着n行,每行包括一个由字母和数字组成的字符串。

Sample Input
2
asdfasdf1
asdf1111111


import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=0;i<n;i++){
String str = sc.next();
......
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
for(int i=0;i<n;i++){
String str = sc.nextLine();
......
}
}
}

输入链表

链表的数据结构

class ListNode {
int val;
ListNode next;

public ListNode(int val) {
this.val = val;
}
}

将要调用的初始链表函数如下

public static ListNode InitListNode(int[] ints) {
ListNode head = new ListNode(0);
ListNode p = head;
for (int i = 0; i < ints.length; i++) {
p.next = new ListNode(ints[i]);
p = p.next;
}
return head.next;
}

输入以逗号分隔的一串数字,比如1,2,3,4,5

Scanner scanner = new Scanner(System.in);
String str = scanner.next().toString();
String[] arr = str.split(",");

int[] ints = new int[arr.length];
for (int j = 0; j < ints.length; j++) {
ints[j] = Integer.parseInt(arr[j]);
}

//初始链表
ListNode head = InitListNode(ints);

scanner.close();

输出,也是以字符串隔开的数字

while (myhead != null) {
if (myhead.next == null) {
System.out.print(myhead.val);
} else {
System.out.print(myhead.val + ",");
}
myhead = myhead.next;
}

下面用反转链表举个例子,怎么将leetode中我们写好的核心代码融入输入输出中

注意如果main里面要调用该类的方法和类,要用static修饰,否则会报错

import java.util.Scanner;

class ListNode {
int val;
ListNode next;

public ListNode(int val) {
this.val = val;
}
}

public class ReverseList {

public static ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}

public static ListNode InitListNode(int[] ints) {
ListNode head = new ListNode(0);
ListNode p = head;
for (int i = 0; i < ints.length; i++) {
p.next = new ListNode(ints[i]);
p = p.next;
}
return head.next;
}

public static void main(String[] args) {
// 1,2,3,4,5
Scanner scanner = new Scanner(System.in);
String str = scanner.next().toString();
String[] arr = str.split(",");

int[] ints = new int[arr.length];
for (int j = 0; j < ints.length; j++) {
ints[j] = Integer.parseInt(arr[j]);
}

ListNode head = InitListNode(ints);

scanner.close();

ListNode myhead = reverseList(head);

// 输出
// 1,5,2,4,3
while (myhead != null) {
if (myhead.next == null) {
System.out.print(myhead.val);
} else {
System.out.print(myhead.val + ",");
}
myhead = myhead.next;
}
}
}

输入二叉树

二叉树的数据结构

class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}