One, PTA basic data type
1. Find the maximum value (15 points)
This question requires to read in two integers A and B, and then output the maximum of the two numbers.
Input format:
Enter two integers A and B whose absolute value does not exceed 1000 in one line.
Output format:
For each group of inputs, output the maximum value in one line.
Input sample:
18 -299
Sample output:
18
Source code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int x= sc.nextInt();
int y= sc.nextInt();
int z=x>y?x:y;
System.out.println(z);
}
}
2. Programming questions: Count the number of eligible elements-hebust (15 points)
Count the odd and even numbers of elements that can be divisible by 3 in the closed interval of 1...n
Input format:
The range of the input value n is [1…1000]
Output format:
Odd number, even number
Input sample:
5
Sample output:
1,0
Source code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int x=0,y=0;
for(int i=1;i<=n;i++){
if(i%3==0){
if(i%2==0){
y++;
}
else{
x++;
}
}
}
System.out.println(x+","+y);
}
}
3. Time Difference (20 分)
What is the time difference between 10:30 and 11:45?
Your program reads two time spots and prints the time difference between them, in terms of hours and minutes.
Input Format
Two time spots, in 24-hour, each is represented as two numbers, as “hour minute”. The second time spot is later than the first and both are within the same day.
Output Format
Two numbers represent the time difference. The first is the hours in the difference, while the second is the minutes.
Sample Input
10 30 11 45
Sample Output
1 15
Source code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h1 = sc.nextInt();
int m1 = sc.nextInt();
int h2 = sc.nextInt();
int m2 = sc.nextInt();
int h,m;
if(m2>=m1){
h=h2-h1;
m=m2-m1;
}
else{
h=h2-1-h1;
m=m2+60-m1;
}
System.out.println(h + " " + m);
}
}
4. Java basic grammar-four arithmetic operations on integers (20 points)
Input 2 integers, and output their sum, difference, product and accurate quotient.
Input format:
Enter two integers
Output format:
The results of the four arithmetic operations are sequentially output in each row
Input sample:
70
16
Sample output:
86
54
1120
4.375
Source code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m= sc.nextInt();
int n= sc.nextInt();
int a=m+n;
int b=m-n;
int c=m*n;
double d=(double)m/(double)n;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.printf("%.3f",d);
}
}
5. Calculate the sum of two numbers (15 points)
Calculate the sum of two numbers. Assign values to variables a and b through the keyboard, then calculate the sum of variables a and b, and assign the sum to the variable sum, and finally output the value of the variable sum;
Input format:
Enter two integers
Output format:
The sum of two numbers
Input sample:
2 8
Sample output:
10
Source code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m= sc.nextInt();
int n= sc.nextInt();
System.out.println(m+n);
}
}
6. Find the sum of the digits of a three-digit positive integer (15 points)
Find the sum of the digits of a three-digit positive integer
Input format:
Enter a three-digit positive integer
Output format:
Output the sum of the digits in the hundred and ten digits
Input sample:
678
Sample output:
21
Source code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m= sc.nextInt();
int a=m/100;
int b=m%100/10;
int c=m%10;
System.out.println(a+b+c);
}
}