今天上oj,想來是准備做做算法和數據結構的。看了看以前做的基礎題目,想着就先熟悉一下java的語言吧!
以下是今天做的10道題目。
備注:oj有時候對格式要求非常嚴格,因為不在格式上糾結太久,只要eclipse編譯出來正確的結果。我就直接跳過了!
一下是題目及代碼:
1001 Hello World!
1 Hello World! 2 3 Time Limit: 200/100 MS (Java/Others) Memory Limit: 32768/5000 K (Java/Others) 4 Total Submission(s): 2931 Accepted Submission(s): 1127 5 6 Description 7 8 輸出以下一行文字: 9 10 Hello World! 11 Input 12 13 (無) 14 Output 15 16 利用printf()函數輸出 "Hello World!"這行文字,最后要一個換行。 17 Sample Input 18 19 (無) 20 Sample Output 21 22 Hello World! 23 Hint
public class HellowWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
1002 格式化輸出(常量練習)
1 Description 2 3 用C語言的printf( )函數輸出下列內容: 請使用格式控制符,如 %d %c %f 等,否則判為cheat 4 100 ———— 一個整數 5 A ———— 一個字符 6 3.140000 —— 輸出小數點后6位。 7 Input 8 9 (本題目沒有輸入數據) 10 Output 11 12 輸出三行數據: 13 100 14 A 15 3.140000 16 Sample Input 17 18 (本題目沒有輸入數據) 19 Sample Output 20 21 100 22 A 23 3.140000 24 25 Author 26 John
1 //3.140000 這個比較難一點 2 public class oj1002 { 3 public static void main(String args[]) 4 { 5 char A='A'; 6 //double b=3.140000; 7 System.out.println(100); 8 System.out.println(A); 9 //String b1 = new Double(b).toString(); 10 //System.out.println(b1.substring(b1.lastIndexOf(".")+1,3)); 11 //System.out.println((double) (Math.round(b*1000)/1000.0)); 12 String pattern="0.000000"; 13 java.text.DecimalFormat df=new java.text.DecimalFormat(pattern); 14 String s=df.format(3.140000); 15 System.out.println(s); 16 17 } 18 19 }
1003階乘 I
Description 請輸入並運行階乘程序的代碼,以驗證代碼的正確性,並對C語言程序有一個感性的認識。 利用以下公式,求一個正整數n的階乘。 n! = 1 * 2 * ... * n Input 一個正整數 n 。 Output 計算並輸出 n! 的值。 Sample Input 5 Sample Output 120
1 public class oj1003 2 { 3 public static void main(String args[]) 4 { 5 int n=5; 6 int result=1; 7 for(int i=1;i<=n;i++) 8 { 9 result=result*i; 10 11 } 12 System.out.println(result); 13 } 14 }
1004階乘 II
1 Description 2 3 本題與上一題《階乘 I》是一樣的,不同之處在於本題是多測試用例。通過本題,逐漸熟悉本OJ多測試用例的模式。 4 5 利用以下公式,求正整數n的階乘。 6 7 n! = 1 * 2 * ... * n 8 Input 9 10 本題有多測試用例。 11 12 每個測試用例占一行,每行是一個正整數 n 。 13 Output 14 15 為每個測試用例輸出一行結果:n! 的值。 16 Sample Input 17 18 2 19 5 20 Sample Output 21 22 2 23 120
1 public class oj1004 2 { 3 public static void main(String args[]) 4 { 5 int n1=2,n2=5; 6 int re1=1,re2=1; 7 for(int i=1;i<=n1;i++) 8 { 9 re1=re1*i; 10 } 11 for(int j=1;j<=n2;j++) 12 { 13 re2=re2*j; 14 } 15 System.out.println(re1); 16 System.out.print(re2); 17 } 18 19 }
1005圖形輸出(字符常量練習)
1 Description 2 3 用C語言的printf()函數輸出下列內容: 4 5 * 6 *** 7 ***** 8 ******* 9 ********* 10 Input 11 12 本題目沒有輸入數據 13 Output 14 15 輸出的圖形由5行組成,第一行有1顆星星,第i行有連續的2i-1顆星星。 16 17 注意每一行前面的空格數。最后一行前面沒有空格。 18 Sample Input 19 20 (本題目沒有輸入數據) 21 Sample Output 22 23 * 24 *** 25 ***** 26 ******* 27 *********
1 public class oj1005 2 { 3 public static void main(String args[]) 4 { 5 System.out.println(" *"); 6 System.out.println(" ***"); 7 System.out.println(" *****"); 8 System.out.println(" *******"); 9 System.out.println("*********"); 10 11 } 12 13 }
1006單個字符輸入和輸出(順序結構)
1 Description 2 3 用 函數scanf()從鍵盤上輸入一個字符,用 函數printf() 輸出。 4 5 本題用 函數getchar() 和 putchar() 也可以完成同樣功能。請試一試。 6 Input 7 8 一個字符。 9 Output 10 11 輸出剛剛讀入的那個字符。溫馨提示:不用輸出換行哦。 12 Sample Input 13 14 a 15 Sample Output 16 17 a 18 Author 19 John
1 public class oj1006 2 { 3 public static void main(String args[]) 4 { 5 char q='a'; 6 System.out.print(q); 7 } 8 }
1007字符輸入和輸出
1 Description 2 3 讀入一個字符,然后輸出它。 4 Input 5 6 有多個測試用例。 7 8 每個測試用例占一行:是一個字符。 9 Output 10 11 為每個測試用例輸出一行:剛剛讀入的字符。 12 Sample Input 13 14 a 15 2 16 b 17 9 18 9 19 c 20 Sample Output 21 22 a 23 2 24 b 25 9 26 9 27 c
1 public class oj1007 2 { 3 public static void main(String args[]) 4 { 5 char[] q={'a','2','b','9','9','c'}; 6 for(int i=0;i<=4;i++) 7 { 8 System.out.println(q[i]); 9 } 10 System.out.print(q[5]); 11 12 } 13 14 }
1008計算a+b(順序結構)
1 Description 2 3 這是一道在各OJ訓練網站上最基本的題目,一般都放在第一道題,來讓大家熟悉"在線程序自動評測系統"(簡稱OJ)的環境。 4 5 讀入兩個整數,然后計算它們的和,並輸出它們的和。 6 Input 7 8 輸入只有一行,兩個整數a和b。它們之間用空格分隔。 9 Output 10 11 輸出這兩個整數之和。 12 Sample Input 13 14 1 2 15 Sample Output 16 17 3 18 Author 19 John
1 import java.util.Scanner; 2 public class oj1008 3 { 4 public static void main(String args[]) 5 { 6 int a,b; 7 int c; 8 Scanner in=new Scanner(System.in);//輸入兩個整數 9 a=in.nextInt(); 10 b=in.nextInt(); 11 c=a+b; 12 System.out.println(c); 13 14 } 15 16 }
1009計算a+b(多測試用例)
1 Description 2 3 本題與上題類似,也是讀入兩個整數,計算並輸出它們的和。 4 5 不同之處在於本題是多測試用例。 6 Input 7 8 有多個測試用例,每個測試用例占單獨一行:兩個整數a和b,它們中間用一個空格分隔。 9 Output 10 11 對應每個測試用例,單獨輸出一行:這兩個整數之和。 12 Sample Input 13 14 1 2 15 -1 9 16 Sample Output 17 18 3 19 8
1 import java.util.Scanner; 2 public class oj1009 3 { 4 public static void main(String args[]) 5 { 6 int a,b; 7 Scanner in=new Scanner(System.in); 8 while(in.hasNext())//有多行測試用例的時候用這個方法 Scanner in中的in就是in.hasNext()中的in 9 { 10 a=in.nextInt(); 11 b=in.nextInt(); 12 int c=a+b; 13 System.out.println(c); 14 } 15 16 } 17 18 }
1010求絕對值(分支結構)
Description 求實數的絕對值。本題用於練習選擇結構(或稱分支結構)。正數的絕對值是其本身,負數的絕對值是其相反數。 Input 有多個測試數據,一個占一行,每行是一個實數 a 。 Output 為每個測試數據輸出一行:a的絕對值,結果保留兩位小數。 Sample Input -126.369 66 -10 0 Sample Output 126.37 66.00 10.00 0.00
1 import java.util.Scanner; 2 import java.text.DecimalFormat; 3 public class oj1010 4 { 5 public static void main(String args[]) 6 { 7 Scanner in=new Scanner(System.in); 8 while(in.hasNext()) 9 { 10 int a=in.nextInt(); 11 if(a>=0) 12 { 13 String pattern="0.00"; 14 //java.text.DecimalFormat df=new java.text.DecimalFormat(pattern); 15 DecimalFormat df=new DecimalFormat(pattern); 16 String s=df.format(a); 17 System.out.println(s); 18 } 19 else 20 { 21 a=Math.abs(a); 22 String pattern="0.00"; 23 //java.text.DecimalFormat df=new java.text.DecimalFormat(pattern); 24 DecimalFormat df=new DecimalFormat(pattern); 25 String s=df.format(a); 26 System.out.println(s); 27 } 28 29 } 30 } 31 32 }
題目難度不大,適合熟悉相應語法和熟悉oj平台使用!每一道都可以仔細看一下。
