文章目錄
- 1、前置準備
- 2、普通輸入輸出API
- ①、輸入API
- ②、輸出API
- 3、快速輸入輸出API
- ①、BufferedReader
- ②、BufferedWriter
- 案例
- 題目描述
- 代碼
面試有時候要acm模式,刷慣leetcode可能會手生不會acm模式,該文直接通過幾個題來熟悉java的輸入輸出模板,多寫幾遍,熟悉即可。
1、前置準備
首先我們需要熟悉在沒有idea的情況下如何手寫并編譯運行一個.java文件。
class Main{public static void main(String[] args) {System.out.println("hello java");}
}
每個.java文件必須有一個類我們一般命名為Main,文件名與代碼中的類名必須一樣。
命令行輸入命令:
javac Main.java
生成Main.class的class文件,隨后輸入:
java Main
即可運行程序
2、普通輸入輸出API
import java.util.*;
import java.io.*;
不管如何,最開頭這兩個頭文件都這樣引入即可,這兩個基本都會用到。
①、輸入API
獲取不同類型數據要使用nextXxx()的api。
示例代碼:
public static void main(String[] args) {Scanner sc = new Scanner(System.in); //用于從控制臺讀入數據//獲取int類型數據Integer a = sc.nextInt();//獲取Double類型數據Double d = sc.nextDouble();//獲取long類型數據long l = sc.nextLong();//獲取short類型數據short b = sc.nextShort();System.out.print(a + " " + d + " " + l + " " + b + " ");}
運行結果:
獲取字符串需要用到兩個api:
①、in.next() 從緩沖區接收字符遇到空格后停止。 相當于 cin 和 scanf
②、in.nextLine() 從緩沖區接收字符,并且接收空格,遇到換行才停止,并且會自動舍棄換行。 相當于 gets()
public static void main(String[] args) {Scanner sc = new Scanner(System.in); //用于從控制臺讀入數據String x = sc.next();String s = sc.nextLine();System.out.println("x: " + x);System.out.println("s: " + s);}
運行結果:
可以看到next()遇到空格就會暫停,而nextLine()遇到換行就會暫停。
②、輸出API
輸出有三種形式;
System.out.print(); // 最后打印結果不會加換行
System.out.println(); // 最后打印結果加換行
System.out.printf(); // 類似c語言中的printf。
直接代碼中來看:
①、
public static void main(String[] args) {for(int i = 0; i < 3; i ++ ) {System.out.print(i + " ");}}
運行結果:
0 1 2
②、
public static void main(String[] args) {for(int i = 0; i < 3; i ++ ) {System.out.println(i + " ");}}
運行結果:
0
1
2
③、
public static void main(String[] args) {for(int i = 0; i < 3; i ++ ) {System.out.printf("%d ", i);}}
運行結果:
0 1 2
3、快速輸入輸出API
①、BufferedReader
public static void main(String[] args) throws Exception{BufferedReader in = new BufferedReader(new InputStreamReader(System.in));String s = in.readLine();String[] arr = in.readLine().split(" ");System.out.println("s: " + s);System.out.print("arr: ");for (String str : arr) {System.out.print(str + " ");}}
運行結果:
補充:in.read()方法返回值為Integer,用于讀取整數。
②、BufferedWriter
當數據量大的時候一定要使用這個類進行輸出,謹記!
public static void main(String[] args) throws Exception{BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));int a = 68;char b = '2';String c = "3";bw.write(a);bw.write("\n");bw.write(b);bw.write("\n");bw.write(c);bw.write("\n");bw.flush();}
運行結果:
D
2
3
在輸出整型要注意,會輸出對應ASCII碼值的字符,比如ASCII碼值為68的字符是D。
解決方式:
bw.write(a + "");
bw.write(Integer.toString(a));
盡量將其轉為字符串進行輸出。
輸出結果:
68
案例
通過一道算法題,來練習熟悉一下api。
題目描述
給定你一個長度為 n的整數數列。
請你使用快速排序對這個數列按照從小到大進行排序。
并將排好序的數列按順序輸出。
輸入格式
輸入共兩行,第一行包含整數 n。
第二行包含 n個整數(所有整數均在 1~109范圍內),表示整個數列。
輸出格式
輸出共一行,包含 n個整數,表示排好序的數列。
數據范圍
1≤n≤100000
輸入樣例:
5
3 1 2 4 5
輸出樣例:
1 2 3 4 5
代碼
這里可以將new Inputstream()用new BufferedInputStream()來替代,讀取速度更快。
import java.util.*;
import java.io.*;class Main{public static void main(String[] args) throws Exception {Scanner sc = new Scanner(new BufferedInputStream(System.in));Integer n = sc.nextInt();Integer[] nums = new Integer[n + 1];for(int i = 0; i < n; i ++ ) {Integer x = sc.nextInt();nums[i] = x;}quick_sort(nums, 0, n - 1);for(int i = 0; i < n; i ++ ) {System.out.print(nums[i] + " ");}}public static void quick_sort(Integer[] nums, int l, int r) {if(l >= r) return;int mid = nums[l + r >>> 1];int i = l - 1, j = r + 1;while(i < j) {do i ++; while(nums[i] < mid);do j --; while(nums[j] > mid);if(i < j) {Integer tem = nums[i];nums[i] = nums[j];nums[j] = tem;}}quick_sort(nums, l, j); quick_sort(nums, j + 1, r);}}