沈师Java程序设计PTA编程题答案1. 求最⼤值 (10 分)
本题⽬要求读⼊2个整数A和B,然后输出两个数的最⼤值。
输⼊格式:
输⼊在⼀⾏中给出2个绝对值不超过1000的整数A和B。
输出格式:
对每⼀组输⼊,在⼀⾏中输出最⼤值。
输⼊样例:
在这⾥给出⼀组输⼊。例如:
18 -299
输出样例:
在这⾥给出相应的输出。例如:
18
答案:
import Scanner;
public class Main{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.print(a>b?a:b);
}
}
2. 编程题:统计符合条件元素的个数-hebust (10 分)
统计1…n的闭区间中,能够被3整除元素的奇数和偶数的个数
输⼊格式:
输⼊值n的范围是 【1…1000】
输出格式:
奇数个数,偶数个数
输⼊样例:
5
输出样例:
1,0
答案:
import 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.print(x+","+y);
}
}
3. 编程题:判断闰年-hebust (10 分)
根据输⼊的正整数y所代表的年份,计算输出该年份是否为闰年 闰年的判断标准:
能够被4整除且不能被100整除的年份
或者能够被400整除的年份
输⼊格式:
输⼊n取值范围是 【1…3000】
输出格式:
是闰年,输出 yes
⾮闰年,输出 no
输⼊样例:
在这⾥给出⼀组输⼊。例如:
100
输出样例:
在这⾥给出相应的输出。例如:
no
答案:
import Scanner;
public class Main {
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
if(n%4==0&&n%100!=0||n%400==0)
System.out.print("yes");
else
System.out.print("no");
}
}
4. Time Difference (10 分)
What is the time difference between 10:30 and 11:45?select查询join的用法
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 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();
if(m2==m1&&h2==h1){
System.out.print(0+" "+0);
}
else if(m2>=m1){
System.out.print(h2-h1+" "+(m2-m1));
}else{
System.out.print(h2-h1-1+" "+(m2-m1+60));
}
}
}
5. java基本语法-整数四则运算 (10 分)
输⼊2个整数,输出它们的和、差、乘积和准确的商。输⼊格式:
输⼊两个整数
jquery基础教程pdf教程输出格式:
每⼀⾏中依次输出四则运算的结果
输⼊样例:
70
16
输出样例:
在这⾥给出相应的输出。例如:
86
54
1120
4.375
答案:
import Scanner;
public class Main{
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a+b);
System.out.println(a-b);
System.out.println(a*b);
System.out.println((double)a/b);
}
}
6. 学投资 (10 分)
⼩⽩学习了⼀些复利投资知识,想⽐较⼀下复利能多赚多少钱(所谓复利投资,是指每年投资的本⾦是上⼀年的本⾦加收益。⽽⾮复利投资是指每年投资⾦额不包含上⼀年的收益,即固定投资额)。假设他每年固定投资M元(整数),每年的年收益达到P(0<P<1,double),那么经过N(整数)年后,复利投资⽐⾮复利投资多收⼊多赚多少钱呢?计算过程使⽤双精度浮点数,最后结果四舍五⼊输出整数(Math的round函数)。
输⼊格式:
新手学java用什么编译器M P N
输出格式:
复利收⼊(含本⾦),⾮复利收⼊(含本⾦),复利⽐⾮复利收⼊多的部分(全部取整,四舍五⼊)
输⼊样例:
10000 0.2 3
输出样例:
17280 16000 1280
答案:
import Scanner;
public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
long Long();
double Double(),sum1,sum2,c;
int Int();
sum1=M*(1+N*P);
sum2=M*(Math.pow((1+P),(double)N));
if(sum1>sum2) c=sum1-sum2;
else c=sum2-sum1;
System.out.und(sum2)+" ");
System.out.und(sum1)+" ");
System.out.und(c));
}
}
7. 点是否在圆内? (10 分)
编写程序,提⽰⽤户输⼊⼀个点(x,y),然后检查这个点是否在以原点(0,0)为圆⼼、半径为10的圆内。
输⼊格式:
输⼊任意⼀个点的x轴和y轴坐标值,且两个值之间空格分隔。
输出格式:
若点在圆内,输出1,否则输出0。
输⼊样例:
4 5
输出样例:
1
答案:
import Scanner;
public class Main {
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
int n1 = sc.nextInt();
int n2 = sc.nextInt();
System.out.println(Math.sqrt(n1*n1+n2*n2)<=10?1:0);
}
可以终止某一进程的快捷键是}
8. 给出⼀个⽉的总天数 (10 分)
编写程序,提⽰⽤户输⼊⽉份和年份,然后显⽰这个⽉的天数。
输⼊格式:
输⼊任意符合范围(1⽉9999年)年份,且两个值之间空格分隔。
12⽉)的⽉份和(1900年
java经典上机编程题输出格式:
输出给定年份和⽉份的天数。
输⼊样例:
2 2000
输出样例:
29
答案:
import Scanner;
public class Main {
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int n3;
switch(n1){
case2:
if(n2%4==0&&n2%100!=0||n2%400==0)
n3=29;
else
n3=28;
break;
case1:
case3:
case5:
case7:
case8:
case10:
case12:
n3=31;
break;
default:
n3=30;
}
System.out.println(n3);
}
}
9. 程序改错题1 (10 分)
程序改错题。以下代码⽬标是实现从键盘输⼊1个整数x,然后根据x的值做不同的计算,输出结果。(程序有错,请改正后提交)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论