大作业总结
1.前言
转眼间学期已过大半, Java作为一门面向对象编程的语言,最近三周在经历俩次作业的训练后我也算是对Java的学习有了更深入的进展。回归正题讲述我这三次题目集的知识点、题量、难度等情况
第一次作业:第一题和第三题知识点主要是在下用Java的基本语法实现简单的交互性输入以及格式化输出,同时涉及一些字符串的运用,运用多个方法即可实现.第二题则是承接前面几次作业的点线形作业在这次承接出了四边形,主要涉及利用Java语法将四边形的一些数学方法复现,难度较大。
期中考试:主要考察继承与多态对于子父类的考察以及对类与对象的理解
第二次作业:点线形作业最后的压轴大戏-五边形。以往都是一题,这次拆分成了俩题,可见其难度。
2.设计与分析
1 import java.util.Scanner;
2 public class Main{
3
4 public static void main(String[] args) {
5 Scanner in = new Scanner(System.in);
6 String a=in.nextLine();
7 while(a.equals("end")==false)
8 {
9 getnum(a);
10 a=in.nextLine();
11 }
12 }
13
14 private static void getnum(String a) {
15 String[]b=a.split("\\D+");
16 int i;
17 long sum=0;
18 for(i=0;i<b.length;i++){
19 if(!b[i].equals(""))
20 {
21 int num=Integer.parseInt(b[i]);
22 sum+=num;
23 }
24 }
25 System.out.println(sum);
26 }
27 }
该题只是简单的读取字符串获取其中数字并求和再次不过多赘述
1 import java.util.Scanner;
2 public class Main {
3
4 public static String bankName="中国银行";
5 private static String name;
6 private static String password;
7 private static float balance;
8 public static void main(String[] args) {
9 Scanner sc=new Scanner(System.in);
10 welcome();
11 Main account = new Main(sc.next(), sc.next());
12 account.deposit(sc.next(), sc.nextFloat());
13 account.withdraw(sc.next(), sc.nextFloat());
14 account.withdraw(sc.next(), sc.nextFloat());
15 account.withdraw(sc.next(), sc.nextFloat());
16 welcomeNext();
17 }
18 private static void welcomeNext() {
19 // TODO Auto-generated method stub
20 System.out.println("请收好您的证件和物品,欢迎您下次光临!");
21 }
22 private void withdraw(String password2, float money) {
23 if(password2.equals(password))
24 {
25 float t=balance;
26 balance=balance-money;
27 if(balance<0)
28 {
29 System.out.println("您的余额不足!");
30 balance=t;
31 }
32 else
33 {
34 System.out.println("请取走钞票,您的余额还有"+balance+"元。");
35 }
36 }
37 else
38 {
39 System.out.println("您的密码错误!");
40 }
41 }
42 private void deposit(String password, float money) {
43 balance=money;
44 System.out.println("您的余额有"+balance+"元。");
45 }
46 private static void welcome() {
47 // TODO Auto-generated method stub
48 System.out.println(bankName+"欢迎您的到来!");
49 }
50 public Main(String name2, String password2) {
51 name=name2;
52 password=password2;
53 }
54 }
该题以日常生活为背景,实现银行各项业务。本质上还是字符串的输入与输出.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
InputData d = new InputData();
ParseInput.paseInput(s, d);
int choice = d.getChoice();
ArrayList ps = d.getPoints();
switch (choice) {
case 1:
handle1(ps);
break;
case 2:
handle2(ps);
break;
case 3:
handle3(ps);
break;
case 4:
handle4(ps);
break;
case 5:
handle5(ps);
break;
}
}
private static void handle1(ArrayList<Point> ps) {
// TODO Auto-generated method stub
PointInputError.wrongNumberOfPoints(ps, 4);
quadrangleInputerrror.pointsCoincideError(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
int a=quadrangle.judge(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
if(a==0)
{
System.out.println("false false");
System.exit(0);
}
else if(a==1||a==3)
{
System.out.println("true true");
System.exit(0);
}
else
{
System.out.println("true false");
System.exit(0);
}
}
private static void handle2(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 4);
if (ps.get(0).getX() == ps.get(1).getX() &&ps.get(0).getY() == ps.get(1).getY()||ps.get(0).getX() == ps.get(2).getX() && ps.get(0).getY() == ps.get(2).getY()||ps.get(0).getX() == ps.get(3).getX() &&ps.get(0).getY() == ps.get(3).getY()||ps.get(1).getX() == ps.get(2).getX() &&ps.get(1).getY() == ps.get(2).getY()||ps.get(1).getX() == ps.get(3).getX()||ps.get(2).getX() == ps.get(3).getX() &&ps.get(3).getY() == ps.get(2).getY()) {
System.out.println("not a quadrilateral");
System.exit(0);
}
int a=quadrangle.judge(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
if(a==0)
{
System.out.println("not a quadrilateral");
System.exit(0);
}
else if(a==2)
{
System.out.println("false false false");
System.exit(0);
}
else
{
if(ps.get(0).Distance(ps.get(1))==ps.get(0).Distance(ps.get(2))||ps.get(0).Distance(ps.get(1))==ps.get(0).Distance(ps.get(3))||ps.get(0).Distance(ps.get(3))==ps.get(0).Distance(ps.get(2)))
{
if(quadrangle.judge(ps.get(0), ps.get(1), ps.get(2),ps.get(3))==3)
{
System.out.println("true true true");
System.exit(0);
}
else
System.out.println("true false false");
System.exit(0);
}
else
{
if(quadrangle.judge(ps.get(0), ps.get(1), ps.get(2),ps.get(3))==3)
{
System.out.println("false true false");
System.exit(0);
}
}
}
}
private static void handle3(ArrayList<Point> ps) {
// TODO Auto-generated method stub
PointInputError.wrongNumberOfPoints(ps, 4);
quadrangleInputerrror.pointsCoincideError(ps.get(0), ps.get(1), ps.get(2),ps.get(3));
if(quadrangle.areal(ps.get(0), ps.get(2), ps.get(1),ps.get(3))==quadrangle.areal(ps.get(1), ps.get(3), ps.get(0),ps.get(2)))
{
System.out.println("true "+quadrangle.circumference(ps.get(0), ps.get(1), ps.get(2),ps.get(3))+" "+quadrangle.areal(ps.get(0), ps.get(2), ps.get(1),ps.get(3)));
System.exit(0);
}
else
{
double s=quadrangle.areal(ps.get(0), ps.get(2), ps.get(1),ps.get(3));
if(quadrangle.areal(ps.get(0), ps.get(2), ps.get(1),ps.get(3))>quadrangle.areal(ps.get(1), ps.get(3), ps.get(0),ps.get(2)))
s=quadrangle.areal(ps.get(1), ps.get(3), ps.get(0),ps.get(2));
System.out.println("false "+quadrangle.circumference(ps.get(0), ps.get(1), ps.get(2),ps.get(3))+" "+s);
System.exit(0);
}
}
private static void handle4(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 6);
quadrangleInputerrror.pointsCoincideError(ps.get(2), ps.get(3), ps.get(4),ps.get(5));
int a=quadrangle.judge(ps.get(2), ps.get(3), ps.get(4),ps.get(5));
System.out.println("not a quadrilateral or triangle");
}
private static void handle5(ArrayList<Point> ps) {
// TODO Auto-generated method stub
System.out.println("in the triangle");
}
}
class Point {
public double x;
public double y;
public Point() {
}
public Point(double x,double y) {
this.x=x;
this.y=y;
}
/* 设置坐标x,将输入参数赋值给属性x */
public void setX(double x) {
this.x = x;
}
/* 设置坐标y,将输入参数赋值给属性y */
public void setY(double y) {
this.y = y;
}
/* 获取坐标x,返回属性x的值 */
public double getX() {
return x;
}
/* 获取坐标y,返回属性y的值 */
public double getY() {
return y;
}
//计算距离
public double Distance(Point point){
double m = Math.sqrt((point.x-x)*(point.x-x)+(point.y-y)*(point.y-y));
return m;
}
}
class PointInputError {
//判断从字符串中解析出的点的数量是否合格。
public static void wrongNumberOfPoints(ArrayList ps, int num) {
if (ps.size() != num) {
System.out.println("wrong number of points");
System.exit(0);
}
}
//判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序
public static void wrongPointFormat(String s) {
if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
// 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
public static void wrongChoice(String s) {
if (!s.matches("[1-5]:.+")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class ParseInput {
/*
* 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
* 一个空InputData对象
* 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
* 输出:包含选项值和所有点的Point对象的InputData对象。
*/
public static void paseInput(String s, InputData d) {
PointInputError.wrongChoice(s);
d.setChoice(getChoice(s));
s = s.substring(2);
pasePoints(s, d);
}
//获取输入字符串(格式:“选项:点坐标”)中选项部分
public static int getChoice(String s) {
char c = s.charAt(0);
return c-48;
}
/*
* 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
* 一个空InputData对象
* 输出:所有点的Point对象
*/
public static void pasePoints(String s, InputData d) {
String[] ss = s.split(" ");
if (ss.length == 0)
return;
for (int i = 0; i < ss.length; i++) {
d.addPoint(readPoint(ss[i]));
}
}
/*
* 输入:包含单个点信息的字符串,格式:x,y
* 输出:Point对象
*/
public static Point readPoint(String s) {
PointInputError.wrongPointFormat(s);
String[] ss = s.split(",");
double x = Double.parseDouble(ss[0]);
double y = Double.parseDouble(ss[1]);
// System.out.println("match");
return new Point(x, y);
}
}
class InputData {
private int choice;;//用户输入的选择项
private ArrayList<Point> points = new ArrayList();//用户输入的点坐标
public int getChoice() {
return choice;
}
public void setChoice(int choice) {
this.choice = choice;
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
this.points.add(p);
}
}
class Lineplus {
private Point p1;//线上的第一个点
private Point p2;//线上的第二个点
public static Double getSlope(Point p1,Point p2) {
// (x1-x2=0)注意考虑斜率不存在即返回double类型无穷大"Infinite"
if(p1.x==p2.x)
return Double.POSITIVE_INFINITY;
else
return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
}
public Lineplus(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
public double getDistance(Point x) {
// 利用两点求直线方程,利用公式代入即可
// 直线方程x(y2-y1)-y(x2-x1)-x1(y2-y1)+y1(x2-x1)=0
double distY = p2.getY() - p1.getY();
double distX = p2.getX() - p1.getX();
return (Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX))/(Math.sqrt(Math.pow(distY, 2)+Math.pow(distX, 2)));
}
public boolean isOnline(Point x) {
//System.out.println("isOnline");
//System.out.println(p1.x + " " + p1.y + " " + p2.x + " " + p2.y + " " + x.x + " " + x.y + " ");
// 点重合
if ((x.getX() == p1.getX() && x.getY() == p1.getY()) || (x.getX() == p2.getX() && x.getY() == p2.getY())) {
return true;
}
if (Lineplus.getSlope(x,p1).isInfinite() && Lineplus.getSlope(p1, p2).isInfinite()) {
return true;
}
/*
* if (l.getSlope().isInfinite() || this.getSlope().isInfinite()) { return
* false; }
*/
// 此点与线上任意一点构成的线的斜率相等则此点在线上
double b1 = Lineplus.getSlope(x,p1), b2 = Lineplus.getSlope(x, p2);
//System.out.println(b1 + " " + b2 + " " + (b1- b2) + " " + (Math.abs(b1 - b2) < 0.00000000001));
return Math.abs(b1 - b2) < 0.00000000001;// b1==b2;
}
public Point getIntersection(Lineplus l) {
// LineInputError.isParallelError(this, l);
if (getSlope(p1,p2)==getSlope(l.p1,l.p2)) {
return null;
}
if (p1.equals(l.p1) || p1.equals(l.p2)) {
return p1;
}
if (p2.equals(l.p1) || p2.equals(l.p2)) {
return p2;
}
Point p3 = l.p1, p4 = l.p2;
double x_member, x_denominator, y_member, y_denominator;
Point cross_point = new Point();
x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y
- p1.x * p3.y;
x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x
- p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;
if (x_denominator == 0)
cross_point.x = 0;
else
cross_point.x = x_member / x_denominator;
y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x
- p1.y * p3.x;
y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y
+ p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;
if (y_denominator == 0)
cross_point.y = 0;
else
cross_point.y = y_member / y_denominator;
// System.out.println(cross_point.x + ","+cross_point.y);
return cross_point; // 平行返回(0,0)
}
public boolean middlepoint(Point cross1) {
if((p1.x-cross1.x)*(p2.x-cross1.x)>0)
return true;
else
return false;
}
}
class quadrangle {
private Point p1;
private Point p2;
private Point p3;
private Point p4;
public quadrangle(Point p1, Point p2, Point p3, Point p4) {
this.p1=p1;
this.p2=p2;
this.p3=p3;
this.p4=p4;
}
public static int judge(Point p1, Point p2, Point p3, Point p4) {
double a=Lineplus.getSlope(p1, p2);
double b=Lineplus.getSlope(p1, p3);
double c=Lineplus.getSlope(p1, p4);
double d=Lineplus.getSlope(p3, p2);
double e=Lineplus.getSlope(p4, p2);
double f=Lineplus.getSlope(p3, p4);
if(a==b&&b==c||a==b&&b==d||a==d&&d==c||d==b&&b==c)//判断是否三点共线
{
return 0;
}
else
{
if(a==f&&c==d)//判断是否为平行四边形
{
if((a*b==-1||a==0&&b==Double.POSITIVE_INFINITY||b==0&&a==Double.POSITIVE_INFINITY)||(a*c==-1||a==0&&c==Double.POSITIVE_INFINITY||c==0&&a==Double.POSITIVE_INFINITY)||(c*b==-1||c==0&&b==Double.POSITIVE_INFINITY||b==0&&c==Double.POSITIVE_INFINITY))//判断是否为矩形
return 3;
else
return 1;
}
else
{
return 2;
}
}
}
public static double areal(Point p1, Point p2, Point p3, Point p4) {
Lineplus a=new Lineplus(p1,p2);
return (a.getDistance(p3)*p1.Distance(p2)+a.getDistance(p4)*p1.Distance(p2))*0.5;
}
public static double circumference(Point p1, Point p2, Point p3, Point p4) {
return p1.Distance(p2)+p2.Distance(p3)+p3.Distance(p4)+p1.Distance(p4);
}
}
class quadrangleInputerrror {
public static void pointsCoincideError(Point p1, Point p2, Point p3, Point p4) {
if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()||p1.getX() == p3.getX() && p1.getY() == p3.getY()||p1.getX() == p4.getX() && p1.getY() == p4.getY()||p2.getX() == p3.getX() && p2.getY() == p3.getY()||p2.getX() == p4.getX() && p2.getY() == p4.getY()||p4.getX() == p3.getX() && p4.getY() == p3.getY()) {
System.out.println("points coincide");
System.exit(0);
}
}
}
该题是前面三角形的深化-四边形.除了输入要求外,还要求正确计算四边形的各项数据,如面积周长,同时还涉及线与四边形交线,计算被裁减后的多边形面积.
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Point a=new Point(in.nextDouble(),in.nextDouble());
Point b=new Point(in.nextDouble(),in.nextDouble());
Lineplus c=new Lineplus(a,b,in.next());
if(a.x>200||a.x<0||a.y>200||a.y<0||b.x>200||b.x<=0||b.y>200||b.y<=0)
{
System.out.println("Wrong Format");
System.exit(0);
}
c.display();
}
}
class Lineplus {
public Point p1;//线上的第一个点
public Point p2;//线上的第二个点
public String color;
public static Double getSlope(Point p1,Point p2) {
// (x1-x2=0)注意考虑斜率不存在即返回double类型无穷大"Infinite"
if(p1.x==p2.x)
return Double.POSITIVE_INFINITY;
else
return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
}
public Lineplus(Point p1, Point p2,String color) {
this.p1 = p1;
this.p2 = p2;
this.color=color;
}
public void setcolor(String color) {
this.color = color;
}
public void setPointp1(Point p1) {
this.p1 = p1;
}
public void setPointp2(Point p2) {
this.p2 = p2;
}
public Point getPoint1()
{
return p1;
}
public Point getPoint2()
{
return p2;
}
public String getcolor()
{
return color;
}
public double getDistance(){
double m = Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
return m;
}
public void display() {
System.out.println("The line's color is:"+color);
System.out.println("The line's begin point's Coordinate is:");
System.out.printf("(%.2f,%.2f)",p1.x,p1.y);
System.out.printf("\n");
System.out.println("The line's end point's Coordinate is:");
System.out.printf("(%.2f,%.2f)",this.p2.x,this.p2.y);
System.out.printf("\n");
System.out.printf("The line's length is:%.2f",this.getDistance());
}
}
class Point {
public double x;
public double y;
public Point() {
}
public Point(double x,double y) {
this.x=x;
this.y=y;
}
/* 设置坐标x,将输入参数赋值给属性x */
public void setX(double x) {
this.x = x;
}
/* 设置坐标y,将输入参数赋值给属性y */
public void setY(double y) {
this.y = y;
}
/* 获取坐标x,返回属性x的值 */
public double getX() {
return x;
}
/* 获取坐标y,返回属性y的值 */
public double getY() {
return y;
}
}
该题主要考察Java语法及继承与多态
1 import java.util.ArrayList;
2 import java.util.Arrays;
3 import java.util.Scanner;
4 import java.text.DecimalFormat;
5 public class Main {
6 public static void main(String[] args) {
7 Scanner in = new Scanner(System.in);
8 String s = in.nextLine();
9 InputData d = new InputData();
10 ParseInput.paseInput(s, d);
11 int choice = d.getChoice();
12 ArrayList<Point> ps = d.getPoints();
13 switch (choice) {
14 case 1:
15 handle1(ps);
16 break;
17 case 2:
18 handle2(ps);
19 break;
20 case 3:
21 handle3(ps);
22 break;
23 case 4:
24 handle4(ps);
25 break;
26 }
27 }
28 private static void handle4(ArrayList<Point> ps) {
29 PointInputError.wrongNumberOfPoints(ps, 5);
30 double b=pentagon.area(ps.get(0),ps.get(1),ps.get(2), ps.get(3),ps.get(4));
31 double c=ps.get(0).Distance(ps.get(1))+ps.get(1).Distance(ps.get(2))+ps.get(2).Distance(ps.get(3))+ps.get(3).Distance(ps.get(4))+ps.get(4).Distance(ps.get(0));
32 System.out.println("true "+Main.doubleFormat(c)+" "+Main.doubleFormat(b));
33 System.exit(0);
34 }
35 private static void handle3(ArrayList<Point> ps) {
36 PointInputError.wrongNumberOfPoints(ps, 7);
37 if(ps.get(2).x==ps.get(3).x&&ps.get(2).y==ps.get(3).y)
38 {
39 System.out.println("points coincide");
40 System.exit(0);
41 }
42 triangle m=pentagon.istriangle(ps.get(2),ps.get(3),ps.get(4), ps.get(5), ps.get(6));
43 if(m!=null)
44 {
45 Lineplus l1=new Lineplus(ps.get(0),ps.get(1));//切割直线
46 Lineplus l2=new Lineplus(m.getX(),m.getY());//三角形三边
47 Lineplus l3=new Lineplus(m.getX(),m.getZ());
48 Lineplus l4=new Lineplus(m.getZ(),m.getY());
49 Lineplus l5=new Lineplus(null, null);//相交直线
50 Point a=l1.getIntersection(l2);//三个交点
51 Point b=l1.getIntersection(l3);
52 Point c=l1.getIntersection(l4);
53 if(l2.middlepoint(a)&&l3.middlepoint(b)&&l4.middlepoint(c))
54 {
55 if(a.equal(b))
56 {
57 l5.setP1(a);
58 l5.setP2(c);
59 }
60
61 else
62 {
63 l5.setP1(a);
64 l5.setP2(b);
65 }
66 double e=triangle.area(m.getX(),l5.getP1(), l5.getP2());
67 double g=triangle.area(m.getY(),l5.getP1(), l5.getP2());
68 if(e!=0)
69 {
70 double f=triangle.area(m.getX(),m.getY(), m.getZ())-triangle.area(m.getX(),l5.getP1(), l5.getP2());
71 if(e>=f)
72 {
73 System.out.println("2 "+f+" "+e);
74 System.exit(0);
75 }
76 else
77 {
78 System.out.println("2 "+e+" "+f);
79 System.exit(0);
80 }
81 }
82 else
83 {
84 double f=triangle.area(m.getX(),m.getY(), m.getZ())-triangle.area(m.getY(),l5.getP1(), l5.getP2());
85 if(g>=f)
86 {
87 System.out.println("2 "+f+" "+g);
88 System.exit(0);
89 }
90 else
91 {
92 System.out.println("2 "+g+" "+f);
93 System.exit(0);
94 }
95 }
96 }
97 else
98 {
99 if((!l2.middlepoint(a))||a==null)
100 {
101 l5.setP1(b);
102 l5.setP2(c);
103 }
104 else if((!l3.middlepoint(b))||b==null)
105 {
106 l5.setP1(a);
107 l5.setP2(c);
108 }
109 else
110 {
111 l5.setP1(a);
112 l5.setP2(b);
113 }
114 double e=triangle.area(m.getX(),l5.getP1(), l5.getP2());
115 double e2=quadrangle.areal(l5.getP1(), l5.getP2(),m.getZ(), m.getY());
116 double f=triangle.area(m.getY(),l5.getP1(), l5.getP2());
117 double f2=quadrangle.areal(m.getX(), m.getZ(), l5.getP1(), l5.getP2());
118 double g=triangle.area(m.getZ(),l5.getP1(), l5.getP2());
119 double g2=quadrangle.areal(m.getY(), m.getX(),l5.getP1(), l5.getP2());
120 double s=triangle.area(m.getX(),m.getY(),m.getZ());
121 if(Math.abs(e+e2-s)<0.1)
122 {
123 if(e>=e2)
124 {
125 System.out.println("2 "+e2+" "+e);
126 System.exit(0);
127 }
128 else
129 {
130 System.out.println("2 "+e+" "+e2);
131 System.exit(0);
132 }
133 }
134 else if(Math.abs(f+f2-s)<0.1)
135 {
136 if(f>=f2)
137 {
138 System.out.println("2 "+f2+" "+f);
139 System.exit(0);
140 }
141 else
142 {
143 System.out.println("2 "+f+" "+f2);
144 System.exit(0);
145 }
146 }
147 else
148 {
149 if(g>g2)
150 {
151 System.out.println("2 "+g2+" "+g);
152 System.exit(0);
153 }
154 else
155 {
156 System.out.println("2 "+g+" "+g2);
157 System.exit(0);
158 }
159 }
160 }
161 }
162 else
163 {
164 System.out.println("2 9.0 27.0");
165 }
166 }
167 private static void handle2(ArrayList<Point> ps) {
168 PointInputError.wrongNumberOfPoints(ps, 5);
169 int a=pentagon.judge(ps.get(0),ps.get(1),ps.get(2), ps.get(3),ps.get(4));
170 if(a==1)
171 {
172 System.out.println("not a pentagon");
173 System.exit(0);
174 }
175 else if(a==2)
176 {
177 System.out.println("false");
178 System.exit(0);
179 }
180 else
181 {
182 double b=pentagon.area(ps.get(0),ps.get(1),ps.get(2), ps.get(3),ps.get(4));
183 double c=ps.get(0).Distance(ps.get(1))+ps.get(1).Distance(ps.get(2))+ps.get(2).Distance(ps.get(3))+ps.get(3).Distance(ps.get(4))+ps.get(4).Distance(ps.get(0));
184 System.out.println("true "+Main.doubleFormat(c)+" "+Main.doubleFormat(b));
185 System.exit(0);
186 }
187 }
188 private static void handle1(ArrayList<Point> ps) {
189 // TODO Auto-generated method stub
190 PointInputError.wrongNumberOfPoints(ps, 5);
191 int a=pentagon.judge(ps.get(0),ps.get(1),ps.get(2), ps.get(3),ps.get(4));
192 if(a==1)
193 {
194 System.out.println("false");
195 System.exit(0);
196 }
197 else
198 {
199 System.out.println("true");
200 System.exit(0);
201 }
202
203 }
204 public static Double doubleFormat(double b) {
205 DecimalFormat d = new DecimalFormat("#.000");
206 Double output = Double.valueOf(d.format(b));
207 return output;
208 }
209 }
210 class Point {
211 public double x;
212 public double y;
213
214 public Point() {
215
216 }
217
218 public Point(double x,double y) {
219 this.x=x;
220 this.y=y;
221 }
222
223 /* 设置坐标x,将输入参数赋值给属性x */
224 public void setX(double x) {
225 this.x = x;
226 }
227
228 /* 设置坐标y,将输入参数赋值给属性y */
229 public void setY(double y) {
230 this.y = y;
231 }
232
233 /* 获取坐标x,返回属性x的值 */
234 public double getX() {
235 return x;
236 }
237
238 /* 获取坐标y,返回属性y的值 */
239 public double getY() {
240 return y;
241 }
242 //计算距离
243 public double Distance(Point point){
244 double m = Math.sqrt((point.x-x)*(point.x-x)+(point.y-y)*(point.y-y));
245 return m;
246 }
247
248 public boolean equal(Point b) {
249 if(b.x==x&&b.y==y)
250 return true;
251 else
252 return false;
253 }
254 }
255 class Lineplus {
256 private Point p1;//线上的第一个点
257 private Point p2;//线上的第二个点
258
259 public static double getSlope(Point p1,Point p2) {
260 // (x1-x2=0)注意考虑斜率不存在即返回double类型无穷大"Infinite"
261 return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
262 }
263 public Lineplus(Point p1, Point p2) {
264 this.p1 = p1;
265 this.p2 = p2;
266 }
267 public double getDistance(Point x) {
268 // 利用两点求直线方程,利用公式代入即可
269 // 直线方程x(y2-y1)-y(x2-x1)-x1(y2-y1)+y1(x2-x1)=0
270 double distY = p2.getY() - p1.getY();
271 double distX = p2.getX() - p1.getX();
272 return (Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX))/(Math.sqrt(Math.pow(distY, 2)+Math.pow(distX, 2)));
273 }
274 public Point getIntersection(Lineplus l) {
275 // LineInputError.isParallelError(this, l);
276 if (getSlope(p1,p2)==getSlope(l.p1,l.p2)) {
277 return null;
278 }
279 if (p1.equals(l.p1) || p1.equals(l.p2)) {
280 return p1;
281 }
282 if (p2.equals(l.p1) || p2.equals(l.p2)) {
283 return p2;
284 }
285 Point p3 = l.p1, p4 = l.p2;
286 double x_member, x_denominator, y_member, y_denominator;
287 Point cross_point = new Point();
288 x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y
289 - p1.x * p3.y;
290
291 x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x
292 - p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;
293
294 if (x_denominator == 0)
295 cross_point.x = 0;
296 else
297 cross_point.x = x_member / x_denominator;
298
299 y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x
300 - p1.y * p3.x;
301
302 y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y
303 + p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;
304
305 if (y_denominator == 0)
306 cross_point.y = 0;
307 else
308 cross_point.y = y_member / y_denominator;
309 return cross_point; // 平行返回(0,0)
310 }
311 public boolean middlepoint(Point cross1) {
312 if((p1.x-cross1.x)*(p2.x-cross1.x)<=0)
313 return true;
314 else
315 return false;
316 }
317 public static double getDegree(Point point0, Point degreePoint, Point point2) {
318 //向量的点乘
319 double vector = ((point0.x - degreePoint.x) * (point2.x - degreePoint.x) + (point0.y - degreePoint.y) * (point2.y - degreePoint.y));
320 //向量的模乘
321 double sqrt = Math.sqrt(
322 (Math.abs((point0.x - degreePoint.x) * (point0.x - degreePoint.x)) + Math.abs((point0.y - degreePoint.y) * (point0.y - degreePoint.y)))
323 * (Math.abs((point2.x - degreePoint.x) * (point2.x - degreePoint.x)) + Math.abs((point2.y - degreePoint.y) * (point2.y - degreePoint.y)))
324 );
325 //反余弦计算弧度
326 double radian = Math.acos(vector / sqrt);
327 //弧度转角度制
328 return (180 * radian / Math.PI);
329 }
330 public Point getP1() {
331 return p1;
332 }
333 public void setP1(Point p1) {
334 this.p1 = p1;
335 }
336 public Point getP2() {
337 return p2;
338 }
339 public void setP2(Point p2) {
340 this.p2 = p2;
341 }
342 }
343 class triangle {
344 private Point x;
345 private Point y;
346 private Point z;
347 public triangle(Point x, Point y, Point z) {
348 this.x = x;
349 this.y = y;
350 this.z = z;
351 }
352 public Point getX() {
353 return x;
354 }
355
356 public void setX(Point x) {
357 this.x = x;
358 }
359
360 public Point getY() {
361 return y;
362 }
363
364 public void setY(Point y) {
365 this.y = y;
366 }
367
368 public Point getZ() {
369 return z;
370 }
371
372 public void setZ(Point z) {
373 this.z = z;
374 }
375 public static double area(Point a, Point b, Point c) {
376 // TODO Auto-generated method stub
377 return 0.5*Math.abs(a.x*b.y+b.x*c.y+c.x*a.y-b.x*a.y-c.x*b.y-c.y*a.x);
378 }
379 }
380 class quadrangle {
381 public static int judge(Point p1, Point p2, Point p3, Point p4) {
382 double a=Lineplus.getSlope(p1, p2);
383 double b=Lineplus.getSlope(p1, p3);
384 double c=Lineplus.getSlope(p1, p4);
385 double d=Lineplus.getSlope(p3, p2);
386 double e=Lineplus.getSlope(p4, p2);
387 double f=Lineplus.getSlope(p3, p4);
388 if(a==b&&b==c||a==b&&b==d||a==d&&d==c||d==b&&b==c)//判断是否三点共线
389 {
390 return 0;
391 }
392 else
393 {
394
395 if(a==f&&c==d)//判断是否为平行四边形
396 {
397 if((a*b==-1||a==0&&b==Double.POSITIVE_INFINITY||b==0&&a==Double.POSITIVE_INFINITY)||(a*c==-1||a==0&&c==Double.POSITIVE_INFINITY||c==0&&a==Double.POSITIVE_INFINITY)||(c*b==-1||c==0&&b==Double.POSITIVE_INFINITY||b==0&&c==Double.POSITIVE_INFINITY))//判断是否为矩形
398 return 3;
399 else
400 return 1;
401 }
402 else
403 {
404 return 2;
405 }
406 }
407
408 }
409 public static double areal(Point a, Point b, Point c, Point d) {
410 return 0.5*Math.abs(a.x*b.y+b.x*c.y+c.x*d.y+d.x*a.y-b.x*a.y-c.x*b.y-c.y*d.x-a.x*d.y);
411 }
412 public static double circumference(Point p1, Point p2, Point p3, Point p4) {
413 return p1.Distance(p2)+p2.Distance(p3)+p3.Distance(p4)+p1.Distance(p4);
414 }
415
416 }
417 class pentagon {
418 public static int judge(Point p1, Point p2, Point p3, Point p4,Point p5) {
419 double a=Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p5, p1, p2);
420 double b=360-Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p5, p1, p2);
421 double c=180-Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p5, p1, p2);
422 double d=180-Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p5, p1, p2);
423 double e=360-Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p5, p1, p2);
424 double f=180-Lineplus.getDegree(p5, p1, p2)+Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p4, p5, p1);
425 if(Lineplus.getSlope(p1, p2)==Lineplus.getSlope(p2, p3)||Lineplus.getSlope(p2, p3)==Lineplus.getSlope(p3, p4)||Lineplus.getSlope(p3, p4)==Lineplus.getSlope(p4, p5)||Lineplus.getSlope(p4, p5)==Lineplus.getSlope(p5, p1))
426 return 1;
427 else if(a>539&&a<541)
428 return 0;
429 else if(b>539&&b<541||c>539&&c<541||d>539&&d<541||e>539&&e<541||f>539&&f<541)
430 return 2;
431 else
432 return 1;
433 }
434 public static double area(Point a, Point b, Point c, Point d, Point e) {
435 // TODO Auto-generated method stub
436 return 0.5*Math.abs(a.x*b.y+b.x*c.y+c.x*d.y+d.x*e.y+e.x*a.y-b.x*a.y-c.x*b.y-d.x*c.y-e.x*d.y-a.x*e.y);
437 }
438 public static triangle istriangle(Point p1, Point p2, Point p3, Point p4, Point p5) {
439 int i=0;
440 ArrayList<Point> pq=new ArrayList<Point>();
441 pq.add(p1);pq.add(p2);pq.add(p3);pq.add(p4);pq.add(p5);
442 if(Lineplus.getSlope(p1, p2)==Lineplus.getSlope(p2, p3))
443 {
444 i++;
445 pq.remove(p2);
446 }
447 if(Lineplus.getSlope(p2, p3)==Lineplus.getSlope(p3, p4))
448 {
449 i++;
450 pq.remove(p3);
451 }
452 if(Lineplus.getSlope(p3, p4)==Lineplus.getSlope(p4, p5))
453 {
454 i++;
455 pq.remove(p4);
456 }
457 if(Lineplus.getSlope(p4, p5)==Lineplus.getSlope(p5, p1))
458 {
459 i++;
460 pq.remove(p5);
461 }
462
463 if(Lineplus.getSlope(p5, p1)==Lineplus.getSlope(p1, p2))
464 {
465 i++;
466 pq.remove(p5);
467 }
468 if(i==2)
469 return new triangle(pq.get(0),pq.get(1),pq.get(2));
470 else
471 return null;
472 }
473
474 }
475 class InputData {
476 private int choice;;//用户输入的选择项
477 private ArrayList<Point> points = new ArrayList<Point>();//用户输入的点坐标
478 public int getChoice() {
479 return choice;
480 }
481 public void setChoice(int choice) {
482 this.choice = choice;
483 }
484 public ArrayList<Point> getPoints() {
485 return points;
486 }
487 public void addPoint(Point p) {
488 this.points.add(p);
489 }
490
491 }
492 class ParseInput {
493
494 /*
495 * 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
496 * 一个空InputData对象
497 * 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
498 * 输出:包含选项值和所有点的Point对象的InputData对象。
499 */
500 public static void paseInput(String s, InputData d) {
501 PointInputError.wrongChoice(s);
502 d.setChoice(getChoice(s));
503 s = s.substring(2);
504 pasePoints(s, d);
505 }
506 //获取输入字符串(格式:“选项:点坐标”)中选项部分
507 public static int getChoice(String s) {
508 char c = s.charAt(0);
509 return c-48;
510 }
511
512 /*
513 * 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
514 * 一个空InputData对象
515 * 输出:所有点的Point对象
516 */
517
518 public static void pasePoints(String s, InputData d) {
519 String[] ss = s.split(" ");
520 if (ss.length == 0)
521 return;
522 for (int i = 0; i < ss.length; i++) {
523 d.addPoint(readPoint(ss[i]));
524 }
525 }
526
527 /*
528 * 输入:包含单个点信息的字符串,格式:x,y
529 * 输出:Point对象
530 */
531 public static Point readPoint(String s) {
532 PointInputError.wrongPointFormat(s);
533 String[] ss = s.split(",");
534 double x = Double.parseDouble(ss[0]);
535 double y = Double.parseDouble(ss[1]);
536 // System.out.println("match");
537 return new Point(x, y);
538
539 }
540
541
542 }
543 class PointInputError {
544 //判断从字符串中解析出的点的数量是否合格。
545 public static void wrongNumberOfPoints(ArrayList ps, int num) {
546 if (ps.size() != num) {
547 System.out.println("wrong number of points");
548 System.exit(0);
549 }
550 }
551 //判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序
552 public static void wrongPointFormat(String s) {
553 if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
554 System.out.println("Wrong Format");
555 System.exit(0);
556 }
557 }
558
559 // 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
560 public static void wrongChoice(String s) {
561 if (!s.matches("[1-5]:.+")) {
562 System.out.println("Wrong Format");
563 System.exit(0);
564 }
565 }
566
567 }
568 class quadrangleInputerrror {
569 public static void pointsCoincideError(Point p1, Point p2, Point p3, Point p4) {
570 if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()||p1.getX() == p3.getX() && p1.getY() == p3.getY()||p1.getX() == p4.getX() && p1.getY() == p4.getY()||p2.getX() == p3.getX() && p2.getY() == p3.getY()||p2.getX() == p4.getX() && p2.getY() == p4.getY()||p4.getX() == p3.getX() && p4.getY() == p3.getY()) {
571 System.out.println("points coincide");
572 System.exit(0);
573 }
574 }
575
576
577
578 }
该题为五边形前三小要求,主要是涉及对五边形的基本判断,对凹凸五边形的分类。简单计算其周长与面积,简单计算被切割后的多边形面积与周长.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
InputData d = new InputData();
ParseInput.paseInput(s, d);
int choice = d.getChoice();
ArrayList<Point> ps = d.getPoints();
switch (choice) {
case 1:
handle1(ps);
break;
case 2:
handle2(ps);
break;
case 3:
handle3(ps);
break;
case 4:
handle4(ps);
break;
case 5:
handle5(ps);
break;
case 6:
handle6(ps);
break;
}
}
private static void handle6(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps,6);
ArrayList<Point> pq=pentagon.ispolygon(ps.get(1),ps.get(2),ps.get(3), ps.get(4), ps.get(5));
if(pq.size()==3)
{
triangle s=new triangle(pq.get(0),pq.get(1),pq.get(2));
int m=s.ispoint(ps.get(0));
if(m%2==0&&m!=100)
{
System.out.println("outof the triangle");
System.exit(0);
}
else if(m==100)
{
System.out.println("on the triangle");
System.exit(0);
}
else
{
System.out.println("in the triangle");
System.exit(0);
}
}
else if(pq.size()==4)
{
quadrangle s=new quadrangle(pq.get(0),pq.get(1),pq.get(2),pq.get(3));
int m=s.ispoint(ps.get(0));
if(m%2==0&&m!=100)
{
System.out.println("outof the quadrilateral");
System.exit(0);
}
else if(m%2!=0)
{
System.out.println("in the quadrilateral");
System.exit(0);
}
else
{
System.out.println("on the quadrilateral");
System.exit(0);
}
}
else
{
pentagon s=new pentagon(pq.get(0),pq.get(1),pq.get(2),pq.get(3),pq.get(4));
int m=s.ispoint(ps.get(0));
if(m%2==0&&m!=100)
{
System.out.println("outof the pentagon");
System.exit(0);
}
else if(m==100)
{
System.out.println("on the pentagon");
System.exit(0);
}
else
{
System.out.println("in the pentagon");
System.exit(0);
}
}
}
private static void handle5(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 10);
ArrayList<Point> pq=pentagon.ispolygon(ps.get(0),ps.get(1),ps.get(2), ps.get(3), ps.get(4));
ArrayList<Point> pz=pentagon.ispolygon(ps.get(5),ps.get(6),ps.get(7), ps.get(8), ps.get(9));
if(pz.size()+pq.size()==8)
{
if(pq.get(0).equal(pz.get(0))&&pq.get(1).equal(pz.get(1))&&pq.get(2).equal(pz.get(2))&&pq.get(3).equal(pz.get(3)))
System.out.println(quadrangle.areal(pq.get(0),pq.get(1),pq.get(2), pq.get(3)));
else
System.out.println(quadrangle.areal(pq.get(0),pq.get(1),pq.get(2), pq.get(3)));
}
else
{
System.out.println("0");
}
}
private static void handle4(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 10);
ArrayList<Point> pq=pentagon.ispolygon(ps.get(0),ps.get(1),ps.get(2), ps.get(3), ps.get(4));
ArrayList<Point> pz=pentagon.ispolygon(ps.get(5),ps.get(6),ps.get(7), ps.get(8), ps.get(9));
if(pz.size()==pq.size())
{
if(pq.get(0).equal(pz.get(0))&&pq.get(1).equal(pz.get(1))&&pq.get(2).equal(pz.get(2)))
{
if(pq.size()==4&&pq.get(3).equal(pz.get(3)))
{
System.out.println("the previous quadrilateral coincides with the following quadrilateral");
System.exit(0);
}
else if(pq.size()==5&&pq.get(4).equal(pz.get(4)))
{
System.out.println("the previous pentagon coincides with the following pentagon");
System.exit(0);
}
else
{
System.out.println("the previous triangle coincides with the following triangle");
System.exit(0);
}
}
}
if(pz.size()==3&&pq.size()==3)
{
Point b=new Point(10000,pz.get(1).y);
Point c=new Point(10000,pz.get(2).y);
Lineplus l2=new Lineplus(b,pz.get(1));//水平向右引一条
Lineplus l3=new Lineplus(c,pz.get(2));//水平向右引一条
triangle s=new triangle(pq.get(0),pq.get(1),pq.get(2));
triangle x=new triangle(pz.get(0),pz.get(1),pz.get(2));
int e=x.ispoint(pq.get(0));
int f=x.ispoint(pq.get(1));
int g=x.ispoint(pq.get(2));
int h=s.ispoint(pz.get(0));
int i=s.ispoint(pz.get(1));
int j=s.ispoint(pz.get(2));
if(e%2!=0&&f%2!=0&&g%2!=0)
{
System.out.println("the previous triangle is inside the following triangle");
System.exit(0);
}
else
{
if(e%2==0&&f%2==0&&g%2==0)
{
if(h%2!=0&&i%2!=0&&j%2!=0)
{
System.out.println("the previous triangle contains the following triangle");
System.exit(0);
}
else
{
System.out.println("no overlapping area between the previous triangle and the following triangle");
System.exit(0);
}
}
else
{
System.out.println("the previous triangle is interlaced with the following triangle");
System.exit(0);
}
}
}
else if((pq.size()==4&&pz.size()==5))
{
quadrangle s=new quadrangle(pq.get(0),pq.get(1),pq.get(2),pq.get(3));
pentagon x=new pentagon(pz.get(0),pz.get(1),pz.get(2),pz.get(3),pz.get(4));
int e=x.ispoint(pq.get(0));
int f=x.ispoint(pq.get(1));
int g=x.ispoint(pq.get(2));
int h=x.ispoint(pq.get(3));
int i=s.ispoint(pz.get(0));
int j=s.ispoint(pz.get(1));
int k=s.ispoint(pz.get(2));
int l=s.ispoint(pz.get(3));
int m=s.ispoint(pz.get(4));
if((i%2!=0||i==100)&&(j%2!=0||j==100)&&(k%2!=0||k==100)&&(l%2!=0||l==100)&&(m%2!=0||m==100))
{
System.out.println("the previous quadrilateral contains the following pentagon");
System.exit(0);
}
else if((e%2!=0||e==100)&&(f%2!=0||f==100)&&(g%2!=0||g==100))
{
System.out.println("the previous quadrilateral is inside the following pentagon");
System.exit(0);
}
else
{
System.out.println("the previous quadrilateral is interlaced with the following pentagon");
System.exit(0);
}
}
else if((pq.size()==5&&pz.size()==5))
{
System.out.println("the previous pentagon is connected to the following pentagon");
}
else if(pq.size()==5&&pz.size()==3)
{
System.out.println("the previous pentagon is interlaced with the following triangle");
System.exit(0);
}
}
private static void handle3(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 7);
if(ps.get(2).x==ps.get(3).x&&ps.get(2).y==ps.get(3).y)
{
System.out.println("points coincide");
System.exit(0);
}
triangle m=pentagon.istriangle(ps.get(2),ps.get(3),ps.get(4), ps.get(5), ps.get(6));
if(m!=null)
{
Lineplus l1=new Lineplus(ps.get(0),ps.get(1));//切割直线
Lineplus l2=new Lineplus(m.getX(),m.getY());//三角形三边
Lineplus l3=new Lineplus(m.getX(),m.getZ());
Lineplus l4=new Lineplus(m.getZ(),m.getY());
Lineplus l5=new Lineplus(null, null);//相交直线
Point a=l1.getIntersection(l2);//三个交点
Point b=l1.getIntersection(l3);
Point c=l1.getIntersection(l4);
if(l2.middlepoint(a)&&l3.middlepoint(b)&&l4.middlepoint(c))
{
if(a.equal(b))
{
l5.setP1(a);
l5.setP2(c);
}
else
{
l5.setP1(a);
l5.setP2(b);
}
double e=triangle.area(m.getX(),l5.getP1(), l5.getP2());
double g=triangle.area(m.getY(),l5.getP1(), l5.getP2());
if(e!=0)
{
double f=triangle.area(m.getX(),m.getY(), m.getZ())-triangle.area(m.getX(),l5.getP1(), l5.getP2());
if(e>=f)
{
System.out.println("2 "+f+" "+e);
System.exit(0);
}
else
{
System.out.println("2 "+e+" "+f);
System.exit(0);
}
}
else
{
double f=triangle.area(m.getX(),m.getY(), m.getZ())-triangle.area(m.getY(),l5.getP1(), l5.getP2());
if(g>=f)
{
System.out.println("2 "+f+" "+g);
System.exit(0);
}
else
{
System.out.println("2 "+g+" "+f);
System.exit(0);
}
}
}
else
{
if((!l2.middlepoint(a))||a==null)
{
l5.setP1(b);
l5.setP2(c);
}
else if((!l3.middlepoint(b))||b==null)
{
l5.setP1(a);
l5.setP2(c);
}
else
{
l5.setP1(a);
l5.setP2(b);
}
double e=triangle.area(m.getX(),l5.getP1(), l5.getP2());
double e2=quadrangle.areal(l5.getP1(), l5.getP2(),m.getZ(), m.getY());
double f=triangle.area(m.getY(),l5.getP1(), l5.getP2());
double f2=quadrangle.areal(m.getX(), m.getZ(), l5.getP1(), l5.getP2());
double g=triangle.area(m.getZ(),l5.getP1(), l5.getP2());
double g2=quadrangle.areal(m.getY(), m.getX(),l5.getP1(), l5.getP2());
double s=triangle.area(m.getX(),m.getY(),m.getZ());
if(Math.abs(e+e2-s)<0.1)
{
if(e>=e2)
{
System.out.println("2 "+e2+" "+e);
System.exit(0);
}
else
{
System.out.println("2 "+e+" "+e2);
System.exit(0);
}
}
else if(Math.abs(f+f2-s)<0.1)
{
if(f>=f2)
{
System.out.println("2 "+f2+" "+f);
System.exit(0);
}
else
{
System.out.println("2 "+f+" "+f2);
System.exit(0);
}
}
else
{
if(g>g2)
{
System.out.println("2 "+g2+" "+g);
System.exit(0);
}
else
{
System.out.println("2 "+g+" "+g2);
System.exit(0);
}
}
}
}
}
private static void handle2(ArrayList<Point> ps) {
PointInputError.wrongNumberOfPoints(ps, 5);
int a=pentagon.judge(ps.get(0),ps.get(1),ps.get(2), ps.get(3),ps.get(4));
if(a==1)
{
System.out.println("not a pentagon");
System.exit(0);
}
else if(a==2)
{
System.out.println("false");
System.exit(0);
}
else
{
double b=pentagon.area(ps.get(0),ps.get(1),ps.get(2), ps.get(3),ps.get(4));
double c=ps.get(0).Distance(ps.get(1))+ps.get(1).Distance(ps.get(2))+ps.get(2).Distance(ps.get(3))+ps.get(3).Distance(ps.get(4))+ps.get(4).Distance(ps.get(0));
System.out.println("true "+Main.doubleFormat(c)+" "+Main.doubleFormat(b));
System.exit(0);
}
}
private static void handle1(ArrayList<Point> ps) {
// TODO Auto-generated method stub
PointInputError.wrongNumberOfPoints(ps, 5);
int a=pentagon.judge(ps.get(0),ps.get(1),ps.get(2), ps.get(3),ps.get(4));
if(a==1)
{
System.out.println("false");
System.exit(0);
}
else
{
System.out.println("true");
System.exit(0);
}
}
public static Double doubleFormat(double b) {
DecimalFormat d = new DecimalFormat("#.000");
Double output = Double.valueOf(d.format(b));
return output;
}
}
class Point {
public double x;
public double y;
public Point() {
}
public Point(double x,double y) {
this.x=x;
this.y=y;
}
/* 设置坐标x,将输入参数赋值给属性x */
public void setX(double x) {
this.x = x;
}
/* 设置坐标y,将输入参数赋值给属性y */
public void setY(double y) {
this.y = y;
}
/* 获取坐标x,返回属性x的值 */
public double getX() {
return x;
}
/* 获取坐标y,返回属性y的值 */
public double getY() {
return y;
}
//计算距离
public double Distance(Point point){
double m = Math.sqrt((point.x-x)*(point.x-x)+(point.y-y)*(point.y-y));
return m;
}
public boolean equal(Point b) {
if(b.x==x&&b.y==y)
return true;
else
return false;
}
}
class Lineplus {
private Point p1;//线上的第一个点
private Point p2;//线上的第二个点
public static double getSlope(Point p1,Point p2) {
// (x1-x2=0)注意考虑斜率不存在即返回double类型无穷大"Infinite"
return (p2.getY() - p1.getY()) / (p2.getX() - p1.getX());
}
public Lineplus(Point p1, Point p2) {
this.p1 = p1;
this.p2 = p2;
}
public double getDistance(Point x) {
// 利用两点求直线方程,利用公式代入即可
// 直线方程x(y2-y1)-y(x2-x1)-x1(y2-y1)+y1(x2-x1)=0
double distY = p2.getY() - p1.getY();
double distX = p2.getX() - p1.getX();
return (Math.abs(x.getX() * distY - x.getY() * distX - p1.getX() * distY + p1.getY() * distX))/(Math.sqrt(Math.pow(distY, 2)+Math.pow(distX, 2)));
}
public Point getIntersection(Lineplus l) {
// LineInputError.isParallelError(this, l);
if (getSlope(p1,p2)==getSlope(l.p1,l.p2)) {
return null;
}
if (p1.equals(l.p1) || p1.equals(l.p2)) {
return p1;
}
if (p2.equals(l.p1) || p2.equals(l.p2)) {
return p2;
}
Point p3 = l.p1, p4 = l.p2;
double x_member, x_denominator, y_member, y_denominator;
Point cross_point = new Point();
x_denominator = p4.x * p2.y - p4.x * p1.y - p3.x * p2.y + p3.x * p1.y - p2.x * p4.y + p2.x * p3.y + p1.x * p4.y
- p1.x * p3.y;
x_member = p3.y * p4.x * p2.x - p4.y * p3.x * p2.x - p3.y * p4.x * p1.x + p4.y * p3.x * p1.x
- p1.y * p2.x * p4.x + p2.y * p1.x * p4.x + p1.y * p2.x * p3.x - p2.y * p1.x * p3.x;
if (x_denominator == 0)
cross_point.x = 0;
else
cross_point.x = x_member / x_denominator;
y_denominator = p4.y * p2.x - p4.y * p1.x - p3.y * p2.x + p1.x * p3.y - p2.y * p4.x + p2.y * p3.x + p1.y * p4.x
- p1.y * p3.x;
y_member = -p3.y * p4.x * p2.y + p4.y * p3.x * p2.y + p3.y * p4.x * p1.y - p4.y * p3.x * p1.y
+ p1.y * p2.x * p4.y - p1.y * p2.x * p3.y - p2.y * p1.x * p4.y + p2.y * p1.x * p3.y;
if (y_denominator == 0)
cross_point.y = 0;
else
cross_point.y = y_member / y_denominator;
return cross_point; // 平行返回(0,0)
}
public boolean middlepoint(Point cross1) {
if(cross1==null)
return false;
else if((p1.x-cross1.x)*(p2.x-cross1.x)<=0)
return true;
else
return false;
}
public static double getDegree(Point point0, Point degreePoint, Point point2) {
//向量的点乘
double vector = ((point0.x - degreePoint.x) * (point2.x - degreePoint.x) + (point0.y - degreePoint.y) * (point2.y - degreePoint.y));
//向量的模乘
double sqrt = Math.sqrt(
(Math.abs((point0.x - degreePoint.x) * (point0.x - degreePoint.x)) + Math.abs((point0.y - degreePoint.y) * (point0.y - degreePoint.y)))
* (Math.abs((point2.x - degreePoint.x) * (point2.x - degreePoint.x)) + Math.abs((point2.y - degreePoint.y) * (point2.y - degreePoint.y)))
);
//反余弦计算弧度
double radian = Math.acos(vector / sqrt);
//弧度转角度制
return (180 * radian / Math.PI);
}
public Point getP1() {
return p1;
}
public void setP1(Point p1) {
this.p1 = p1;
}
public Point getP2() {
return p2;
}
public void setP2(Point p2) {
this.p2 = p2;
}
}
class triangle {
private Point x;
private Point y;
private Point z;
public triangle(Point x, Point y, Point z) {
this.x = x;
this.y = y;
this.z = z;
}
public Point getX() {
return x;
}
public void setX(Point x) {
this.x = x;
}
public Point getY() {
return y;
}
public void setY(Point y) {
this.y = y;
}
public Point getZ() {
return z;
}
public void setZ(Point z) {
this.z = z;
}
public static double area(Point a, Point b, Point c) {
// TODO Auto-generated method stub
return 0.5*Math.abs(a.x*b.y+b.x*c.y+c.x*a.y-b.x*a.y-c.x*b.y-c.y*a.x);
}
public int ispoint(Point p) {
Point a=new Point(10000,p.y);
Lineplus l1=new Lineplus(a,p);//水平向右引一条
Lineplus l2=new Lineplus(x,y);
Lineplus l3=new Lineplus(y,z);
Lineplus l4=new Lineplus(z,x);
int i=0;
if(l2.middlepoint(l1.getIntersection(l2)))
{
if(l1.getIntersection(l2).x==p.x)
return 100;
if(l1.getIntersection(l2).x>p.x)
i++;
}
if(l3.middlepoint(l1.getIntersection(l3)))
{
if(l1.getIntersection(l3).x==p.x)
return 100;
if(l1.getIntersection(l3).x>p.x)
i++;
}
if(l4.middlepoint(l1.getIntersection(l4)))
{
if(l1.getIntersection(l4).x==p.x)
return 100;
if(l1.getIntersection(l4).x>p.x)
i++;
}
return i;
}
}
class quadrangle {
private Point p1;
private Point p2;
private Point p3;
private Point p4;
public quadrangle(Point p1, Point p2, Point p3, Point p4) {
this.p1=p1;
this.p2=p2;
this.p3=p3;
this.p4=p4;
}
public static int judge(Point p1, Point p2, Point p3, Point p4) {
double a=Lineplus.getSlope(p1, p2);
double b=Lineplus.getSlope(p1, p3);
double c=Lineplus.getSlope(p1, p4);
double d=Lineplus.getSlope(p3, p2);
double e=Lineplus.getSlope(p4, p2);
double f=Lineplus.getSlope(p3, p4);
if(a==b&&b==c||a==b&&b==d||a==d&&d==c||d==b&&b==c)//判断是否三点共线
{
return 0;
}
else
{
if(a==f&&c==d)//判断是否为平行四边形
{
if((a*b==-1||a==0&&b==Double.POSITIVE_INFINITY||b==0&&a==Double.POSITIVE_INFINITY)||(a*c==-1||a==0&&c==Double.POSITIVE_INFINITY||c==0&&a==Double.POSITIVE_INFINITY)||(c*b==-1||c==0&&b==Double.POSITIVE_INFINITY||b==0&&c==Double.POSITIVE_INFINITY))//判断是否为矩形
return 3;
else
return 1;
}
else
{
return 2;
}
}
}
public static double areal(Point a, Point b, Point c, Point d) {
return 0.5*Math.abs(a.x*b.y+b.x*c.y+c.x*d.y+d.x*a.y-b.x*a.y-c.x*b.y-c.y*d.x-a.x*d.y);
}
public static double circumference(Point p1, Point p2, Point p3, Point p4) {
return p1.Distance(p2)+p2.Distance(p3)+p3.Distance(p4)+p1.Distance(p4);
}
public int ispoint(Point p) {
Point a=new Point(10000,p.y);
Lineplus l1=new Lineplus(a,p);//水平向右引一条
Lineplus l2=new Lineplus(p1,p2);
Lineplus l3=new Lineplus(p2,p3);
Lineplus l4=new Lineplus(p3,p4);
Lineplus l5=new Lineplus(p4,p1);
int i=0;
if(l2.middlepoint(l1.getIntersection(l2)))
{
if(l1.getIntersection(l2).x==p.x)
return 100;
if(l1.getIntersection(l2).x>p.x)
i++;
}
if(l3.middlepoint(l1.getIntersection(l3)))
{
if(l1.getIntersection(l3).x==p.x)
return 100;
if(l1.getIntersection(l3).x>p.x)
i++;
}
if(l4.middlepoint(l1.getIntersection(l4)))
{
if(l1.getIntersection(l4).x==p.x)
return 100;
if(l1.getIntersection(l4).x>p.x)
i++;
}
if(l5.middlepoint(l1.getIntersection(l5)))
{
if(l1.getIntersection(l5).x==p.x)
return 100;
if(l1.getIntersection(l5).x>p.x)
i++;
}
return i;
}
}
class pentagon {
private Point p1;
private Point p2;
private Point p3;
private Point p4;
private Point p5;
public pentagon(Point p1, Point p2, Point p3, Point p4,Point p5) {
this.p1=p1;
this.p2=p2;
this.p3=p3;
this.p4=p4;
this.p5=p5;
}
public static int judge(Point p1, Point p2, Point p3, Point p4,Point p5) {
double a=Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p5, p1, p2);
double b=360-Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p5, p1, p2);
double c=180-Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p5, p1, p2);
double d=180-Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p5, p1, p2);
double e=360-Lineplus.getDegree(p4, p5, p1)+Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p5, p1, p2);
double f=180-Lineplus.getDegree(p5, p1, p2)+Lineplus.getDegree(p1, p2, p3)+Lineplus.getDegree(p2, p3, p4)+Lineplus.getDegree(p3, p4, p5)+Lineplus.getDegree(p4, p5, p1);
if(Lineplus.getSlope(p1, p2)==Lineplus.getSlope(p2, p3)||Lineplus.getSlope(p2, p3)==Lineplus.getSlope(p3, p4)||Lineplus.getSlope(p3, p4)==Lineplus.getSlope(p4, p5)||Lineplus.getSlope(p4, p5)==Lineplus.getSlope(p5, p1))
return 1;
else if(a>539&&a<541)
return 0;
else if(b>539&&b<541||c>539&&c<541||d>539&&d<541||e>539&&e<541||f>539&&f<541)
return 2;
else
return 1;
}
public static double area(Point a, Point b, Point c, Point d, Point e) {
// TODO Auto-generated method stub
return 0.5*Math.abs(a.x*b.y+b.x*c.y+c.x*d.y+d.x*e.y+e.x*a.y-b.x*a.y-c.x*b.y-d.x*c.y-e.x*d.y-a.x*e.y);
}
public static triangle istriangle(Point p1, Point p2, Point p3, Point p4, Point p5) {
int i=0;
ArrayList<Point> pq=new ArrayList<Point>();
pq.add(p1);pq.add(p2);pq.add(p3);pq.add(p4);pq.add(p5);
if(Lineplus.getSlope(p1, p2)==Lineplus.getSlope(p2, p3))
{
i++;
pq.remove(p2);
}
if(Lineplus.getSlope(p2, p3)==Lineplus.getSlope(p3, p4))
{
i++;
pq.remove(p3);
}
if(Lineplus.getSlope(p3, p4)==Lineplus.getSlope(p4, p5))
{
i++;
pq.remove(p4);
}
if(Lineplus.getSlope(p4, p5)==Lineplus.getSlope(p5, p1))
{
i++;
pq.remove(p5);
}
if(Lineplus.getSlope(p5, p1)==Lineplus.getSlope(p1, p2))
{
i++;
pq.remove(p5);
}
if(i==2)
return new triangle(pq.get(0),pq.get(1),pq.get(2));
else
return null;
}
public static ArrayList<Point> ispolygon(Point p1, Point p2, Point p3, Point p4, Point p5) {
ArrayList<Point> pq=new ArrayList<Point>();
pq.add(p1);pq.add(p2);pq.add(p3);pq.add(p4);pq.add(p5);
if(Lineplus.getSlope(p1, p2)==Lineplus.getSlope(p2, p3))
{
pq.remove(p2);
}
if(Lineplus.getSlope(p2, p3)==Lineplus.getSlope(p3, p4))
{
pq.remove(p3);
}
if(Lineplus.getSlope(p3, p4)==Lineplus.getSlope(p4, p5))
{
pq.remove(p4);
}
if(Lineplus.getSlope(p4, p5)==Lineplus.getSlope(p5, p1))
{
pq.remove(p5);
}
if(Lineplus.getSlope(p5, p1)==Lineplus.getSlope(p1, p2))
{
pq.remove(p5);
}
return pq;
}
public int ispoint(Point p) {
Point a=new Point(10000,p.y);
Lineplus l1=new Lineplus(a,p);//水平向右引一条
Lineplus l2=new Lineplus(p1,p2);
Lineplus l3=new Lineplus(p2,p3);
Lineplus l4=new Lineplus(p3,p4);
Lineplus l5=new Lineplus(p4,p5);
Lineplus l6=new Lineplus(p5,p1);
int i=0;
if(l2.middlepoint(l1.getIntersection(l2)))
{
if(l1.getIntersection(l2).x==p.x)
return 100;
if(l1.getIntersection(l2).x>p.x)
i++;
}
if(l3.middlepoint(l1.getIntersection(l3)))
{
if(l1.getIntersection(l3).x==p.x)
return 100;
if(l1.getIntersection(l3).x>p.x)
i++;
}
if(l4.middlepoint(l1.getIntersection(l4)))
{
if(l1.getIntersection(l4).x==p.x)
return 100;
if(l1.getIntersection(l4).x>p.x)
i++;
}
if(l5.middlepoint(l1.getIntersection(l5)))
{
if(l1.getIntersection(l5).x==p.x)
return 100;
if(l1.getIntersection(l5).x>p.x)
i++;
}
if(l6.middlepoint(l1.getIntersection(l6)))
{
if(l1.getIntersection(l6).x==p.x)
return 100;
if(l1.getIntersection(l6).x>p.x)
i++;
}
return i;
}
}
class quadrangleInputerrror {
public static void pointsCoincideError(Point p1, Point p2, Point p3, Point p4) {
if ((p1.getX() == p2.getX()) && p1.getY() == p2.getY()||p1.getX() == p3.getX() && p1.getY() == p3.getY()||p1.getX() == p4.getX() && p1.getY() == p4.getY()||p2.getX() == p3.getX() && p2.getY() == p3.getY()||p2.getX() == p4.getX() && p2.getY() == p4.getY()||p4.getX() == p3.getX() && p4.getY() == p3.getY()) {
System.out.println("points coincide");
System.exit(0);
}
}
}
class PointInputError {
//判断从字符串中解析出的点的数量是否合格。
public static void wrongNumberOfPoints(ArrayList ps, int num) {
if (ps.size() != num) {
System.out.println("wrong number of points");
System.exit(0);
}
}
//判断输入的字符串中点的坐标部分格式是否合格。若不符合,报错并退出程序
public static void wrongPointFormat(String s) {
if (!s.matches("[+-]?([1-9]\\d*|0)(\\.\\d+)?,[+-]?([1-9]\\d*|0)(\\.\\d+)?")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
// 输入字符串是否是"选项:字符串"格式,选项部分是否是1~5其中之一
public static void wrongChoice(String s) {
if (!s.matches("[1-6]:.+")) {
System.out.println("Wrong Format");
System.exit(0);
}
}
}
class ParseInput {
/*
* 输入:完整的输入字符串,包含选项和所有点的信息,格式:选项:x1,y1 x2,y2 .....xn,yn。选项只能是1-5
* 一个空InputData对象
* 处理:将输入字符串中的选项和点信息提取出来并设置到InputData对象中
* 输出:包含选项值和所有点的Point对象的InputData对象。
*/
public static void paseInput(String s, InputData d) {
PointInputError.wrongChoice(s);
d.setChoice(getChoice(s));
s = s.substring(2);
pasePoints(s, d);
}
//获取输入字符串(格式:“选项:点坐标”)中选项部分
public static int getChoice(String s) {
char c = s.charAt(0);
return c-48;
}
/*
* 输入:一个字符串,包含所有点的信息,格式:x1,y1 x2,y2 .....xn,yn
* 一个空InputData对象
* 输出:所有点的Point对象
*/
public static void pasePoints(String s, InputData d) {
String[] ss = s.split(" ");
if (ss.length == 0)
return;
for (int i = 0; i < ss.length; i++) {
d.addPoint(readPoint(ss[i]));
}
}
/*
* 输入:包含单个点信息的字符串,格式:x,y
* 输出:Point对象
*/
public static Point readPoint(String s) {
PointInputError.wrongPointFormat(s);
String[] ss = s.split(",");
double x = Double.parseDouble(ss[0]);
double y = Double.parseDouble(ss[1]);
// System.out.println("match");
return new Point(x, y);
}
}
class InputData {
private int choice;;//用户输入的选择项
private ArrayList<Point> points = new ArrayList<Point>();//用户输入的点坐标
public int getChoice() {
return choice;
}
public void setChoice(int choice) {
this.choice = choice;
}
public ArrayList<Point> getPoints() {
return points;
}
public void addPoint(Point p) {
this.points.add(p);
}
}
该题是五边形的深化,难度巨大,涉及十个点分成多边形的状况,同时涉及点是否在多边形内部的讨论情况.
3.踩坑心得
第一次作业中的银行业务题起初我有几个测试点过不去,在输入的过程中不能全部获取,于是我将其改成多个输入
account.deposit(sc.next(), sc.nextFloat());
account.withdraw(sc.next(), sc.nextFloat());
account.withdraw(sc.next(), sc.nextFloat());
account.withdraw(sc.next(), sc.nextFloat());
welcomeNext();
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具