??途W在線判題系統使用幫助
熟悉目標OJ非常重要,才能保證你做對的代碼運行成功,下面看??途WOJ:
一、正確處理輸入格式:
常見的輸入格式 | 應對方法 |
---|---|
預先不輸入數據的組數 | 讀到文件結尾 |
預先輸入數據的組數 | 先讀出組數,然后循環 |
只有一組數據 | 直接讀 |
1. 沒有組數
Scanner scanner = new Scanner(System.in);
While(scanner.hasNextInt()){
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a+b);
}
2. 組數 + 循環輸入
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a+b);
}
3. 直接讀
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a+b);
二、正確處理輸入格式:
看具體要求 |
---|
不要輸出Case數 |
要輸出case數 |
每個case之后有空行 |
兩個case之間有空行 |
1. 不要輸出Case數
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(a+b);
}
2. 要輸出Case數
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(“case”+ (i+1)+ “ ”+ (a+b));
}
3. 每個case之后有空行
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
int a = scanner.nextInt();
int b = scanner.nextInt();
System.out.println(“case”+ (i+1)+ “ ”+ (a+b)+"\n");
}
4. 兩個case之間有空行
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
for(int i = 0; i < n; i++){
int a = scanner.nextInt();
int b = scanner.nextInt();
if(i > 0){
System.out.println(“\n”);
}
System.out.println(“case”+ (i+1)+ “ ”+ (a+b));
}
處理細節和技巧
處理細節和技巧-輸入
處理細節和技巧-輸出
image.png
image.png