Output a greeting phrase "Hello, Java! "
Input
None
Output
Hello, Java!
Hints
Please output things according to the requirements. No extra output is allowed.
The class name MUST be Main. It's case sensitive. And NO package statement is allowed, so get rid of the package statement if there is such statement.
代码如下:
public class Main{
public static void main(String[] args){
System.out.println("Hello, Java!");
}
}
学习java的第一节台阶,主要是对java框架的理解以及sout的使用,然后正式开始java的学习。
2. what have you got?
You are requested to output what you got from the system console.
Input:
A line of string, until no input is found! Example:
This is the first line. This is the second line. ... This is the end.
Output:
What you get from the console(keyboard)
This is the first line. This is the second line. ... This is the end.
Hints:
On getting input from the console, you can refer to various articles on the web. For reading a line from the console, here is an example:
Scanner s = new Scanner(System.in);
if (s.hasNextLine()) {
String line = s.nextLine();
......
}
You can get more info on console input/output by Google.
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (true) {
if (s.hasNextLine()){
String line = s.nextLine();
System.out.println(line);
}
else
break;
}
s.close();
}
}
这次题目涉及到了hasnextline的使用,下面对hasnextline和hasnext等进行辨析:
3.A+B problemYou are requested to calculate the sum of two integral numbers a and b.
InputA pair of number separated by the blank character. Example
5 12
Outputthe sum of the input pair
17Hints
On getting input from the console, you can refer to various articles on the web. Here is an example,
Scanner s = new Scanner(System.in); int age = s.nextInt();
If you want to get the whole line, you can use
String str = s.nextLine();
You can get more info on console input/output by Google.
代码如下:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
long age1 = s.nextLong();
long age2 = s.nextLong();
long count=age1+age2;
System.out.println(count);
s.close();
}
}
4.A+B problem II
You are requested to calculate the sum of two integers a and b. This time we go further. There are multiple pair of a and b in a testing group. So you need to calculate the sum several times for each testing group. Note that there may be huge number of pairs to calculate. So don't make an assumption of how many test cases.
InputTest case count T following by each test case. Example:
3 5 12 1 1 3 -5Output
The sum of each input pair
17 2 -2Hints
On geting input from the console, you can refer to various articles on the web. Here is a example:
Scanner s = new Scanner(System.in); int age = s.nextInt();
If you want to get the whole line, you can use:
String str = s.nextLine();
You can get more info on console input/output by Google.
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
long sum = s.nextLong();
for(long i = 0;i
5.Repeat I
Given a non negative number n(0<=n<=20) and a string s, you are expected to repeatedly output the string s n times.
Input:
4
aabb
output:
aabbaabbaabbaabb
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
int n = s.nextInt();s.nextLine();
String str = s.nextLine();
for (int i=0;i
6.Repeat II
Given a non negative number n(0<=n<=20) and a string s, you are expected to repeatedly output the string s in n lines.
Input:
4
aabb
output:
aabb
aabb
aabb
aabb
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
int n = s.nextInt();
String b = s.nextLine();
String str = s.nextLine();
for (int i=0;i
7.Repeat III
An repeat problem is that given a non negative number n(0<=n<=20) and a string s, you are expected to repeatedly output the string s n times. Now it has become a bit more complicated. There are several test cases.
Input: Total test case count T following by T cases of test instances. Example:
3
4 aabb
0 a b
3 cd
output:
aabbaabbaabbaabb
cdcdcd
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
int number = s.nextInt();
for(int i = 0; i < number; i++){
int j=s.nextInt();
String str = s.nextLine();
for(int m=0;m
8. Approximate PI
π can be computed using the following formula: pi = 4 * (1- frac{1}{3}+frac{1}{5}-frac{1}{7}+frac{1}{9}-...+frac{1}{n})π=4∗(1−31+51−71+91−...+n1)
For a given number nn, write a program that displays the result of piπ.
Input
A single line containing a odd number nn. Example:
9
Output
The result of calculated PI, keep 6 digit after the decimal point.
3.339683
Hints
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
double sum=0,temp=0;
int k=1;
for(int j=1;j<=n;j=j+2) {
temp=1.0/(double)j;
temp=temp*k;
sum+=temp;
k=-1*k;
}
double PI=4*sum;
System.out.printf("%.6fn",PI);
}
}
9.A+B Problem III
Description
Your task is to Calculate a + b
Too easy?!
Of course! I specially designed the problem for acm beginners.
You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same goal.
Input
The input will consist of a series of pairs of integers aa and bb, separated by a space, one pair of integers per line. Both aa and bb are within the range of 32 bit integers.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
1 5
10 20
Sample Output
6
30
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
while (true) {
if(s.hasNextLine()){
long a =s.nextLong(),b=s.nextLong();
System.out.println(a+b);
}else break;
}s.close();
}
}
10.A+B Problem IV
Description
Your task is to Calculate a + b.
Input
Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.
Output
For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.
Sample Input
1 5
10 20
0 0
Sample Output
6
30
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s =new Scanner(System.in);
while (true) {
long a =s.nextLong(),b=s.nextLong();
if(a==0 && b==0)
break;
System.out.println(a+b);
}s.close();}
}
11.Digit count
Given a number n, you are required to output how many digits are there.
There are several test cases for each test group.
Input
Test case count T following by each test case
Output
Output digit count of the given numbers in each group.
Sample Input
2
12
120
Sample Output
2
3
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
int[] l=new int[100];
int n=Math.abs(a.nextInt());
for(int j=0;j
12.Unique Digits
Given a number n, you are required to output its unique digits in appearing order. There are several test cases for each test group.
Input
Test case count T following by each test case, preceding zeros may exist. Example:
5
1212
120
0121
0000
-23
Output
12
120
012
0
23
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
int b = a.nextInt();
for(int i=0;i < b;i++){
String num = a.next();
char[] arr = num.toCharArray();
int j=0;
if(arr[0]=='-')j=1;
outer:for(;j
13.Sum of digits
Given a number n, you are required to output the sum of its digits. There are several test cases for each test group.
Input
Test case count T following by each test case. Example:
5
1211
1234
012
1111
-23
Output
5
10
3
4
5
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
int[] sum=new int[10];
for(int i=0;i<10;i++)sum[i]=0;
Scanner in = new Scanner(System.in);
int n=in.nextInt();
for(int j=0;j=0){
char[]strarray=t.toCharArray();
for(int i=0;i
14.String Length
Your are to output the length of a string. There are several test cases for each test group.
Input: Test case count T following by each test case. Example:
4
asdf
avc asd as
00100
Output:
4
8
10
5
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
int c = Integer.parseInt(a.nextLine());
for(int i=0;i
15.foo bar baz
You are given two numbers "from"(inclusive) and "to"(inclusive). And you are expected to create an application that loops from "from" to "to" and prints out each value on a separate line, except print out "foo" for every multiple of 3, "bar" for every multiple of 5, and "baz" for every multiple of 7.
If "From" is greater than "to", then output a blank line.
For example: if from=1 and to=16, then it will give the following output:
1
2
3 foo
4
5 bar
6 foo
7 baz
8
9 foo
10 bar
11
12 foo
13
14 baz
15 foo bar
16
Input: Total test case count T following by T cases of test instances. Example:
3
4 5
6 10
3 -1
output:
4
5 bar
6 foo
7 baz
8
9 foo
10 bar
Hint -- The % operator calculates an integer remainder.
代码如下:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner a=new Scanner(System.in);
int b = a.nextInt();
for(int i=0;ito){
System.out.print("n");
continue;
}
for(int count = from;count>=from&&count<=to;count++){
if(count%3==0&&count%5!=0&&count%7!=0)System.out.println(count+" foo");
else if(count%3!=0&&count%5==0&&count%7!=0)System.out.println(count+" bar");
else if(count%3!=0&&count%5!=0&&count%7==0)System.out.println(count+" baz");
else if(count%3==0&&count%5==0&&count%7!=0)System.out.println(count+" foo bar");
else if(count%3==0&&count%7==0&&count%5!=0)System.out.println(count+" foo baz");
else if(count%5==0&&count%7==0&&count%3!=0)System.out.println(count+" bar bar");
else if(count%3==0&&count%5==0&&count%7==0)System.out.println(count+" foo bar baz");
else System.out.println(count);
}
}
a.close();
}
}



