java学习笔记--1_常见输入输出语句熟悉篇章

今天上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平台使用!每一道都可以仔细看一下。

---- 动动手指关注我!或许下次你又能在我这里找到你需要的答案!ZZZZW与你一起学习,一起进步!
原文地址:https://www.cnblogs.com/zzzzw/p/4524386.html