1. Hello, Java
例子:
Input: None
Output:Hello, Java!
代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello, 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 inut 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.
代码:
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();
}
}
3、A+B问题
You are requested to calculate the sum of two integral numbers a and b.
例子:
Input:
A pair of number seperated by the blank character. Example:
5 12
Output: the sum of the input pair
17
代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int age1 = s.nextInt();
int age2 = s.nextInt();
int count = age1+age2;
System.out.println(count);
s.close();
}
}
4、A+B问题进阶
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.
例子:
Input:
Test case count T following by each test case. Example:
3
5 12
1 1
3 -5
Output: the sum of each input pair
17
2
-2
代码:
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int sum = s.nextInt();
for(int i = 0; i < sum; i++)
{
int age1 = s.nextInt();
int age2 = s.nextInt();
int count = age1+age2;
System.out.println(count);
}
s.close();
}
}
5、Approximate PI
PI can be computed using the following formula:
PI = 4 * (1-1/3+1/5-1/7+1/9-…+1/n).
For a given n, write a program that displays the result of PI
例子:
Input:
A single line containing a odd number n. Example:
9
Output: the result of calculated PI, keep 6 digit after the decimal point.
3.339683
代码:
import java.util.*;
public class Main {
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);
int num = s.nextInt();
double count = 0, k=1;
for(double i = 1; i <= num; i = i + 2.0)
{
count = k/i + count;
k = k*(-1.0);
}
count = 4.0 * count;
System.out.println(String.format("%.6f", count));
s.close();
}
}
6、A+B问题后续1
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 aim.
Input
The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.
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.hasNextInt())
{
int num1 = s.nextInt();
int num2 = s.nextInt();
int count = num1 + num2;
System.out.println(count);
}
else break;
}
s.close();
}
}
7、A+B问题后续2
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)
{
int num1 = s.nextInt();
int num2 = s.nextInt();
int count = num1 + num2;
if(num1 == 0 && num2 == 0)
break;
System.out.println(" "+count);
}
s.close();
}
}
8、各个位数的绝对值之和
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.
5
1211
1234
012
1111
-23
Output:
5
10
3
4
5
代码:
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 sum = 0, k = 0;
int count = Math.abs(s.nextInt());
while(true)
{
k = count % 10;
sum = k + sum;
count = (count - k)/10;
if(count == 0)
break;
}
System.out.println(" "+sum);
}
s.close();
}
}