**
pta题目集1~3总结
**
<1>题目详解
1,第一次题目集
7-7 对多个整数进行排序 (16 分)
先从键盘输入一个整数n,n代表需要排序的整数数量,然后再从键盘输入n个整数,对这些数从小到大排序并输出。
输入格式:
先从键盘输入一个整数n,之后回车 再从键盘输入n个整数,整数之间用一个或多个空格分隔
输出格式:
按如下示例输出排序后的数据:The sorted numbers are:排序后的n个数,每个输出的整数之后有一个空格作为分隔符
输入样例:
在这里给出一组输入。例如:
10
3 5 2 4 76 89 56 4 3 8
输出样例:
在这里给出相应的输出。例如:
The sorted numbers are:2 3 3 4 4 5 8 56 76 89
我所提交的代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();//获取需进行排序的数的个数
int t=0;
int i=0;
int j=0;
int [] a=new int [n];
for(i=0;i<n;i++){//将这些数存入数组中
a[i]=sc.nextInt();
}
for(i=0;i<n;i++){//使用冒泡排序进行排序
for(j=0;j<n-i-1;j++){
if(a[j]>a[j+1]){
t=a[j+1];
a[j+1]=a[j];
a[j]=t;
}
}
}
System.out.print("The sorted numbers are:");
for(i=0;i<n;i++){
System.out.print(a[i]+" ");//将排序完成的数按顺序输出
}
}
}
7-8 判断三角形类型 (20 分)
输入三角形三条边,判断该三角形为什么类型的三角形。
输入格式:
在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。
输出格式:
(1)如果输入数据非法,则输出“Wrong Format”;
(2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”;
(3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”;
(4)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”;
(5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”;
(6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”;
(7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。
输入样例1:
在这里给出一组输入。例如:
50 50 50.0
输出样例1:
在这里给出相应的输出。例如:
Equilateral triangle
输入样例2:
在这里给出一组输入。例如:
60.2 60.2 80.56
输出样例2:
在这里给出相应的输出。例如:
Isosceles triangle
输入样例3:
在这里给出一组输入。例如:
0.5 20.5 80
输出样例3:
在这里给出相应的输出。例如:
Wrong Format
代码如下:
import java.util.Scanner;
public class Main{
public static void main(String [] args){
int a,b,c;
Scanner scanner=new Scanner(System.in);
Judge d = new Judge();
a=scanner.nextInt();
b=scanner.nextInt();
c=scanner.nextInt();
//判断输入三边是否合法
if(a<0||a>200||b<0||b>200||c<0||c>200){
System.out.println("Wrong Format");
}
els{
JudgeAngle(a,b,c);
}
}
}
class Judge {
Judge(){
}
public static void JudgeAngle(int a,int b,int c){
int r1,r2,r3;
if(a+b>c&&a+c>b&&b+c>a){
if(a==b||a==c||b==c){
if(a==b&&a*a+b*b==c*c){//判断是否为等腰直角三角行
System.out.println("Isosceles right-angled triangle");
break;
}
else if(a==c&&a*a+c*c==b*b){//判断是否为等腰直角三角行
System.out.println("Isosceles right-angled triangle");
break;
}
else if(b==c&&b*b+c*c==a*a){//判断是否为等腰直角三角行
System.out.println("Isosceles right-angled triangle");
break;
}
else if(a==b&&a==c)//判断是否为等边三角行
System.out.println("Equilateral triangle");
else
System.out.println("Isosceles triangle");
}
else{//判断是否为直角三角行
r1=a*a+b*b-c*c;
r2=a*a+c*c-b;
r3=b*b+c*c-a*a;
if(r1==0||r2==0||r3==0)
System.out.println("Right-angled triangle");
}
}
else
System.out.println("Not a triangle");
}
}
2,第二次题目集
7-4 求下一天 (30 分)
输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法。
要求:Main类中必须含有如下方法,签名如下:
public static void main(String[] args);//主方法
public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型
public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值
public static void nextDate(int year,int month,int day) ; //求输入日期的下一天
输入格式:
在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:
当输入数据非法及输入日期不存在时,输出“Wrong Format”;
当输入日期合法,输出下一天,格式如下:Next date is:年-月-日
输入样例1:
在这里给出一组输入。例如:
2020 3 10
输出样例1:
在这里给出相应的输出。例如:
Next date is:2020-3-11
输入样例2:
在这里给出一组输入。例如:
2025 2 10
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
import java.util.Scanner;
public class Main {
public static int isLeapYear(int year) {//判断是否为闰年
int Isleapyear = 0;
if((year%4==0&&year%100!=0)||year%400==0)
Isleapyear = 1;
return Isleapyear;
}
public static int checkInputValidity(int year,int month,int day) {//判断日期是否合理
int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year)==1)
a[2] = 29;
int c=0;
if(year>=1820&&year<=2020&&month>0&&month<=12&&day<=a[month]&&day>0)
c=1;
return c;
}
public static void nextDate(int year,int month,int day) {
int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year)=1)
a[2] = 29;
int n = 0,b = 0,c = 0;
if(checkInputValidity(year,month,day)==1) {
if(month==12) {
if(day==a[month]) {
n = year+1;
b = 1;
c = 1;
}
if(day>0&&day<a[month]) {
n = year;
b = month;
c =day +1;
}
}
if(month<12) {
if(day==a[month]) {
n = year;
b = month + 1;
c = 1;
}
if(day>0&&day<a[month]) {
n = year;
b = month;
c = day+1;
}
}
System.out.println("Next date is:"+a+"-"+b+"-"+c);
}
else System.out.println("Wrong Format");
}
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
Main b = new Main();
int year = a.nextInt();
int month = a.nextInt();
int day = a.nextInt();
b.nextDate(year,month,day);
}
}
7-5 求前N天 (30 分)
输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
注意:不允许使用Java中任何与日期有关的类或方法。
输入格式:
在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。
输出格式:
当输入的年、月、日以及n的值非法时,输出“Wrong Format”;
当输入数据合法时,输出“n days ago is:年-月-日”
输入样例1:
在这里给出一组输入。例如:
2018 6 19 8
输出样例1:
在这里给出相应的输出。例如:
8 days ago is:2018-6-11
输入样例2:
在这里给出一组输入。例如:
2018 6 19 -8
输出样例2:
在这里给出相应的输出。例如:
-8 days ago is:2018-6-27
import java.util.Scanner;
public class Main {
public static int isLeapYear(int year) {//判断是否为闰年
int Isleapyear = 0;
if((year%4==0&&year%100!=0)||year%400==0)
Isleapyear = 1;
return Isleapyear;
}
public static int checkInputValidity(int year,int month,int day,int n) {//判断日期是否合理
int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year)==1)
a[2] = 29;
int c=0;
if(year>=1820&&year<=2020&&month>0&&month<=12&&day<=a[month]&&day>0&&(n>=-10||n<=10))
c=1;
return c;
}
public static void nextDate(int year,int month,int day,int n) {
int[] a=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year)=1)
a[2] = 29;
int n = 0,b = 0,c = 0;
if(checkInputValidity(year,month,day,n)==1) {
if (day - n >= 1 && day - n <= Month[month])
System.out.printf("%d days ago is:%d-%d-%d\n", n, year, month, day - n);
else {
if (day - n < 1) {
int a = 0 - (day - n);
if (month - 1 < 1) {
year -= 1;
month = 13;
}
System.out.printf("%d days ago is:%d-%d-%d\n", n, year, month - 1, Month[month - 1] - a);
} else {
int a = day - n - Month[month];
if (month + 1 > 12) {
year += 1;
month = 0;
}
System.out.printf("%d days ago is:%d-%d-%d\n", n, year, month + 1, a);
}
}
}
else System.out.println("Wrong Format");
}
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
Main b = new Main();
int year = a.nextInt();
int month = a.nextInt();
int day = a.nextInt();
int n = a.nextInt();
b.nextDate(year,month,day,n);
}
}
3,第三次题目集
7-2 定义日期类 (28 分)
定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。
要求:Date类结构如下图所示:
类图.jpg
输入格式:
在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。
输出格式:
当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
当输入日期合法,输出下一天,格式如下:Next day is:年-月-日
输入样例1:
在这里给出一组输入。例如:
1912 12 25
输出样例1:
在这里给出相应的输出。例如:
Next day is:1912-12-26
输入样例2:
在这里给出一组输入。例如:
2001 2 30
输出样例2:
在这里给出相应的输出。例如:
Date Format is Wrong
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Date b = new Date();
Scanner a = new Scanner(System.in);
b.setyear(a.nextInt());
b.setmonth(a.nextInt());
b.setday(a.nextInt());
b.getnextDate();
}
}
class Date{
private int year = 0;
private int month = 0;
private int day = 0;
int mon_maxnum[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
public Date(){
}
public Date(int year,int month,int day) {
this.year=year;
this.month=month;
this.day=day;
}
public int getyear() {
return year;
}
public void setyear(int year) {
this.year = year;
}
public int getmonth() {
return month;
}
public void setmonth(int month) {
this.month = month;
}
public int getday() {
return day;
}
public void setday(int day) {
this.day=day;
}
public boolean isleapYear(int year) {
boolean a = false;
if(this.year>=1900&&this.year<=2000) {
a=true;
return a;
}
else
return a;
}
public boolean checkInputValidity() {
boolean a = true;
if(this.month<1||this.month>12)
a = false;
if(day<1)
a=false;
if((this.year%4==0&&this.year%100!=0)||this.year%400==0) {
if(this.month==1||this.month==3||this.month==5||this.month==7||this.month==8||this.month==10||this.month==12) {
if(this.day>31)
a=false;
}
else
if(this.month==4||this.month==6||this.month==9||this.month==11) {
if(this.day>30) {
a=false;
}
}
else
if(this.month==2) {
if(this.day>29) {
a=false;
}
}
}
else {
if(this.month==1||this.month==3||this.month==5||this.month==7||this.month==8||this.month==10||this.month==12) {
if(this.day>31) {
a=false;
}
}
else
if(this.month==4||this.month==6||this.month==9||this.month==11) {
if(this.day>30) {
a=false;
}
}
else
if(this.month==2) {
if(this.day>28) {
a=false;
}
}
}
return a;
}
public void getnextDate(){
boolean a,b,c;
a= isleapYear(this.year);
b= checkInputValidity();
if(a!=false&&b!=false) {
if((this.year%4==0&&this.year%100!=0)||this.year%400==0) {
if(this.month==1||this.month==3||this.month==5||this.month==7||this.month==8||this.month==10||this.month==12) {
this.day=this.day+1;
if(this.day>31) {
if(this.month!=12) {
this.month=this.month+1;
}
else{
this.year=this.year+1;
this.month=1;
}
this.day=1;
}
}
else
if(this.month==4||this.month==6||this.month==9||this.month==11) {
this.day=this.day+1;
if(this.day>30) {
this.month=this.month+1;
this.day=1;
}
}
else
if(this.month==2) {
this.day=this.day+1;
if(this.day>29) {
this.month=this.month+1;
this.day=1;
}
}
}
else {
if(this.month==1||this.month==3||this.month==5||this.month==7||this.month==8||this.month==10||this.month==12) {
this.day=this.day+1;
if(this.day>31) {
if(this.month!=12) {
this.month=this.month+1;
}
else {
this.year=this.year+1;
this.month=1;
}
this.day=1;
}
}
else
if(this.month==4||this.month==6||this.month==9||this.month==11) {
this.day=this.day+1;
if(this.day>30) {
this.month=this.month+1;
this.day=1;
}
}
else
if(this.month==2) {
this.day=this.day+1;
if(this.day>28) {
this.month=this.month+1;
this.day=1;
}
}
}
System.out.println("Next day is:"+this.year+"-"+this.month+"-"+this.day);
}
else
System.out.println("Date Format is Wrong");
}
}
7-3 一元多项式求导(类设计) (50 分)
编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。 OO作业3-3题目说明.pdf
输入格式:
在一行内输入一个待计算导函数的表达式,以回车符结束。
输出格式:
如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
当输出结果第一项系数符号为“+”时,不输出“+”;
当指数符号为“+”时,不输出“+”;
当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。
输出格式见输入输出示例。
输入样例1:
在这里给出一组输入。例如:
-2* x^-2+ 5x^12-4x+ 12
输出样例1:
在这里给出相应的输出。例如:
4*x-3+60*x11-4
输入样例2:
在这里给出一组输入。例如:
2*x6-0*x7+5
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
这道题涉及到正则运算我还没太学会,所以我只能跳过
<2>踩坑心得
1,使用else,if语句时时常忘记else使得有时不能完全跳出条件
2,对日期的判断语句一开始写错了,有一个括号是真的容易忘记
3,对等腰直角三角行的判断语句有问题不够准确
<3>改进建议
我认为我的三角形判断还需要继续改进,现在还是有一点问题;还有就是写题目是要更加仔细;还需要继续学习正则表达式
<4>总结
这次的作业对于我来说还是有点超出范围,需要我花大量的时间去不断地学习,不过也就是因为这个我能明显感觉到自己也同样学到了很多在课堂上没有学到的东西。