一、PTA 基本数据类型
文章目录
1.求最大值 (15 分)
本题目要求读入2个整数A和B,然后输出两个数的最大值。
输入格式:
输入在一行中给出2个绝对值不超过1000的整数A和B。
输出格式:
对每一组输入,在一行中输出最大值。
输入样例:
18 -299
输出样例:
18
源代码:
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. 编程题:统计符合条件元素的个数-hebust (15 分)
统计1…n的闭区间中,能够被3整除元素的奇数和偶数的个数
输入格式:
输入值n的范围是 【1…1000】
输出格式:
奇数个数,偶数个数
输入样例:
5
输出样例:
1,0
源代码:
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
源代码:
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基本语法-整数四则运算 (20 分)
输入2个整数,输出它们的和、差、乘积和准确的商。
输入格式:
输入两个整数
输出格式:
每一行中依次输出四则运算的结果
输入样例:
70
16
输出样例:
86
54
1120
4.375
源代码:
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.计算两个数的和 (15 分)
计算两个数的和。 通过键盘为变量a和b赋值,然后计算变量a与b的和,并将和赋值给变量sum,最终输出变量sum的值;
输入格式:
输入两个整数
输出格式:
两个数的和
输入样例:
2 8
输出样例:
10
源代码:
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.求一个三位正整数各位数字之和 (15 分)
求一个三位正整数各位数字之和
输入格式:
输入一个三位的正整数
输出格式:
输出百十个位上各位数字的和
输入样例:
678
输出样例:
21
源代码:
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);
}
}