PTA- JAVA題目集(一)總結與經驗
PTA- JAVA題目集(一)總結與經驗
問題1. 7-1 點線形系列1-計算兩點之間的距離 (10 分)
- 輸入連個點的座標,計算兩點之間的距離
- 輸入格式:
- 4個double型別的實數,兩個點的x,y座標,依次是x1、y1、x2、y2,兩個點的座標之間以空格分隔,每個點的x,y座標以英文“,”分隔。例如:0,0 1,1或0.1,-0.3 +3.5,15.6。
- 若輸入格式非法,輸出"Wrong Format"。
- 若輸入格式合法但座標點的數量超過兩個,輸出“wrong number of points”。
- 輸出格式:
- 計算所得的兩點之間的距離。例如:1.4142135623730951
問題2. 7-2 點線形系列2-線的計算 (42 分)
-
使用者輸入一組選項和資料,進行與直線有關的計算。選項包括:
1:輸入兩點座標,計算斜率,若線條垂直於X軸,輸出"Slope does not exist"。
2:輸入三個點座標,輸出第一個點與另外兩點連線的垂直距離。
3:輸入三個點座標,判斷三個點是否在一條線上,輸出true或者false。
4:輸入四個點座標,判斷前兩個點所構成的直線與後兩點構成的直線是否平行,輸出true或者false.
5:輸入四個點座標,計算輸出前兩個點所構成的直線與後兩點構成的直線的交點座標,x、y座標之間以英文分隔",",並輸出交叉點是否在兩條線段之內(不含四個端點)的判斷結果(true/false),判斷結果與座標之間以一個英文空格分隔。若兩條線平行,沒有交叉點,則輸出"is parallel lines,have no intersection point"。輸入格式:
基本格式:選項+":"+座標x+","+座標y+" "+座標x+","+座標y。
問題3. 7-3 點線形系列3-三角形的計算 (48 分)
例如:1:0,0 1,1
如果不符合基本格式,輸出"Wrong Format"。
如果符合基本格式,但輸入點的數量不符合要求,輸出"wrong number of points"。
不論哪個選項,如果格式、點數量都符合要求,但構成任一條線的兩個點座標重合,輸出"points coincide",使用者輸入一組選項和資料,進行與三角形有關的計算。選項包括:
1:輸入三個點座標,判斷是否是等腰三角形、等邊三角形,判斷結果輸出true/false,兩個結果之間以一個英文空格符分隔。
2:輸入三個點座標,輸出周長、面積、重心座標,三個引數之間以一個英文空格分隔,座標之間以英文","分隔。
3:輸入三個點座標,輸出是鈍角、直角還是銳角三角形,依次輸出三個判斷結果(true/false),以一個英文空格分隔,
4:輸入五個點座標,輸出前兩個點所在的直線與三個點所構成的三角形相交的交點數量,如果交點有兩個,則按面積大小依次輸出三角形被直線分割成兩部分的面積。若直線與三角形一條線重合,輸出"The point is on the edge of the triangle"
5:輸入四個點座標,輸出第一個是否在後三個點所構成的三角形的內部(輸出in the triangle/outof triangle)。
必須使用射線法,原理:由第一個點往任一方向做一射線,射線與三角形的邊的交點(不含點本身)數量如果為1,則在三角形內部。如果交點有兩個或0個,則在三角形之外。若點在三角形的某條邊上,輸出"on the triangle"輸入格式:
基本格式:選項+":"+座標x+","+座標y+" "+座標x+","+座標y。點的x、y座標之間以英文","分隔,點與點之間以一個英文空格分隔。
輸出格式:
基本輸出格式見每種選項的描述。
異常情況輸出:
如果不符合基本格式,輸出"Wrong Format"。
如果符合基本格式,但輸入點的數量不符合要求,輸出"wrong number of points"。
如果輸入的三個點無法構成三角形,輸出"data error"。
注意:輸出的資料若小數點後超過6位,只保留小數點後6位,多餘部分採用四捨五入規則進到最低位。小數點後若不足6位,按原始位數顯示,不必補齊。例如:1/3的結果按格式輸出為 0.333333,1.0按格式輸出為1.0選項4中所輸入線的兩個點座標重合,輸出"points coincide",
1.設計Java程式時先構思類
- public class Main{}//解決指令問題 class point{} //解決點問題 class line{}//解決線問題 class triangle{} //解決三角形問題
- 錯誤寫法:只有一個類Main,所有問題交給main方法執行,無法做到資源的重複利用,從而減少解決相關問題時間和資源;
2.設計類(分析類的職能與作用)
- Main類-------接受輸入,解析輸入,檢查輸入,呼叫point ,line,triangle完成輸入指令
a. public static void main(String[] args) // 執行主功能
b.public static boolean chack_in(String a) // 檢查是否符合格式
c.public static boolean chack_pointsum(String a) // 檢查點數量是否符合
e.public static void operation1() //操作一
..........
- Point類-------包含點的屬性(橫座標x,縱座標),對座標進行賦值,計算點到另外的點的距離
a.double x;double y; //定義橫座標x,縱座標y
b.public void input(Arraylist<String> a); //給x,y賦值
c.public double calculatedistance(Point p) //計算點到另一點的距離
d.public boolean chack_point(Point p); //判斷兩點是否重合
- Line類---------包含線的屬性(點p1,點p2),計算線的斜率,計算另外一點到直線距離,判斷點是否在直線上,判斷兩直線是否平行,計算兩直線交點;
a.Point p1 = new Point();Point p2 = new Point();
b.public void input(Arraylist<String> a); //給點p1,p2賦值
c.public double calculate_slope(void); //計算線的斜率
d.public double calculate_distance(Point p); //計算另外一點到直線的距離
e.public int if_on_line(Point p); //判斷點是否在直線上
f.public boolean if_parallel(Line l); //判斷另外一直線是否與該直線平行
g.public Point intersection(Line l); //計算另外一直線與該直線的交點
h.public boolean chack_slope() //檢查線段斜率是否存在
- Triangle類------包含三角形的屬性(點p1,點p2 , 點p3),根據邊長判斷三角形型別(邊,角),計算三角形面積,周長以及重心座標,計算直線與三角形的交點個數,判斷點是否在三角形的內部
a.Point p1 = new Point();Point p2 = new Point();Point p3 = new Point();
b.public void input(Arraylist<String> a); //給點p1,p2,p3賦值
c.public int if_edge_shape(); //根據邊判斷三角形的形狀
d.public int if_horn_shape(); //根據角判斷三角形的形狀
e.public double caluate_C(); //計算三角形的周長
f.public double caluate_S(); //計算三角形的面積
g.public Point caluate_gravity_point(); //計算三角形的重心
h.public int caluate_sum_intersection(); //計算直線與三角形的交點個數
i.public Point if_inside(); //判斷點是否在三角形內部
j.public boolean chack_triangle(); //判斷三角形是否合法
注意點:設計類中的方法時應該體現“封裝性”,儘量不要進行與類無關的輸出和返回 ,確保方法能夠再次利用
錯誤示範:
public void countk() {
if(Math.abs(p1.x-p2.x)<1e-15) {
System.out.print("Slope does not exist");
}else {
System.out.print((p1.y-p2.y)/(p1.x-p2.x));
}
}
其中將Main類中指令操作(計算斜率)的輸出結果放進了Line類中計算斜率的方法中,而不返回。導致後續程式碼無法引用計算斜率;
3.問題三實現程式碼:
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a = in.nextLine();
if(chack_in(a)) {
System.out.print("");
}else if(chack_point_sum(a)) {
System.out.print("");
}else {
String[] aa = a.split(":");
String[] point = aa[1].split(" ");
switch(aa[0]) {
case "1":
operation_1(point);
break;
case "2":
operation_2(point);
break;
case "3":
operation_3(point);
break;
case "4":
operation_4(point);
break;
case "5":
operation_5(point);
break;
}
}
in.close();
}
//操作1//
public static void operation_1(String[] point) {
Triangle triangle = new Triangle();
String[] point1 = point[0].split(",");
String[] point2 = point[1].split(",");
String[] point3 = point[2].split(",");
triangle.p1.input(Double.parseDouble(point1[0]), Double.parseDouble(point1[1]));
triangle.p2.input(Double.parseDouble(point2[0]), Double.parseDouble(point2[1]));
triangle.p3.input(Double.parseDouble(point3[0]), Double.parseDouble(point3[1]));
if(triangle.chack_triangle()){
System.out.print("data error");
}else {
if(triangle.shape_line_triangle()==1) {
System.out.print("true true");
}else {
System.out.print("true false");
}
}
}
//操作2//
public static void operation_2(String[] point) {
Triangle triangle = new Triangle();
String[] point1 = point[0].split(",");
String[] point2 = point[1].split(",");
String[] point3 = point[2].split(",");
triangle.p1.input(Double.parseDouble(point1[0]), Double.parseDouble(point1[1]));
triangle.p2.input(Double.parseDouble(point2[0]), Double.parseDouble(point2[1]));
triangle.p3.input(Double.parseDouble(point3[0]), Double.parseDouble(point3[1]));
if(triangle.chack_triangle()){
System.out.print("data error");
}else {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(6);
System.out.print(nf.format(triangle.caluate_C())+" "+nf.format(triangle.caluate_S())+" "+
nf.format(triangle.caluate_gravity_point().read_x())+" "+nf.format(triangle.caluate_gravity_point().read_y()));
}
}
//操作3//
public static void operation_3(String[] point) {
Triangle triangle = new Triangle();
String[] point1 = point[0].split(",");
String[] point2 = point[1].split(",");
String[] point3 = point[2].split(",");
triangle.p1.input(Double.parseDouble(point1[0]), Double.parseDouble(point1[1]));
triangle.p2.input(Double.parseDouble(point2[0]), Double.parseDouble(point2[1]));
triangle.p3.input(Double.parseDouble(point3[0]), Double.parseDouble(point3[1]));
if(triangle.chack_triangle()){
System.out.print("data error");
}else {
if(triangle.shape_horn_triangle()==1) {
System.out.print("false false true");
}else if(triangle.shape_horn_triangle()==0) {
System.out.print("false true false");
}else {
System.out.print("true false false");
}
}
}
//操作4//
public static void operation_4(String[] point) {
Triangle triangle = new Triangle();
Triangle triangle1 = new Triangle();
Line line = new Line();
String[] point1 = point[0].split(",");
String[] point2 = point[1].split(",");
String[] point3 = point[2].split(",");
String[] point4 = point[3].split(",");
String[] point5 = point[4].split(",");
line.p1.input(Double.parseDouble(point1[0]), Double.parseDouble(point1[1]));
line.p2.input(Double.parseDouble(point2[0]), Double.parseDouble(point2[1]));
triangle.p1.input(Double.parseDouble(point3[0]), Double.parseDouble(point3[1]));
triangle.p2.input(Double.parseDouble(point4[0]), Double.parseDouble(point4[1]));
triangle.p3.input(Double.parseDouble(point5[0]), Double.parseDouble(point5[1]));
Line l1 = new Line();
l1.p1=triangle.p1;l1.p2=triangle.p2;
Line l2 = new Line();
l2.p1=triangle.p1;l2.p2=triangle.p3;
Line l3 = new Line();
l3.p1=triangle.p2;l3.p2=triangle.p3;
if(line.p1.chack_point(line.p2)){
System.out.print("points coincide");
}else if(triangle.chack_triangle()){
System.out.print("data error");
}else {
if(line.if_parallel(l3)==0||line.if_parallel(l2)==0||line.if_parallel(l1)==0) {
System.out.print("The point is on the edge of the triangle");
}else {
System.out.print(triangle.caluate_sum_intersection(line));
}
}
}
//操作5//
public static void operation_5(String[] point) {
Triangle triangle = new Triangle();
Point p = new Point();
String[] point1 = point[0].split(",");
String[] point2 = point[1].split(",");
String[] point3 = point[2].split(",");
String[] point4 = point[3].split(",");
p.input(Double.parseDouble(point1[0]), Double.parseDouble(point1[1]));
triangle.p1.input(Double.parseDouble(point2[0]), Double.parseDouble(point2[1]));
triangle.p2.input(Double.parseDouble(point3[0]), Double.parseDouble(point3[1]));
triangle.p3.input(Double.parseDouble(point4[0]), Double.parseDouble(point2[1]));
Line l1 = new Line();
l1.p1=triangle.p1;l1.p2=triangle.p2;
Line l2 = new Line();
l2.p1=triangle.p1;l2.p2=triangle.p3;
Line l3 = new Line();
l3.p1=triangle.p2;l3.p2=triangle.p3;
if(triangle.chack_triangle()){
System.out.print("data error");
}else {
if(l1.if_on_line(p)||l2.if_on_line(p)||l3.if_on_line(p)) {
System.out.print("on the triangle");
}else {
if(triangle.if_inside(p)) {
System.out.print("in the triangle");
}else {
System.out.print("outof the triangle");
}
}
}
}
//檢查總字串的格式//
public static boolean chack_in(String a) {
boolean ttr = false;
String[] aa = a.split(":");
String[] point = aa[1].split(" ");
if(aa[0].length()!=1) {
ttr = true;
}else if(aa[0].charAt(0)!='1'&&aa[0].charAt(0)!='2'&&aa[0].charAt(0)!='3'
&&aa[0].charAt(0)!='4'&&aa[0].charAt(0)!='5') {
ttr = true;
}
if(aa.length!=2) {
ttr = true;
}
for(int i=0;i<point.length;i++) {
if(point[i].length()==0) {
ttr = true ;
}
}
for(int i=0;i<point.length;i++ ) {
for(int j=0;j<point[i].length();j++) {
if(point[i].contains(",")) {
String[] number = point[i].split(",");
if(number.length==2) {
if(number[0].length()>=2) {
if(number[0].charAt(0)=='0'&&number[0].charAt(1)=='0') {
ttr = true;
}
}
if(number[1].length()>=2) {
if(number[1].charAt(0)=='0'&&number[1].charAt(1)=='0') {
ttr = true;
}
}
if(!number[0].matches("^([+-]?\\d+)(.\\d+)?")) {
ttr = true ;
}
if(!number[1].matches("^([+-]?\\d+)(.\\d+)?")) {
ttr = true ;
}
}else {
ttr = true ;
}
}else {
ttr = true ;
}
}
}
return ttr;
}
//檢查總字串所含的點的個數//
public static boolean chack_point_sum(String a) {
boolean arry = false;
int cnt = 0;
String[] aa = a.split(":");
String[] point = aa[1].split(" ");
cnt = point.length;
if(a.charAt(0)=='1'&&cnt!=3) {
arry = true;
}else if(a.charAt(0)=='2'&&cnt!=3) {
arry = true ;
}else if(a.charAt(0)=='3'&&cnt!=3) {
arry = true ;
}else if(a.charAt(0)=='4'&&cnt!=5) {
arry = true ;
}else if(a.charAt(0)=='5'&&cnt!=4) {
arry = true ;
}
return arry;
}
}
//點類//
class Point {
private double x,y;
//讀入x y 的值//
public void input(double a,double b) {
x = a;
y = b;
}
//讀取x的值//
public double read_x() {
return x;
}
//讀取y的值//
public double read_y() {
return y;
}
//計算兩點的距離//
public double caluate_distance(Point p) {
return Math.sqrt((x-p.x )*(x-p.x )+(y-p.y )*(y-p.y));
}
//檢查線的兩點是否重合//
public boolean chack_point(Point p) {
boolean wssa = false ;
if(Math.abs(x-p.read_x())<1e-15&&Math.abs(y-p.read_y())<1e-15) {
wssa = true ;
}
return wssa;
}
}
//線段//
class Line{
Point p1 = new Point();
Point p2 = new Point();
//檢查線段的斜率是否存在//
public boolean chack_slope() {
boolean wssa = true ;
if(Math.abs(p1.read_x()-p2.read_x())<1e-15) {
wssa = false ;
}
return wssa;
}
//計算線段的斜率//
public double caluate_slope() {
return (p1.read_x()-p2.read_y())/(p1.read_x()-p2.read_y());
}
//計算點到直線的距離//
public double caluate_distance(Point a) {
double distance ;
if(if_on_line(a)){
distance = 0;
}else {
distance = Math.abs(a.read_x()*p1.read_y()+p1.read_x()*p2.read_y()+p2.read_x()*a.read_y()
-a.read_x()*p2.read_y()-p1.read_x()*a.read_y()-p2.read_x()*p1.read_y())/p1.caluate_distance(p2);
}
return distance ;
}
//判斷點是否再直線上//
public boolean if_on_line(Point p) {
boolean jes = false ;
Line l1 = new Line();
Line l2 = new Line();
l1.p1=p1;l1.p2=p;
l2.p1=p2;l2.p2=p;
if(l1.caluate_slope()==l2.caluate_slope()) {
jes = true;
}
return jes;
}
//判斷點是否線上段內//
//線上段內返回true,線上段外返回false//
public boolean if_in_line(Point p) {
boolean jes = true ;
if(p.caluate_distance(p1)>p1.caluate_distance(p2)||p.caluate_distance(p2)>p1.caluate_distance(p2)) {
jes = false ;
}
return jes;
}
//判斷兩直線是否平行//
//若兩直線平行返回1,若兩直線重合返回0,若兩直線相交返回-1
public int if_parallel(Line l) {
int cnt ;
double x1 = (p1.read_x()-p2.read_x())*(l.p1.read_x()-l.p2.read_x())
+(p1.read_y()-p2.read_y())*(l.p1.read_y()-l.p2.read_y());
if(x1/(p1.caluate_distance(p2)*l.p1.caluate_distance(p2))!=1) {
cnt=-1;
}else {
if(l.caluate_distance(p1)==0) {
cnt=0;
}else {
cnt=1;
}
}
return cnt;
}
//計算兩直線交點,返回Point//
public Point caluate_intersection(Line a) {
Point p = new Point();
double dx1 = p1.read_x()-p2.read_x();
double dx2 = a.p1.read_x()-a.p2.read_x();
double dy1 = p1.read_x()-p2.read_x();
double dy2 = a.p1.read_x()-a.p2.read_x();
double mid1 = dy1/dx1;
double mid2 = dy2/dx2;
double rx = (a.p2.read_y()-p2.read_y()-a.p2.read_y()*mid2+p2.read_y()*mid1)/(mid1-mid2);
double ry = (rx-p2.read_x())*mid1+p2.read_y();
p.input(rx, ry);
return p;
}
}
//三角形//
class Triangle{
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
//檢查三點是否構成三角形//
//若構成三角形返回false,若不構成三角形返回true//
public boolean chack_triangle() {
boolean err = true ;
if(Math.abs(p1.caluate_distance(p2)-p1.caluate_distance(p3))<p2.caluate_distance(p3)
&&Math.abs(p1.caluate_distance(p3)+p1.caluate_distance(p2))>p2.caluate_distance(p3)){
err = false ;
}
return err ;
}
//判斷是否為等邊三角形,或等腰三角形//
//若為等腰三角形,返回0//
//若為等邊三角形,返回1//
public int shape_line_triangle() {
int shape = 0;
if(Math.abs(p1.caluate_distance(p2)-p1.caluate_distance(p3))<1e-15&&
Math.abs(p2.caluate_distance(p1)-p2.caluate_distance(p3))<1e-15) {
shape = 1;
}
return shape;
}
//計算三角形面積,返回面積//
public double caluate_S() {
return Math.abs(p1.read_x()*p2.read_y()+p2.read_x()*p3.read_y()+p3.read_x()*p1.read_y()
-p1.read_x()*p3.read_y()-p2.read_x()*p1.read_y()-p3.read_x()*p2.read_y())/2;
}
//計算三角形的周長,返回周長//
public double caluate_C() {
return p1.caluate_distance(p2)+p1.caluate_distance(p3)+p2.caluate_distance(p3);
}
//計算三角形的重心座標,返回Point//
public Point caluate_gravity_point() {
Point p = new Point();
double dx = (p1.read_x()+p2.read_x()+p3.read_x())/3;
double dy = (p1.read_y()+p2.read_y()+p3.read_y())/3;
p.input(dx, dy);
return p;
}
//判斷三角形是銳角三角形還是直角三角形或鈍角三角形//
//若是銳角三角形返回1,若是直角三角形返回0,若是鈍角三角形則返回-1//
public int shape_horn_triangle() {
int shape ;
double j1 = (p2.read_x()-p1.read_x())*(p3.read_x()-p1.read_x())
+(p2.read_y()-p1.read_y())*(p3.read_y()-p1.read_y());
double j2 = (p3.read_x()-p2.read_x())*(p1.read_x()-p2.read_x())
+(p3.read_y()-p2.read_y())*(p1.read_y()-p2.read_y());
double j3 = (p1.read_x()-p3.read_x())*(p2.read_x()-p3.read_y())
+(p1.read_y()-p3.read_y())*(p2.read_y()-p3.read_y());
if(j1<0||j2<0||j3<0) {
shape = 1;
}else if(j1==0||j2==0||j3==0) {
shape = 0 ;
}else {
shape = -1;
}
return shape;
}
//計算直線與三角形的交點//
//返回交點個數//
public int caluate_sum_intersection(Line a) {
int sum = 0;
Line l1 = new Line();
l1.p1=p1;l1.p2=p2;
Line l2 = new Line();
l2.p1=p1;l2.p2=p3;
Line l3 = new Line();
l3.p1=p2;l3.p2=p3;
if(l1.if_parallel(a)==1) {
Point point1 = a.caluate_intersection(l2);
if(l2.if_in_line(point1)) {
sum++;
}
Point point2 = a.caluate_intersection(l3);
if(l3.if_in_line(point2)) {
sum++;
}
}else if(l2.if_parallel(a)==1) {
Point point1 = a.caluate_intersection(l1);
if(l1.if_in_line(point1)) {
sum++;
}
Point point2 = a.caluate_intersection(l3);
if(l3.if_in_line(point2)) {
sum++;
}
}else if(l3.if_parallel(a)==1) {
Point point1 = a.caluate_intersection(l1);
if(l1.if_in_line(point1)) {
sum++;
}
Point point2 = a.caluate_intersection(l2);
if(l2.if_in_line(point2)&&point1.chack_point(point2)) {
sum++;
}
}else {
Point point1 = a.caluate_intersection(l1);
if(l1.if_in_line(point1)) {
sum++;
}
Point point2 = a.caluate_intersection(l2);
if(l2.if_in_line(point2)&&point1.chack_point(point2)) {
sum++;
}
Point point3 = a.caluate_intersection(l2);
if(l2.if_in_line(point3)&&point1.chack_point(point3)&&point2.chack_point(point3)) {
sum++;
}
}
return sum;
}
//判斷點是否再三角形內部//
public boolean if_inside(Point a) {
boolean rea = false ;
Line l1 = new Line();
l1.p1=p1;l1.p2=a;
Line l2 = new Line();
l2.p1=p2;l2.p2=a;
Line l3 = new Line();
l3.p1=p3;l3.p2=a;
Line l4 = new Line();
l4.p1=p1;l4.p2=p2;
Line l5 = new Line();
l5.p1=p2;l5.p2=p3;
Line l6 = new Line();
l6.p1=p3;l6.p2=p1;
if(!l5.if_in_line(l1.caluate_intersection(l5))||!l4.if_in_line(l3.caluate_intersection(l4))
||!l6.if_in_line(l2.caluate_intersection(l6))) {
rea = true;
}
return rea;
}
}