第二次博客
目录前言
要学会使用规范的正则表达式,为什么这么说呢,因为我在写题目集3、4、5三次大作业时,每次做到判断输入格式是否正确时,我使用的正则表达式都不同,甚至几乎每个题目的判断都不一样,每次提交时返回的通过测试点(输入格式)都会有问题。题目意思要理解清楚,并且要看清楚题目所给出的限定条件。
设计与分析
题目集4——7-1 sdut-String-2 识蛟龙号载人深潜,立科技报国志(II)(正则表达式)
①题目及分析
背景简介:
“蛟龙号”载人深潜器是我国首台自主设计、自主集成研制的作业型深海载人潜水器,设计最大下潜深度为7000米级,也是目前世界上下潜能力最强的作业型载人潜水器。“蛟龙号”可在占世界海洋面积99.8%的广阔海域中使用,对于我国开发利用深海的资源有着重要的意义。
中国是继美、法、俄、日之后世界上第五个掌握大深度载人深潜技术的国家。在全球载人潜水器中,“蛟龙号”属于第一梯队。目前全世界投入使用的各类载人潜水器约90艘,其中下潜深度超过1000米的仅有12艘,更深的潜水器数量更少,目前拥有6000米以上深度载人潜水器的国家包括中国、美国、日本、法国和俄罗斯。除中国外,其他4国的作业型载人潜水器最大工作深度为日本深潜器的6527米,因此“蛟龙号”载人潜水器在西太平洋的马里亚纳海沟海试成功到达7020米海底,创造了作业类载人潜水器新的世界纪录。
从2009年至2012年,蛟龙号接连取得1000米级、3000米级、5000米级和7000米级海试成功。下潜至7000米,说明蛟龙号载人潜水器集成技术的成熟,标志着我国深海潜水器成为海洋科学考察的前沿与制高点之一。
2012年6月27日11时47分,中国“蛟龙”再次刷新“中国深度”——下潜7062米。6月3日,“蛟龙”出征以来,已经连续书写了5个“中国深度”新纪录:6月15日,6671米;6月19日,6965米;6月22日,6963米;6月24日,7020米;6月27日,7062米。下潜至7000米,标志着我国具备了载人到达全球99%以上海洋深处进行作业的能力,标志着“蛟龙”载人潜水器集成技术的成熟,标志着我国深海潜水器成为海洋科学考察的前沿与制高点之一,标志着中国海底载人科学研究和资源勘探能力达到国际领先水平。
‘蛟龙’号是我国载人深潜发展历程中的一个重要里程碑。它不只是一个深海装备,更代表了一种精神,一种不畏艰险、赶超世界的精神,它是中华民族进军深海的号角。
了解蛟龙号”载人深潜器“的骄人业绩,为我国海底载人科学研究和资源勘探能力达到国际领先水平而自豪,小伙伴们与祖国同呼吸、共命运,一定要学好科学文化知识、提高个人能力,增强创新意识,做事精益求精,立科技报国之志!
请编写程序,实现如下功能:读入关于蛟龙号载人潜水器探测数据的多行字符串,从给定的信息找出数字字符,输出每行的数字之和。
提示 若输入为“2012年2月”,则该行的输出为:2014。若干个连续的数字字符作为一个整体,以十进制形式相加。
输入格式:
读入关于蛟龙号载人潜水器探测数据的多行字符串,每行字符不超过80个字符。
以"end"结束。
输出格式:
与输入行相对应的各个整数之和。
②分析
本题就是考察对基础正则表达式的使用,较为简单
③代码
点击查看代码
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main{
public static void main(String[] args) {
Scanner set = new Scanner(System.in);
String str = set.nextLine();
ArrayList<Integer> list = new ArrayList<>();
while(!str.equals("end")) {
Pattern p = Pattern.compile("[0-9]+");
Matcher number = p.matcher(str);
int sum = 0;
while (number.find()) {
sum =sum + Integer.parseInt(number.group());
}
list.add(sum);
str = set.nextLine();
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
题目集4——7-2 点线形系列4-凸四边形的计算
①题目及分析
用户输入一组选项和数据,进行与四边形有关的计算。
以下四边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入四个点坐标,判断是否是四边形、平行四边形,判断结果输出true/false,结果之间以一个英文空格符分隔。
2:输入四个点坐标,判断是否是菱形、矩形、正方形,判断结果输出true/false,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
3:输入四个点坐标,判断是凹四边形(false)还是凸四边形(true),输出四边形周长、面积,结果之间以一个英文空格符分隔。 若四个点坐标无法构成四边形,输出"not a quadrilateral"
4:输入六个点坐标,前两个点构成一条直线,后四个点构成一个四边形或三角形,输出直线与四边形(也可能是三角形)相交的交点数量。如果交点有两个,再按面积从小到大输出四边形(或三角形)被直线分割成两部分的面积(不换行)。若直线与四边形或三角形的一条边线重合,输出"The line is coincide with one of the lines"。若后四个点不符合四边形或三角形的输入,输出"not a quadrilateral or triangle"。
后四个点构成三角形的情况:假设三角形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2)不符合要求的输入:z 不与xy都相邻,如z x y s、x z s y、x s z y
5:输入五个点坐标,输出第一个是否在后四个点所构成的四边形(限定为凸四边形,不考虑凹四边形)或三角形(判定方法见选项4)的内部(若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。如果点在多边形的某条边上,输出"on the triangle或者on the quadrilateral"。若后四个点不符合四边形或三角形,输出"not a quadrilateral or triangle"。
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
选项1、2、3中,若四边形四个点中有重合点,输出"points coincide"。
选项4中,若前两个输入线的点重合,输出"points coincide"。
②分析
>1.首先熟悉的操作,分析判断输入是否正确,使用规范的正则表达式。
2.判断能否构成四边形、平行四边形、菱形等特殊四边形:
连续的四个点都需要用上,也就是删除冗余点仍然剩下四个点。构成条件:相邻的两条线段不能再同一条直线上,不相邻的两条线段所在的直线产生的交点不能够在这两条线段上、判断完四边形,在这种情况下再加一些限制条件已完成特殊四边形判定的方法。
3.判断四边形的凹凸性:这里提供两种方法,向量叉乘法。或者面积法
4.分割面积的操作,我用的暴力破解列举了很多很多种情况,这里不太推荐暴力。
以上是我对各选项的理解分析,下面提供了一些方法仅供参考。
③代码
点击查看代码
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner set = new Scanner(System.in);
String str = set.nextLine();
String str1[] = str.split(":");
if(str1.length!=2){
System.out.print("Wrong Format");
System.exit(0);
}
String str2 = str1[1];
// System.out.printf(str2);
String str3[] = str2.split(" ");
for (String d : str3) {
String x[] = d.split(",");
if (x.length != 2) {
System.out.print("Wrong Format");
System.exit(0);
}
for (String j : x) {
if (!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
System.out.print("Wrong Format");
System.exit(0);
}
}
}
if(str.charAt(1)==':'){
ArrayList<String> list = new ArrayList<>();
ArrayList<Double> list1 = new ArrayList<>();
int index = 0;
String record;
int j=0;
for (j= 0; j < str2.length(); j++) {
if (str2.charAt(j) == ' ' || str2.charAt(j) == ',') {
if (index == 0) {
record = str2.substring(index, j);
} else {
record = str2.substring(index + 1, j);
}
list.add(record);
index = j;
}
}
if((str2.charAt(str2.length()-1)!=' ')){
record = str2.substring(index + 1, str2.length());
list.add(record);
}
for (int i = 0; i < list.size(); i++) {
double g = Double.parseDouble(list.get(i));
list1.add(g);
}
switch (str.charAt(0)){
case '1' :
if(list1.size()!=8){
System.out.println("wrong number of points");
}
else {
Point A = new Point(list1.get(0),list1.get(1));
Point B = new Point(list1.get(2),list1.get(3));
Point C = new Point(list1.get(4),list1.get(5));
Point D = new Point(list1.get(6),list1.get(7));
method1(A,B,C,D);
}
break;
case'2':
if(list1.size()!=8){
System.out.println("wrong number of points");
}
else {
Point A = new Point(list1.get(0),list1.get(1));
Point B = new Point(list1.get(2),list1.get(3));
Point C = new Point(list1.get(4),list1.get(5));
Point D = new Point(list1.get(6),list1.get(7));
method2(A,B,C,D);
}
break;
case '3':
if(list1.size()!=8){
System.out.println("wrong number of points");
}
else {
Point A = new Point(list1.get(0),list1.get(1));
Point B = new Point(list1.get(2),list1.get(3));
Point C = new Point(list1.get(4),list1.get(5));
Point D = new Point(list1.get(6),list1.get(7));
method3(A,B,C,D);
}
break;
case '4':
if(list1.size()!=12){
System.out.println("wrong number of points");
}
else {
Point A = new Point(list1.get(0),list1.get(1));
Point B = new Point(list1.get(2),list1.get(3));
Point C = new Point(list1.get(4),list1.get(5));
Point D = new Point(list1.get(6),list1.get(7));
Point E = new Point(list1.get(8),list1.get(9));
Point F = new Point(list1.get(10),list1.get(11));
method4(A,B,C,D,E,F);
}
break;
case '5':
if(list1.size()!=10){
System.out.println("wrong number of points");
}
else {
Point A = new Point(list1.get(0),list1.get(1));
Point B = new Point(list1.get(2),list1.get(3));
Point C = new Point(list1.get(4),list1.get(5));
Point D = new Point(list1.get(6),list1.get(7));
Point E = new Point(list1.get(8),list1.get(9));
method5(A,B,C,D,E);
}
break;
default:
System.out.println("Wrong Format");
break;
}
}
else {
System.out.printf("Wrong Format");
System.exit(0);
}
}
private static void method5(Point a, Point b, Point c, Point d,Point e) {
if(sbx(b,c,d,e)){//四边形
Line BC = new Line(b,c);
Line CD = new Line(c,d);
Line DE = new Line(d,e);
Line EB = new Line(e,b);
double s1 = sjS(a,b,c);
double s2 = sjS(a,d,c);
double s3 = sjS(a,d,e);
double s4 = sjS(a,b,e);
double S = sjS(c,b,d)+sjS(b,e,d);
if(a.isOnLine(BC)||a.isOnLine(CD)||
a.isOnLine(DE)||a.isOnLine(EB)){
System.out.printf("on the quadrilateral");
}else if(Math.abs(s1+s2+s3+s4-S)<0.000001){
System.out.printf("in the quadrilateral");
}else{
System.out.printf("outof the quadrilateral");
}
}//5:1,1 0,0 5,0 2,0 3,3
else if(sjx(b,c,d,e)){//三角形
triangle sj = new triangle();
sj = triangle.SJmax(b,c,d,e);
Line AB = new Line(sj.A,sj.B);
Line BC = new Line(sj.B,sj.C);
Line CA = new Line(sj.C,sj.A);
double s1 = sjS(a,sj.A,sj.B);
double s2 = sjS(a,sj.B,sj.C);
double s3 = sjS(a,sj.C,sj.A);
double S = sjS(sj.A,sj.B,sj.C);
if(a.isOnLine(AB)||a.isOnLine(BC)||a.isOnLine(CA)){
System.out.println("on the triangle");
}else if(s1+s2+s3-S<0.000001){
System.out.println("in the triangle");
}else {
System.out.println("outof the triangle");
}
}
else{
System.out.printf("not a quadrilateral or triangle");
}
}
public static boolean sjx(Point a, Point b, Point c, Point d){
Line AB = new Line(a,b);
Line BC = new Line(b,c);
Line CA = new Line(c,a);
if((d.dxDistance(AB)==0&&c.dxDistance(AB)!=0)||
(d.dxDistance(BC)==0&&a.dxDistance(BC)!=0)||
(d.dxDistance(CA)==0&&b.dxDistance(CA)!=0)||
(a.dxDistance(BC)==0&&d.dxDistance(BC)!=0)){
return true;
}else
return false;
}
public static ArrayList<Double> jd1(Point a, Point b, Point c, Point d, Point e, Point f){
ArrayList<Double> jiaodian = new ArrayList<>();
Line X = new Line(a,b);
Line AB = new Line(c,d);
Line BC = new Line(d,e);
Line CD = new Line(e,f);
Line DA = new Line(f,c);
//线重合
if(X.xch(AB)|| X.xch(BC)||X.xch(CD)||X.xch(DA)){
return jiaodian;
}
else{
//x1x2与线段1:
if(!X.pxline(AB)){
double getline1jdx = X.getjdx(AB);
double getline1jdy = X.getjdy(AB);
Point jd = new Point(getline1jdx,getline1jdy);
// System.out.println(getline1jdx+" "+getline1jdy+" 线1");
boolean flagjd1 = jd.isOnLine(AB);
// System.out.println(flagjd1);
if(flagjd1){
jiaodian.add(getline1jdx);
jiaodian.add(getline1jdy);
}
}
//x1,x2与线段2:
if(!X.pxline(BC)){
double getline2jdx = X.getjdx(BC);
double getline2jdy = X.getjdy(BC);
// System.out.println(getline2jdx+" "+getline2jdy+" 线2");
Point jd = new Point(getline2jdx,getline2jdy);
boolean flagjd2 = jd.isOnLine(BC);
// System.out.println(flagjd2);
if(flagjd2){
if(jiaodian.size()==0){
jiaodian.add(getline2jdx);
jiaodian.add(getline2jdy);
}
else {
int flag = 1;
for (int i = 0; i < jiaodian.size(); i=i+2) {
if(jiaodian.get(i)==getline2jdx&&jiaodian.get(i+1)==getline2jdy)
{
flag=0;
// System.out.printf("2重复啦");
break;
}
}
if(flag == 1){
jiaodian.add(getline2jdx);
jiaodian.add(getline2jdy);
}
}
}
}
//4:2,8 2,5 1,0 1,2 4,0
//x1,x2与线段3:
if(!X.pxline(CD)){
double getline3jdx = X.getjdx(CD);
double getline3jdy = X.getjdy(CD);
Point jd = new Point(getline3jdx,getline3jdy);
boolean flagjd3 = jd.isOnLine(CD);
// System.out.printf("dada");
// System.out.println(flagjd3);
if(flagjd3){
if(jiaodian.size()==0){
jiaodian.add(getline3jdx);
jiaodian.add(getline3jdy);
}
else {
int flag = 1;
for (int i = 0; i < jiaodian.size(); i=i+2) {
if(jiaodian.get(i)==getline3jdx&&jiaodian.get(i+1)==getline3jdy)
{
flag=0;
// System.out.printf("3重复啦");
break;
}
}
if(flag == 1){
jiaodian.add(getline3jdx);
jiaodian.add(getline3jdy);
}
}
}
}
if(!X.pxline(DA)){
double getline4jdx = X.getjdx(DA);
double getline4jdy = X.getjdy(DA);
Point jd = new Point(getline4jdx,getline4jdy);
boolean flagjd4 = jd.isOnLine(DA);
// System.out.printf("dada");
// System.out.println(flagjd3);
if(flagjd4){
if(jiaodian.size()==0){
jiaodian.add(getline4jdx);
jiaodian.add(getline4jdy);
}
else {
int flag = 1;
for (int i = 0; i < jiaodian.size(); i=i+2) {
if(jiaodian.get(i)==getline4jdx&&jiaodian.get(i+1)==getline4jdy)
{
flag=0;
// System.out.printf("3重复啦");
break;
}
}
if(flag == 1){
jiaodian.add(getline4jdx);
jiaodian.add(getline4jdy);
}
}
}
}
}
return jiaodian;
}
public static ArrayList<Double> jd2(Point a, Point b, Point c, Point d, Point e){
ArrayList<Double> jiaodian = new ArrayList<>();
Line X = new Line(a,b);
Line AB = new Line(c,d);
Line BC = new Line(d,e);
Line CA = new Line(e,c);
//线重合
if(X.xch(AB)|| X.xch(BC)||X.xch(CA)){
return jiaodian;
}
else{
//x1x2与线段1:
if(!X.pxline(AB)){
double getline1jdx = X.getjdx(AB);
double getline1jdy = X.getjdy(AB);
Point jd = new Point(getline1jdx,getline1jdy);
// System.out.println(getline1jdx+" "+getline1jdy+" 线1");
boolean flagjd1 = jd.isOnLine(AB);
// System.out.println(flagjd1);
if(flagjd1){
jiaodian.add(getline1jdx);
jiaodian.add(getline1jdy);
}
}
//x1,x2与线段2:
if(!X.pxline(BC)){
double getline2jdx = X.getjdx(BC);
double getline2jdy = X.getjdy(BC);
// System.out.println(getline2jdx+" "+getline2jdy+" 线2");
Point jd = new Point(getline2jdx,getline2jdy);
boolean flagjd2 = jd.isOnLine(BC);
// System.out.println(flagjd2);
if(flagjd2){
if(jiaodian.size()==0){
jiaodian.add(getline2jdx);
jiaodian.add(getline2jdy);
}
else {
int flag = 1;
for (int i = 0; i < jiaodian.size(); i=i+2) {
if(jiaodian.get(i)==getline2jdx&&jiaodian.get(i+1)==getline2jdy)
{
flag=0;
// System.out.printf("2重复啦");
break;
}
}
if(flag == 1){
jiaodian.add(getline2jdx);
jiaodian.add(getline2jdy);
}
}
}
}
//4:2,8 2,5 1,0 1,2 4,0
//x1,x2与线段3:
if(!X.pxline(CA)){
double getline3jdx = X.getjdx(CA);
double getline3jdy = X.getjdy(CA);
Point jd = new Point(getline3jdx,getline3jdy);
boolean flagjd3 = jd.isOnLine(CA);
// System.out.printf("dada");
// System.out.println(flagjd3);
if(flagjd3){
if(jiaodian.size()==0){
jiaodian.add(getline3jdx);
jiaodian.add(getline3jdy);
}
else {
int flag = 1;
for (int i = 0; i < jiaodian.size(); i=i+2) {
if(jiaodian.get(i)==getline3jdx&&jiaodian.get(i+1)==getline3jdy)
{
flag=0;
// System.out.printf("3重复啦");
break;
}
}
if(flag == 1){
jiaodian.add(getline3jdx);
jiaodian.add(getline3jdy);
}
}
}
}
}
return jiaodian;
}
public static double max(double x1,double x2){
if(x1>x2){
return x1;
}else {
return x2;
}
}
public static double min(double x1,double x2){
if(x1<x2){
return x1;
}else {
return x2;
}
}
public static double Ssbx(Point c,Point d,Point e,Point f){
double ab = c.Distance(d); double bc = d.Distance(e);
double cd = e.Distance(f); double da = f.Distance(c);
double s = (ab+bc+cd+da)/2;
double S = Math.sqrt((s-ab)*(s-bc)*(s-cd)*(s-da));
return S;
}
public static void mjsc(double S,double s1){
double s2 = S-s1;
double S1 = Double.parseDouble(String.format("%.3f",s1));
double S2 = Double.parseDouble(String.format("%.3f",s2));
System.out.printf(min(S1, S2) + " " + max(S1, S2));
}//输出最大面积+最小面积
private static void method4(Point a, Point b, Point c, Point d,Point e,Point f) {
if(superposition(a,b)){
System.out.println("points coincide");
}
else {
if(sbx(c,d,e,f)){
Line X = new Line(a,b);
Line CD = new Line(c,d);
Line DE = new Line(d,e);
Line EF = new Line(e,f);
Line FC = new Line(f,c);
if(X.xch(CD)||X.xch(DE)||X.xch(EF)||X.xch(FC)){
System.out.println("The line is coincide with one of the lines");
}
else {
ArrayList<Double> JD;
JD = jd1(a,b,c,d,e,f);
if(JD.size()!=4){
System.out.println(JD.size()/2);
}else{
System.out.printf(JD.size() / 2 + " ");
Point M = new Point(JD.get(0),JD.get(1));
Point N = new Point(JD.get(2),JD.get(3));
double S = Ssbx(c,d,e,f);
double Sabc = sjS(c,d,e);
double Sadc = sjS(c,e,f);
double Sabd = sjS(c,d,f);
double Sbcd = sjS(d,e,f);
if(superposition(M,c)&&superposition(N,e)||superposition(M,e)&&superposition(N,c)){
System.out.printf(min(Sabc, Sadc) + " " + max(Sabc, Sadc));
System.exit(0);
}
if(superposition(M,d)&&superposition(N,f)||superposition(M,f)&&superposition(N,d)){
System.out.printf(min(Sabd, Sbcd) + " " + max(Sabc, Sbcd));
System.exit(0);
}
if(superposition(M,c)&&!superposition(N,d)&&!superposition(N,e)&&!superposition(N,f)){
if(N.isOnLine(DE)){
double s1 = sjS(c,d,N);
mjsc(S,s1);
System.exit(0);
}
if(N.isOnLine(EF)){
double s1 = sjS(c,f,N);
mjsc(S,s1);
System.exit(0);
}
}if(superposition(N,c)&&!superposition(M,d)&&!superposition(M,e)&&!superposition(M,f)){
if(M.isOnLine(DE)){
double s1 = sjS(c,d,M);
mjsc(S,s1);
System.exit(0);
}
if(M.isOnLine(EF)){
double s1 = sjS(c,f,M);
mjsc(S,s1);
System.exit(0);
}
}
if(superposition(M,d)&&!superposition(N,c)&&!superposition(N,e)&&!superposition(N,f)){
if(N.isOnLine(FC)){
double s1 = sjS(c,d,N);
mjsc(S,s1);
System.exit(0);
}
if(N.isOnLine(EF)){
double s1 = sjS(e,d,N);
mjsc(S,s1);
System.exit(0);
}
}if(superposition(N,d)&&!superposition(M,c)&&!superposition(M,e)&&!superposition(M,f)){
if(M.isOnLine(FC)){
double s1 = sjS(c,d,M);
mjsc(S,s1);
System.exit(0);
}
if(M.isOnLine(EF)){
double s1 = sjS(e,d,M);
mjsc(S,s1);
System.exit(0);
}
}
if(superposition(M,e)&&!superposition(N,c)&&!superposition(N,d)&&!superposition(N,f)){
if(N.isOnLine(CD)){
double s1 = sjS(d,e,N);
mjsc(S,s1);
System.exit(0);
}
if(N.isOnLine(FC)){
double s1 = sjS(e,f,N);
mjsc(S,s1);
System.exit(0);
}
}if(superposition(N,e)&&!superposition(M,c)&&!superposition(M,d)&&!superposition(M,f)){
if(M.isOnLine(CD)){
double s1 = sjS(e,d,M);
mjsc(S,s1);
System.exit(0);
}
if(M.isOnLine(FC)){
double s1 = sjS(e,f,M);
mjsc(S,s1);
System.exit(0);
}
}
if(superposition(M,f)&&!superposition(N,c)&&!superposition(N,d)&&!superposition(N,e)){
if(N.isOnLine(CD)){
double s1 = sjS(c,f,N);
mjsc(S,s1);
System.exit(0);
}
if(N.isOnLine(DE)){
double s1 = sjS(e,f,N);
mjsc(S,s1);
System.exit(0);
}
}if(superposition(N,f)&&!superposition(M,c)&&!superposition(M,d)&&!superposition(M,e)){
if(M.isOnLine(CD)){
double s1 = sjS(c,f,M);
mjsc(S,s1);
System.exit(0);
}
if(M.isOnLine(DE)){
double s1 = sjS(e,f,M);
mjsc(S,s1);
System.exit(0);
}
}
if(!superposition(M,c)&&!superposition(M,d)&&!superposition(M,e)&&!superposition(M,f)
&&!superposition(N,c)&&!superposition(N,d)&&!superposition(N,e)&&!superposition(N,f)){
if((N.isOnLine(CD)&&M.isOnLine(DE))||(M.isOnLine(CD)&&N.isOnLine(DE))){
double s1 = sjS(d,M,N);
mjsc(S,s1);
System.exit(0);
}
if((N.isOnLine(DE)&&M.isOnLine(EF))||(M.isOnLine(DE)&&N.isOnLine(EF))){
double s1 = sjS(e,M,N);
mjsc(S,s1);
System.exit(0);
}
if((N.isOnLine(FC)&&M.isOnLine(EF))||(M.isOnLine(FC)&&N.isOnLine(EF))){
double s1 = sjS(f,M,N);
mjsc(S,s1);
System.exit(0);
}
if((N.isOnLine(FC)&&M.isOnLine(CD))||(M.isOnLine(FC)&&N.isOnLine(CD))){
double s1 = sjS(c,M,N);
mjsc(S,s1);
System.exit(0);
}
if((M.isOnLine(CD)&&N.isOnLine(EF))||(M.isOnLine(EF)&&N.isOnLine(CD))){
double s1 = Ssbx(c,f,N,M);
mjsc(S,s1);
System.exit(0);
}
if((M.isOnLine(DE)&&N.isOnLine(FC))||(M.isOnLine(DE)&&N.isOnLine(FC))){
double s1 = Ssbx(c,f,M,N);
mjsc(S,s1);
System.exit(0);
}
}
}
}
}
else if(sjxpd(c,d,e,f)){
triangle sj = new triangle();
sj = triangle.SJmax(c,d,e,f);
Line AB = new Line(sj.A,sj.B);
Line BC = new Line(sj.B,sj.C);
Line CA = new Line(sj.C,sj.A);
Line X = new Line(a,b);
if(X.xch(AB)||X.xch(BC)||X.xch(CA)){
System.out.println("The line is coincide with one of the lines");
}
else{
ArrayList<Double> JD;
JD = jd2(a,b,sj.A,sj.B,sj.C);
if(JD.size()!=4){
System.out.println(JD.size()/2);
}else {
System.out.printf(JD.size() / 2 + " ");
double S = sjS(sj.A,sj.B,sj.C);
Point M = new Point(JD.get(0),JD.get(1));
Point N = new Point(JD.get(2),JD.get(3));
double s123 = sjS(M,N,sj.A);
double s124 = sjS(M,N,sj.B);
double s125 = sjS(M,N,sj.C);
double sh = 0.0;
if (superposition(M,sj.A)) {
if (S == s124 + s125) {
System.out.printf(min(s124, s125) + " " + max(s124, s125));
System.exit(0);
}
} else if (superposition(N,sj.A)) {
if (S == s124 + s125) {
System.out.printf(min(s124, s125) + " " + max(s124, s125));
System.exit(0);
}
} else if (superposition(M,sj.B)) {
if (S == s123 + s125) {
System.out.printf(min(s123, s125) + " " + max(s123, s125));
System.exit(0);
}
} else if (superposition(N,sj.B)) {
if (S == s123 + s125) {
System.out.printf(min(s123, s125) + " " + max(s123, s125));
System.exit(0);
}
} else if (superposition(M,sj.C)) {
if (S == s123 + s124) {
System.out.printf(min(s123, s124) + " " + max(s123, s124));
System.exit(0);
}
}
else if(superposition(N,sj.C)) {
if (S == s123 + s124) {
System.out.printf(min(s123, s124) + " " + max(s123, s124));
System.exit(0);
}
}
if ((M.isOnLine(AB) && N.isOnLine(CA)) ||
(N.isOnLine(AB) && M.isOnLine(CA))) {
sh = S-s123;
double s = Double.parseDouble(String.format("%.3f",sh));
System.out.printf(min(s123,s)+" "+max(s123,s));
System.exit(0);
} else if ((M.isOnLine(AB) && N.isOnLine(BC)) ||
(N.isOnLine(AB) && M.isOnLine(BC))) {
sh = S-s124;
double s = Double.parseDouble(String.format("%.3f",sh));
System.out.printf(min(s124,s)+" "+max(s124,s));
System.exit(0);
} else if ((M.isOnLine(CA) && N.isOnLine(BC)) ||
(N.isOnLine(CA) && M.isOnLine(BC))) {
sh = S-s125;
double s = Double.parseDouble(String.format("%.3f",sh));
System.out.printf(min(s125,s)+" "+max(s125,s));
System.exit(0);
}
}
}
}
else {
System.out.println("not a quadrilateral or triangle");
}
}
}
private static boolean sjxpd(Point a, Point b, Point c, Point d) {
Line AB = new Line(a,b);
Line BC = new Line(b,c);
Line CA = new Line(c,a);
Line CD = new Line(c,d);
Line BD = new Line(b,d);
if(d.dxDistance(AB)==0&&c.dxDistance(AB)!=0){
if(superposition(a,d)||superposition(d,b)){
return true;
}else{
if(a.isOnLine(BD)){
return true;
}else{
return false;
}
}
}
else if (d.dxDistance(BC)==0&&a.dxDistance(BC)!=0) {
if(superposition(d,b)||superposition(d,c)){
return true;
}else{
if(c.isOnLine(BD)){
return true;
}else{
return false;
}
}
}
else if(d.dxDistance(CA)==0&&b.dxDistance(CA)!=0){
if(superposition(c,d)||superposition(d,a)){
return true;
}else{
if(d.isOnLine(CA)){
return true;
}else{
return false;
}
}
}
else if(a.dxDistance(BC)==0&&d.dxDistance(BC)!=0){
if(superposition(a,b)||superposition(c,a)){
return true;
}else{
if(b.isOnLine(CA)){
return true;
}else{
return false;
}
}
}else
return false;
}
private static void method3(Point a, Point b, Point c, Point d) {
if(superposition(a,b)||superposition(a,c)||superposition(a,d)
||superposition(b,c)||superposition(b,d)||superposition(c,d)){
System.out.println("points coincide");
}
else{
if(!sbx(a,b,c,d)){
System.out.println("not a quadrilateral");
}
else {
double d1,d2,d3,d4;
d1 = a.Distance(b); d2 = b.Distance(c);
d3 = c.Distance(d); d4 = d.Distance(a);
double C = Double.parseDouble(String.format("%.3f",d1 + d2 +d3 +d4));
double S=0.0;
double Smax = 0.0;
double Smin = 0.0;
if(a.dzsjn(b,c,d)||b.dzsjn(a,c,d)
||c.dzsjn(a,b,d)||d.dzsjn(a,b,c)){
if(a.dzsjn(b,c,d)){
Smax = sjS(b,c,d);
Smin = sjS(a,b,d);
S = Smax - Smin;
}
if(b.dzsjn(a,c,d)){
Smax = sjS(a,c,d);
Smin = sjS(a,b,c);
S = Smax - Smin;
}
if(c.dzsjn(a,b,d)){
Smax = sjS(b,a,d);
Smin = sjS(c,b,d);
S = Smax - Smin;
}
if(d.dzsjn(a,b,c)){
Smax = sjS(b,c,a);
Smin = sjS(a,c,d);
S = Smax - Smin;
}
double s = Double.parseDouble(String.format("%.3f",S));
System.out.printf("false"+" "+C+" "+s);
}else{
double s1 = sjS(a,b,c);
double s2 = sjS(a,c,d);
double ss = Ssbx(a,b,c,d);
double s = Double.parseDouble(String.format("%.3f",ss));
System.out.printf("true"+" "+C+" "+s);
}
}
}
}
private static void method2(Point a, Point b, Point c, Point d) {
if(!sbx(a,b,c,d)){
System.out.println("not a quadrilateral");
}
else{
if(pxsbx(a,b,c,d)){
double d1,d2,d3,d4;
d1 = a.Distance(b); d2 = b.Distance(c);
d3 = c.Distance(d); d4 = d.Distance(a);
double D1 = a.Distance(c);
double D2 = b.Distance(d);
if(d1==d2&&d2==d3&&d3==d4){
System.out.printf("true"+" ");
}else{
System.out.printf("false"+" ");
}
if(Math.abs(d1*d1+d2*d2-D1*D1)<0.0000001){
System.out.printf("true"+" ");
}else{
System.out.printf("false"+" ");
}
if(d1==d2&&d2==d3&&d3==d4&&D1==D2){
System.out.printf("true");
}
else{
System.out.printf("false");
}
}
}
}
public static boolean superposition(Point a, Point b){
if(a.x == b.x && a.y == b.y){
return true;
}
else
return false;
}
public static void method1(Point a, Point b, Point c, Point d){
if(superposition(a,b)||superposition(a,c)||superposition(a,d)
||superposition(b,c)||superposition(b,d)||superposition(c,d)){
System.out.println("points coincide");
}
else{
if(sbx(a,b,c,d)){
System.out.printf("true"+" ");
}else{
System.out.printf("false"+" ");
}
if(pxsbx(a,b,c,d)){
System.out.println("true");
}else{
System.out.println("false");
}
}
}
public static boolean sbx(Point a,Point b,Point c,Point d){
Line AB = new Line();AB.setA(a);AB.setB(b);
Line BC = new Line();BC.setA(b);BC.setB(c);
Line CD = new Line();CD.setA(c);CD.setB(d);
Line DA = new Line();DA.setA(d);DA.setB(a);
if(a.dxDistance(BC)==0||b.dxDistance(DA)==0
||a.dxDistance(CD)==0||b.dxDistance(CD)==0) {
return false;
}
else{
if(AB.pxline(CD)&&BC.pxline(DA)){
return true;
}else{
if(!AB.pxline(CD)){
double jdx = AB.getjdx(CD);
double jdy = AB.getjdy(CD);
Point jd = new Point(jdx,jdy);jd.setX(jdx);jd.setY(jdy);
boolean flag = (jd.isOnLine(CD)&&jd.isOnLine(AB));
if(flag){
return false;
}
else {
return true;
}
}
if(!BC.pxline(DA)){
double jdx = BC.getjdx(DA);
double jdy = BC.getjdy(DA);
Point jd = new Point(jdx,jdy);jd.setX(jdx);jd.setY(jdy);
boolean flag = (jd.isOnLine(DA)&&jd.isOnLine(BC));
if(flag){
return false;
}
else {
return true;
}
}
}
}
return true;
}
public static boolean pxsbx(Point a, Point b, Point c, Point d){
if(sbx(a, b, c, d)){
double d1,d2,d3,d4;
d1 = a.Distance(b);
d2 = b.Distance(c);
d3 = c.Distance(d);
d4 = d.Distance(a);
if(d1 == d3 && d2 == d4){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
public static double sjS(Point b,Point c,Point d){
double d1 = b.Distance(c);
double d2 = c.Distance(d);
double d3 = d.Distance(b);
double C = d1 + d2 +d3;
double S = Math.abs(Math.pow((C/2)*(C/2-d1)*(C/2-d2)*(C/2-d3),0.5));
double s = Double.parseDouble(String.format("%.3f",S));
return s;
}
static class Point{
public double x;
public double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double Distance(Point b){
double d = Math.sqrt((b.x-this.x)*(b.x-this.x)+(b.y-this.y)*(b.y-this.y));
return d;
}
public double dxDistance(Line M){
double d;
double x1,x2,x3,y1,y2,y3;
x1=this.x;y1=this.y;x2=M.a.x;y2=M.a.y;x3=M.b.x;y3=M.b.y;
d=Math.abs((y3-y2)*x1+(x2-x3)*y1+x3*y2-y3*x2) /
Math.sqrt(Math.pow(y3-y2, 2)+Math.pow(x3-x2, 2));
return d;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public boolean isOnLine(Line M){
double a = this.x;double b = this.y;
double d1 = this.Distance(M.a);
double d2 = this.Distance(M.b);
double D = M.a.Distance(M.b);
if(Math.abs(d1+d2-D)<0.000001){
return true;
}
return false;
}
public boolean dzsjn(Point b, Point c, Point d){
double S = sjS(b,c,d);
double s1 = sjS(this,b,c);
double s2 = sjS(this,c,d);
double s3 = sjS(this,b,d);
if(Math.abs(s1+s2+s3-S)<0.000001){
return true;
}
else
return false;
}
}
static class Line{
Point a;
Point b;
public Line() {
}
public Line(Point a, Point b) {
this.a = a;
this.b = b;
}
public boolean xch(Line N){
if(this.a.dxDistance(N)==0&&this.b.dxDistance(N)==0){
return true;
}else{
return false;
}
}
public double getjdx(Line N){
double x1,x2,x3,x4,y1,y2,y3,y4;
x1 = this.a.x;x2=this.b.x;x3=N.a.x;x4=N.b.x;
y1 = this.a.y;y2=this.b.y;y3=N.a.y;y4=N.b.y;
double m = (y2-y1)*(x3-x4)-(y4-y3)*(x1-x2);
double x = ((x4 * y3 - x3 * y4)*(x1 - x2) - (x2 * y1 - x1 * y2)*(x3 - x4)) / m;
return x;
}
public double getjdy(Line N){
double x1,x2,x3,x4,y1,y2,y3,y4;
x1 = this.a.x;x2=this.b.x;x3=N.a.x;x4=N.b.x;
y1 = this.a.y;y2=this.b.y;y3=N.a.y;y4=N.b.y;
double m = (y2-y1)*(x3-x4)-(y4-y3)*(x1-x2);
double y = ((x2 * y1 - x1 * y2)*(y4 - y3) - (x4 * y3 - x3 * y4)*(y2 - y1)) / m;
return y;
}
public boolean pxline(Line N){
double x1,y1,x2,y2,x3,y3,x4,y4;
x1 = this.a.x; x2 = this.b.x; x3 = N.a.x; x4 = N.b.x;
y1 = this.a.y; y2 = this.b.y; y3 = N.a.y; y4 = N.b.y;
double k1=0,k2=0;
if(x1==x2 && x3 == x4){
return true;
}
else {
k1 = (y2-y1)/(x2-x1);
k2 = (y4-y3)/(x4-x3);
if(k1 == k2){
return true;
}
else {
return false;
}
}
}
public void setA(Point a) {
this.a = a;
}
public void setB(Point b) {
this.b = b;
}
}
static class triangle{
public Point A;
public Point B;
public Point C;
public triangle() {
}
public triangle(Point A, Point B, Point C) {
this.A = A;
this.B = B;
this.C = C;
}
public static triangle SJmax(Point a,Point b,Point c,Point d){
Line AB = new Line(a,b);
Line BC = new Line(b,c);
Line CA = new Line(c,a);
triangle A = new triangle(a,c,d);
if(d.dxDistance(AB)==0&&c.dxDistance(AB)!=0){
if(d.isOnLine(AB)){
triangle MAXSJ = new triangle(a,b,c);
return MAXSJ;
}else{
double s1 = sjS(c,b,d);
double s2 = sjS(a,c,d);
if(s1>s2){
triangle MAXSJ = new triangle(c,b,d);
return MAXSJ;
}else{
triangle MAXSJ = new triangle(a,c,d);
return MAXSJ;
}
}
}
if(d.dxDistance(BC)==0&&a.dxDistance(BC)!=0){
if(d.isOnLine(BC)){
triangle MAXSJ = new triangle(a,b,c);
return MAXSJ;
}else{
double s1 = sjS(a,b,d);
double s2 = sjS(a,c,d);
if(s1>s2){
triangle MAXSJ = new triangle(a,b,d);
return MAXSJ;
}else{
triangle MAXSJ = new triangle(a,c,d);
return MAXSJ;
}
}
}
if(d.dxDistance(CA)==0&&b.dxDistance(CA)!=0){
if(d.isOnLine(CA)){
triangle MAXSJ = new triangle(a,b,c);
return MAXSJ;
}else{
double s1 = sjS(c,b,d);
double s2 = sjS(a,b,d);
if(s1>s2){
triangle MAXSJ = new triangle(c,b,d);
return MAXSJ;
}else{
triangle MAXSJ = new triangle(a,b,d);
return MAXSJ;
}
}
}
if(a.dxDistance(BC)==0&&d.dxDistance(CA)!=0){
if(c.isOnLine(AB)){
triangle MAXSJ = new triangle(a,b,d);
return MAXSJ;
}else{
double s1 = sjS(a,c,d);
double s2 = sjS(c,b,d);
if(s1>s2){
triangle MAXSJ = new triangle(a,c,d);
return MAXSJ;
}else{
triangle MAXSJ = new triangle(c,b,d);
return MAXSJ;
}
}
}
return A;
}
}
}
④报表
⑤总结与分析
再次强调一下正则表达式的规范!认真学习正则表达式对简单测试点通过率的提高有很大的帮助!!!
因为起初没有想太多,包括考虑设计图形父类、设计三角形、四边形类,导致思路局限于想到啥或者需要用到什么就在写一个静态方法,在测试类中直接调用这些方法。这样的写法非常愚笨。不建议大家用这种思路解题。该题目较为简单,我使用暴力破解的方法,起初代码过长无法提交,后面将一些重复冗余的代码重构、删除之后才控制在规定限制长度内,已完成代码的提交以及满分.
题目集4——7-3 设计一个银行业务类
①题目及分析
编写一个银行业务类BankBusiness,具有以下属性和方法:
(1)公有、静态的属性:银行名称bankName,初始值为“中国银行”。
(2)私有属性:账户名name、密码password、账户余额balance。
(3)银行对用户到来的欢迎(welcome)动作(静态、公有方法),显示“中国银行欢迎您的到来!”,其中“中国银行”自动使用bankName的值。
(4)银行对用户离开的提醒(welcomeNext)动作(静态、公有方法),显示“请收好您的证件和物品,欢迎您下次光临!”
(5)带参数的构造方法,完成开户操作。需要账户名name、密码password信息,同时让账户余额为0。
(6)用户的存款(deposit)操作(公有方法,需要密码和交易额信息),密码不对时无法存款且提示“您的密码错误!”;密码正确、完成用户存款操作后,要提示用户的账户余额,例如“您的余额有1000.0元。”。
(7)用户的取款(withdraw)操作(公有方法,需要密码和交易额信息)。密码不对时无法取款且提示“您的密码错误!”;密码正确但余额不足时提示“您的余额不足!”;密码正确且余额充足时扣除交易额并提示用户的账户余额,例如“请取走钞票,您的余额还有500.0元。”。
编写一个测试类Main,在main方法中,先后执行以下操作:
(1)调用BankBusiness类的welcome()方法。
(2)接收键盘输入的用户名、密码信息作为参数,调用BankBusiness类带参数的构造方法,从而创建一个BankBusiness类的对象account。
(3)调用account的存款方法,输入正确的密码,存入若干元。密码及存款金额从键盘输入。
(4)调用account的取款方法,输入错误的密码,试图取款若干元。密码及取款金额从键盘输入。
(5)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额大于余额)。密码及取款金额从键盘输入。
(6)调用account的取款方法,输入正确的密码,试图取款若干元(取款金额小于余额)。密码及取款金额从键盘输入。
(7)调用BankBusiness类的welcomeNext()方法。
输入格式:
输入开户需要的姓名、密码
输入正确密码、存款金额
输入错误密码、取款金额
输入正确密码、大于余额的取款金额
输入正确密码、小于余额的取款金额
输出格式:
中国银行(银行名称)欢迎您的到来!
您的余额有多少元。
您的密码错误!
您的余额不足!
请取走钞票,您的余额还有多少元。
请收好您的证件和物品,欢迎您下次光临!
②分析
设计类以及相应属性,将属性私有化,书写基本JavaBean类setter、getter方法,以及题目所需要的方法并且在测试类调用。较为简单。
③代码
点击查看代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner set = new Scanner(System.in);
BankBusiness.welcome();
String name = set.next();
String passward = set.next();
BankBusiness account = new BankBusiness(name,passward);
String testpassward1 = set.next();
double testmonety1 = set.nextDouble();
account.deposit(testpassward1,testmonety1);
String testpassward2 = set.next();
double testmonety2 = set.nextDouble();
account.withdraw(testpassward2,testmonety2);
String testpassward3 = set.next();
double testmonety3 = set.nextDouble();
account.withdraw(testpassward3,testmonety3);
String testpassward4 = set.next();
double testmonety4 = set.nextDouble();
account.withdraw(testpassward4,testmonety4);
BankBusiness.welcomeNext();
}
}
class BankBusiness{
public static String bankname="中国银行";
private String name;
private String passward;
private double balance;
public BankBusiness() {
}
public BankBusiness(String name, String passward) {
this.name = name;
this.passward = passward;
this.balance = 0;
}
public static void welcome(){
System.out.println(bankname+"欢迎您的到来!");
}
public static void welcomeNext(){
System.out.println("请收好您的证件和物品,欢迎您下次光临!");
}
public void deposit(String passward, double x){
if(!passward.equals(this.passward)){
System.out.println("您的密码错误!");
}
else{
this.balance += x;
System.out.println("您的余额有"+this.balance+"元。");
}
}
public void withdraw(String passward, double x){
if(!passward.equals(this.passward)){
System.out.println("您的密码错误!");
}
else{
if(this.balance>=x){
this.balance -= x;
System.out.println("请取走钞票,您的余额还有"+this.balance+"元。");
}
else {
System.out.println("您的余额不足!");
}
}
}
public static String getBankname() {
return bankname;
}
public static void setBankname(String bankname) {
BankBusiness.bankname = bankname;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassward() {
return passward;
}
public void setPassward(String passward) {
this.passward = passward;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public String toString() {
return "BankBusiness{bankname = " + bankname + ", name = " + name + ", passward = " + passward + ", balance = " + balance + "}";
}
}
④总结与分析
基本JavaBean使用。巩固基础。
题目集5——7-1 点线形系列5-凸五边形的计算-1
①题目及分析
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
1:输入五个点坐标,判断是否是五边形,判断结果输出true/false。
2:输入五个点坐标,判断是凹五边形(false)还是凸五边形(true),如果是凸五边形,则再输出五边形周长、面积,结果之间以一个英文空格符分隔。 若五个点坐标无法构成五边形,输出"not a pentagon"
3:输入七个点坐标,前两个点构成一条直线,后五个点构成一个凸五边形、凸四边形或凸三角形,输出直线与五边形、四边形或三角形相交的交点数量。如果交点有两个,再按面积从小到大输出被直线分割成两部分的面积(不换行)。若直线与多边形形的一条边线重合,输出"The line is coincide with one of the lines"。若后五个点不符合五边形输入,若前两点重合,输出"points coincide"。
以上3选项中,若输入的点无法构成多边形,则输出"not a polygon"。输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
基本输出格式见每种选项的描述。
异常情况输出:
如果不符合基本格式,输出"Wrong Format"。
如果符合基本格式,但输入点的数量不符合要求,输出"wrong number of points"。
注意:输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
②分析
1.输入格式问题:强调很多遍的正则表达式!使用规范的正则表达式
2.判断五边形:这里要根据写题集4时的思路往下走,可以发现能否按照顺序的点构成四边形、五边形等多边形
3.判断能否构成多边形:重点在于排除冗余点。
4.对于多边形面积、周长等计算这里不在做说明,无非就是调用类(点、线)中的方法获取。
5.关于面积分割!这里非常重要,不能够再像我之前的暴力方法破解啦!要不然肯定代码长度过长,连题目都交不了,要想好如何设计好方法。
关于判断点集能否构成多边形已经在题集4第二题中详细描述,这里不在赘述。
③代码
点击查看代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class one{
public static void main(String[] args) {
String str = input.input_String();
boolean flag = input.input_check(str);
if(flag&&str.charAt(1)==':'){
ArrayList<Double> Point_List = input.Point_list(str);
// for (int i = 0; i < Point_List.size(); i++) {
// System.out.printf(Point_List.get(i)+" ");
// }
switch (str.charAt(0)){
case '1' :
if(Point_List.size()!=10){
System.out.println("wrong number of points");
}
else{
ArrayList<Point> points = method.get_point_from_input(Point_List);
pentagon W = new pentagon(points);
W.setGraphics_point(points);
W.pentagon_line = W.setGraphics_P_Line();
// W.setGraphics_Line(W.pentagon_line);
// for (int i = 0; i < W.pentagon_point.size(); i++) {
// System.out.println(W.pentagon_point.get(i).x+" "+W.pentagon_point.get(i).y);
// }
// for (int i = 0; i < W.pentagon_line.size(); i++) {
// System.out.println(W.pentagon_line.get(i).a.x+","+W.pentagon_line.get(i).a.y+
// " "+W.pentagon_line.get(i).b.x+","+W.pentagon_line.get(i).b.y);
// }
if(W.Is_pentagon()){
System.out.println("true");
}else{
System.out.println("false");
}
}
break;
case '2' :
if(Point_List.size()!=10){
System.out.println("wrong number of points");
}
else{
ArrayList<Point> points = method.get_point_from_input(Point_List);
pentagon W = new pentagon(points);
W.setGraphics_point(W.pentagon_point);
W.pentagon_line = W.setGraphics_P_Line();
W.setGraphics_Line(W.pentagon_line);
if(W.Is_pentagon()){
if(W.Is_convex()){
System.out.print("true"+" ");
double C = method.format_output(W.C_Graphics());
double S = method.format_output(W.area());
System.out.println(C+" "+S);
}else{
System.out.print("false");
}
}
else{
System.out.println("not a pentagon");
}
}
break;
case '3' :
if(Point_List.size()!=14){
System.out.println("wrong number of points");
}
else{
ArrayList<Point> points = method.get_point_from_input(Point_List);
Line line = new Line(points.get(0), points.get(1));
if(points.get(0).Same_Point(points.get(1))) {
System.out.println("points coincide");
System.exit(0);
}
points.remove(0);points.remove(0);
ArrayList<Point> new_points = method.delete_Point(points);
pentagon pg = new pentagon(new_points);
pg.setGraphics_point(pg.pentagon_point);
pg.pentagon_line = pg.setGraphics_P_Line();
pg.setGraphics_Line(pg.pentagon_line);
if(new_points.size() == 5){
ArrayList<Point> list1 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list1.add(new_points.get(i));
}
if(pg.line_Is_onGraphics(line))
System.out.println("The line is coincide with one of the lines");
else
pg.Onepentagon(line);
}
else if(new_points.size() == 4){
quadrilateral ql = new quadrilateral(new_points);
ql.setGraphics_point(ql.quadrilateral_point);
ql.quadrilateral_line = pg.setGraphics_P_Line();
ql.setGraphics_Line(ql.quadrilateral_line);
if(ql.line_Is_onGraphics(line))
System.out.println("The line is coincide with one of the lines");
else {
ql.Onequarilateral(line);
}
}
else if(new_points.size() == 3) {
triangle tl = new triangle(new_points);
tl.setGraphics_point(tl.triangle_point);
tl.triangle_line = pg.setGraphics_P_Line();
tl.setGraphics_Line(tl.triangle_line);
// for (int i = 0; i < tl.Graphics_point.size(); i++) {
// System.out.println(tl.Graphics_point.get(i).x+","+tl.Graphics_point.get(i).y);
// }
// for (int i = 0; i < tl.triangle_point.size(); i++) {
// System.out.println(tl.triangle_point.get(i).x+","+tl.triangle_point.get(i).y);
// }
// for (int i = 0; i < tl.triangle_line.size(); i++) {
// System.out.println(tl.Graphics_line.get(i).a.x+","+tl.Graphics_line.get(i).a.y+" "+tl.Graphics_line.get(i).b.x+","+tl.Graphics_line.get(i).b.y+" ");
// }
// for (int i = 0; i < tl.triangle_line.size(); i++) {
// System.out.println(tl.triangle_line.get(i).a.x+","+tl.triangle_line.get(i).a.y+" "+tl.triangle_line.get(i).b.x+","+tl.triangle_line.get(i).b.y+" ");
// }
if(tl.line_Is_onGraphics(line))
System.out.println("The line is coincide with one of the lines");
else {
tl.OneTriangle(line);
}
}
else {
System.out.println("not a polygon");
}
}
break;
default:
System.out.println("wrong format");
System.exit(0);
}
}
else{
System.out.println("wrong format");
System.exit(0);
}
}
static class input{
public static String input_String(){
Scanner set = new Scanner(System.in);
return set.nextLine();
}
public static boolean input_check(String str){
//结尾输入:应该返回wrong format;
String[] str1 = str.split(":");
if (str1.length != 2) {
System.out.print("Wrong Format");
System.exit(0);
}
String str2 = str1[1];
// System.out.printf(str2);
String[] str3 = str2.split(" ");
for (String d : str3) {
String[] x = d.split(",");
if (x.length != 2) {
System.out.print("Wrong Format");
System.exit(0);
}
for (String j : x) {
if (!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
System.out.print("Wrong Format");
System.exit(0);
}
}
}
return true;
}
public static ArrayList<Double> Point_list(String str){
String[] str1 = str.split(":");
String str2 = str1[1];
ArrayList<String> list = new ArrayList<>();
ArrayList<Double> list1 = new ArrayList<>();
int index = 0;
String record;
for (int j= 0; j < str2.length(); j++) {
if (str2.charAt(j) == ' ' || str2.charAt(j) == ',') {
if (index == 0) {
record = str2.substring(index, j);
} else {
record = str2.substring(index + 1, j);
}
list.add(record);
index = j;
}
}
if((str2.charAt(str2.length()-1)!=' ')){
record = str2.substring(index + 1);
list.add(record);
}
for (String s : list) {
double g = Double.parseDouble(s);
list1.add(g);
}
return list1;
}
}
static class Point{
public double x;
public double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public boolean Same_Point(Point b){
return this.x == b.x && this.y == b.y;
}
public double Distance(Point b){
return Math.sqrt((b.x-this.x)*(b.x-this.x)+(b.y-this.y)*(b.y-this.y));
}
public double dxDistance(Line M){
double d;
double x1,x2,x3,y1,y2,y3;
x1=this.x;y1=this.y;x2=M.a.x;y2=M.a.y;x3=M.b.x;y3=M.b.y;
d=Math.abs((y3-y2)*x1+(x2-x3)*y1+x3*y2-y3*x2) /
Math.sqrt(Math.pow(y3-y2, 2)+Math.pow(x3-x2, 2));
return d;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public boolean isOnLine(Line M){
double d1 = this.Distance(M.a);
double d2 = this.Distance(M.b);
double D = M.a.Distance(M.b);
return Math.abs(d1 + d2 - D) < 0.000001;
}
public boolean is_Online(Line M){
double d1 = this.Distance(M.a);
double d2 = this.Distance(M.b);
double D = M.a.Distance(M.b);
return Math.abs(d1 + d2 - D) < 0.000001 && !this.Same_Point(M.a) && !this.Same_Point(M.b);
}
public boolean PPP_IsOn_SanmeLine(Point A,Point B){
Line AB = new Line(A,B);
return this.dxDistance(AB) == 0;
}
public boolean CheckPoint(Point p){
if( Math.abs(this.x - p.x )< 0.000001 && Math.abs(this.y - p.y )< 0.000001){
return true;
}
else
return false;
}
}
static class Line{
Point a;
Point b;
public Line() {
}
public Line(Point a, Point b) {
this.a = a;
this.b = b;
}
public boolean xch(Line N){//线重合
return this.a.dxDistance(N) == 0 && this.b.dxDistance(N) == 0;
}
public Point Get_jd(Line N){//两直线交点
double x1,x2,x3,x4,y1,y2,y3,y4;
x1 = this.a.x;x2=this.b.x;x3=N.a.x;x4=N.b.x;
y1 = this.a.y;y2=this.b.y;y3=N.a.y;y4=N.b.y;
double m = (y2-y1)*(x3-x4)-(y4-y3)*(x1-x2);
double x = ((x4 * y3 - x3 * y4)*(x1 - x2) - (x2 * y1 - x1 * y2)*(x3 - x4)) / m;
double y = ((x2 * y1 - x1 * y2)*(y4 - y3) - (x4 * y3 - x3 * y4)*(y2 - y1)) / m;
return new Point(x,y);
}
public boolean Line_px(Line N){//两直线平行
double x1,y1,x2,y2,x3,y3,x4,y4;
x1 = this.a.x; x2 = this.b.x; x3 = N.a.x; x4 = N.b.x;
y1 = this.a.y; y2 = this.b.y; y3 = N.a.y; y4 = N.b.y;
double k1,k2;
if(x1==x2 && x3 == x4){
return true;
}
else {
k1 = (y2-y1)/(x2-x1);
k2 = (y4-y3)/(x4-x3);
return k1 == k2;
}
}
public double Line_length(){
return Math.sqrt((this.a.x- this.b.x)*(this.a.x- this.b.x)+(this.a.y- this.b.y)*(this.a.y- this.b.y));
}
public void setA(Point a) {
this.a = a;
}
public void setB(Point b) {
this.b = b;
}
public boolean istongce(Point E, Point F) {
double x1,x2;
double A = this.b.y - this.a.y;
double B = this.a.x - this.b.x;
double C = this.b.x * this.a.y - this.a.x * this.b.y;
x1 = A * E.x + B * E.y + C;
x2 = A * F.x + B * F.y + C;
if(x1 * x2 > 0)
return true;
else
return false;
}
}
public static double sjS(Point b, Point c, Point d){
double d1 = b.Distance(c);
double d2 = c.Distance(d);
double d3 = d.Distance(b);
double C = d1 + d2 +d3;
double S = Math.abs(Math.pow((C/2)*(C/2-d1)*(C/2-d2)*(C/2-d3),0.5));
double s = Double.parseDouble(String.format("%.3f",S));
return s;
}
static class method{
public static double sswr(double s) {
String str=String.valueOf(s);
String substring = str.substring(str.indexOf(".") + 1);
int str_len;
if(substring.length()>3)
{
str_len=3;
}else {
str_len=substring.length();
}
String formats="%."+str_len+"f";
String out=String.format(formats,s);
double res=Double.parseDouble(out);
return res;
}
public static double Ssbx(Point c, Point d, Point e, Point f){
double ab = c.Distance(d); double bc = d.Distance(e);
double cd = e.Distance(f); double da = f.Distance(c);
double s = (ab+bc+cd+da)/2;
double S = Math.sqrt((s-ab)*(s-bc)*(s-cd)*(s-da));
return S;
}
public static boolean superposition(Point a,Point b){
if(a.x == b.x && a.y == b.y){
return true;
}
else
return false;
}
public static ArrayList<Point> get_point_from_input(ArrayList<Double> list){
ArrayList<Point> points = new ArrayList<>();
for (int i = 0; i < list.size(); i=i+2) {
Point newpoint = new Point(list.get(i),list.get(i+1));
points.add(newpoint);
}
return points;
}
public static double format_output(double a){
return Double.parseDouble(String.format("%.3f",a));
}
public static ArrayList<Point> delete_Point(ArrayList<Point> points){
for (int i = 0; i < points.size(); i++) {
for (int j = i+1; j < points.size(); j++) {
if(points.get(i).Same_Point(points.get(j))){
points.remove(j);
}
}
}
for (int i = 0; i < points.size()-2; i++) {
if(points.get(i%points.size()).PPP_IsOn_SanmeLine(points.get((i+1)%points.size()),points.get((i+2)%points.size()))){
if(points.get(i%points.size()).is_Online(new Line(points.get((i+1)%points.size()),points.get((i+2)%points.size())))){
points.remove(i%points.size());
}
if(points.get((i+1)%points.size()).is_Online(new Line(points.get((i)%points.size()),points.get((i+2)%points.size())))){
points.remove((i+1)%points.size());
}
if(points.get((i+2)%points.size()).is_Online(new Line(points.get(i%points.size()),points.get((i+1)%points.size())))){
points.remove((i+2)%points.size());
}
}
}
return points;
}//3:6,0 6,6 0,0 6,0 8,0 8,3 8,6
public static void mjsc(double S,double s1){
double s2 = S-s1;
double S1 = Double.parseDouble(String.format("%.3f",s1));
double S2 = Double.parseDouble(String.format("%.3f",s2));
System.out.printf(min(S1, S2) + " " + max(S1, S2));
}//输出最大面积+最小面积
}
static class Graphics{
public ArrayList<Point> Graphics_point = new ArrayList<>();
public ArrayList<Line> Graphics_line = new ArrayList<>();
public boolean Is_convex() {//2:0,0 1,0 2,1 1,2 0,2
boolean flag = false;
for (int i = 0; i < this.Graphics_line.size(); i++) {
int cot = i;
ArrayList<Point> newlist = new ArrayList<>(this.Graphics_point);
if (i == this.Graphics_point.size() - 1) {
newlist.remove(i);
newlist.remove(0);
} else {
newlist.remove(i);
newlist.remove(i);
}
for (int j = 0; j < newlist.size()-1; j++) {
if (!this.Graphics_line.get(i).istongce(newlist.get(j), newlist.get(j+1)))
return false;
}
}
return true;
}
public double C_Graphics(){
double C = 0.0;
for (Line line : this.Graphics_line) {
C = C + line.a.Distance(line.b);
}
return C;
}
// public double area(){//凸多边形面积
// double S1 = 0.0;double S2 = 0.0;
// for (int i = 0; i < this.Graphics_point.size()-2; i++) {
// int j = i + 1;
// double s = area_triangle(this.Graphics_point.get(0), this.Graphics_point.get(j), this.Graphics_point.get(j + 1));
// S1 += s;
// }
// return S1;
// }
public boolean line_Is_onGraphics(Line M){
boolean flag = false;
for (Line line : Graphics_line) {
if (M.a.dxDistance(line) == 0 && M.b.dxDistance(line) == 0) {
flag = true;
break;
}
}
return flag;
}
public void setGraphics_point(ArrayList<Point> Graphics_point) {
this.Graphics_point = Graphics_point;
}
public ArrayList<Line> getGraphics_line() {
return Graphics_line;
}
public ArrayList<Line> setGraphics_P_Line(){//需要将点集和线集初始化
ArrayList<Line> G_Line = new ArrayList<>();
for (int i = 0; i < this.Graphics_point.size(); i++) {
Line newline = new Line(this.Graphics_point.get(i%this.Graphics_point.size()),
this.Graphics_point.get((i+1)%this.Graphics_point.size()));
// System.out.println(newline.a.x+","+newline.a.y+" "+newline.b.x+","+newline.b.y);
G_Line.add(newline);
}
return G_Line;
}
public void setGraphics_Line(ArrayList<Line> polygon_Line) {
this.Graphics_line = polygon_Line;
}
public ArrayList<Point> get_jd(Line M){
ArrayList<Point> jiaodian = new ArrayList<>();
//线重合
for (int i = 0; i < this.Graphics_line.size(); i++) {
if(M.xch(this.Graphics_line.get(i))){
return jiaodian;
}
}
for (int i = 0; i < this.Graphics_line.size(); i++) {
if(!M.Line_px(this.Graphics_line.get(i))){
Point jd = M.Get_jd(this.Graphics_line.get(i));
// System.out.println(getline2jdx+" "+getline2jdy+" 线2");
boolean flagjd2 = jd.isOnLine(this.Graphics_line.get(i));
// System.out.println(flagjd2);
if(flagjd2){
if(jiaodian.size()==0){
jiaodian.add(jd);
}
else {
int flag = 1;
for (int j = 0; j < jiaodian.size(); j++) {
if(jiaodian.get(j).Same_Point(jd))
{
flag=0;
// System.out.printf("2重复啦");
break;
}
}
if(flag == 1){
jiaodian.add(jd);
}
}
}
}
continue;
}
return jiaodian;
}
}
static class triangle extends Graphics{//三角形
public void OneTriangle(Line line){
List<Line> lineList = new ArrayList<>();
lineList.add(this.Graphics_line.get(0));lineList.add(this.Graphics_line.get(1));lineList.add(this.Graphics_line.get(2));
Line l1 = this.Graphics_line.get(0);Line l2 = this.Graphics_line.get(1); Line l3 = this.Graphics_line.get(2);
List<Point> list = new ArrayList<>();
int cnt = 0;
Point u;
for(int i=0;i<lineList.size();i++){
u = lineList.get(i).Get_jd(line);
boolean flag = false;
if( u != null){
if(u.is_Online(lineList.get(i))){
flag = true;
}
for (Point p : list){
if( p.CheckPoint(u) ){
flag = false;
break;
}
}
if(flag){
list.add(u);
//System.out.println(u.getX() + " " + u.getY() );
cnt++;
}
}
}
if(cnt < 2){
System.out.println(cnt);
}
else {
double s1,s2,s;
System.out.print("2 ");
Line lline = new Line(list.get(0), list.get(1));
Point p1 = list.get(0),p2 = list.get(1);
Point m;
if(p1.dxDistance(l1)==0 && p2.dxDistance(l2)==0 || p2 .dxDistance(l2)==0 ||p2.dxDistance(l2)==0 && p1.dxDistance(l1)==0)
m = l1.b;
else if(p1.dxDistance(l1)==0 && p2.dxDistance(l3)==0 || p2.dxDistance(l3)==0 && p1.dxDistance(l1)==0 )
m = l1.a;
else
m = l2.b;
triangle triangle1 = new triangle(m,list.get(0),list.get(1));
s1 = triangle1.area();
s1 = method.sswr(s1);
s = this.area();
s = method.sswr(s);
s2 = s - s1;
s2 = method.sswr(s2);
System.out.println(Math.min(s1,s2) + " " + Math.max(s1,s2));
}
}
public double area() {
double s = this.C_Graphics()/2;
return Math.sqrt(s * (s - this.triangle_line.get(0).Line_length()) *
(s - this.triangle_line.get(1).Line_length()) * (s - this.triangle_line.get(2).Line_length()));
}
public ArrayList<Point> triangle_point;
public ArrayList<Line> triangle_line;
public triangle(ArrayList<Point> triangle_point){
this.triangle_point = triangle_point;
}
public triangle(Point a, Point b, Point c) {
ArrayList<Point> list =new ArrayList<>();
list.add(a);list.add(b);list.add(c);
ArrayList<Line> list1 = new ArrayList<>();
Line AB = new Line(a,b);
Line BC = new Line(b,c);
Line CA = new Line(c,a);
list1.add(AB);list1.add(BC);list1.add(CA);
this.triangle_point = list;
this.Graphics_point = list;
this.triangle_line = list1;
this.Graphics_line = list1;
}
public static double area(Point a, Point b, Point c){
double d1 = a.Distance(b);
double d2 = b.Distance(c);
double d3 = c.Distance(a);
double C = d1 + d2 +d3;
return Math.abs(Math.pow((C/2)*(C/2-d1)*(C/2-d2)*(C/2-d3),0.5));
}
public boolean PD(){
if(this.triangle_point.size()<3){
return false;
}
if(this.triangle_point.get(0).PPP_IsOn_SanmeLine(this.triangle_point.get(1), this.triangle_point.get(2))){
return false;
}
return true;
}
public void setTriangle_point(ArrayList<Point> triangle_point) {
this.triangle_point = triangle_point;
}
public void setTriangle_line(ArrayList<Line> triangle_line) {
this.triangle_line = triangle_line;
}
public boolean sjxpd(ArrayList<Point> points) {
Point a = points.get(0), b = points.get(1), c = points.get(2), d = points.get(3);
Line AB = new Line(a,b);
Line BC = new Line(b,c);
Line CA = new Line(c,a);
Line BD = new Line(b,d);
if(d.dxDistance(AB)==0&&c.dxDistance(AB)!=0){
if(a.Same_Point(d)||b.Same_Point(d)){
return true;
}else{
return a.isOnLine(BD);
}
}
else if (d.dxDistance(BC)==0&&a.dxDistance(BC)!=0) {
if(b.Same_Point(d)||c.Same_Point(d)){
return true;
}else{
return c.isOnLine(BD);
}
} else if(d.dxDistance(CA)==0&&b.dxDistance(CA)!=0){
if(c.Same_Point(d)||a.Same_Point(d)){
return true;
}else{
return d.isOnLine(CA);
}
}
else if(a.dxDistance(BC)==0&&d.dxDistance(BC)!=0){
if(a.Same_Point(b)||c.Same_Point(a)){
return true;
}else{
return b.isOnLine(CA);
}
}else
return false;
}
}
static class quadrilateral extends Graphics{//四边形
public ArrayList<Point> quadrilateral_point = new ArrayList<>();
public ArrayList<Line> quadrilateral_line = new ArrayList<>();
public quadrilateral(ArrayList<Point> quadrilateral_point){
this.quadrilateral_point = quadrilateral_point;
}
public quadrilateral(Point a, Point b, Point c, Point d) {
this.quadrilateral_point.add(a);this.quadrilateral_point.add(b);this.quadrilateral_point.add(c);this.quadrilateral_point.add(d);
}
public double quadrilateral_area() {
triangle triangle01 = new triangle(this.quadrilateral_line.get(0).a, this.quadrilateral_line.get(0).b, this.quadrilateral_line.get(1).b);
triangle triangle02 = new triangle(this.quadrilateral_line.get(2).a, this.quadrilateral_line.get(2).b, this.quadrilateral_line.get(3).b);
triangle triangle03 = new triangle(this.quadrilateral_line.get(1).a, this.quadrilateral_line.get(1).b, this.quadrilateral_line.get(2).b);
triangle triangle04 = new triangle(this.quadrilateral_line.get(3).a, this.quadrilateral_line.get(3).b, this.quadrilateral_line.get(0).b);
double qarea01,qarea02;
qarea01 = triangle01.area() + triangle02.area();
qarea02 = triangle03.area() + triangle04.area();
if(qarea02 < qarea01)//凹四边形面积比较特殊,要找凹点,这里偷懒直接判段小的输出
return qarea02;
else
return qarea01;
}
public void fengeArea(Point a, Point b, Point c) {
triangle tr1 = new triangle(a,b,c);
double area1 = tr1.area();
double area = this.area();
double area2 = area - area1;
area1 = method.sswr(area1);
area2 = method.sswr(area2);
System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
}
public double area() {
triangle triangle01 = new triangle(this.quadrilateral_line.get(0).a, this.quadrilateral_line.get(0).b,this.quadrilateral_line.get(1).b);
triangle triangle02 = new triangle(this.quadrilateral_line.get(2).a, this.quadrilateral_line.get(2).b,this.quadrilateral_line.get(3).b);
triangle triangle03 = new triangle(this.quadrilateral_line.get(1).a, this.quadrilateral_line.get(1).b,this.quadrilateral_line.get(2).b);
triangle triangle04 = new triangle(this.quadrilateral_line.get(3).a, this.quadrilateral_line.get(3).b,this.quadrilateral_line.get(0).b);
double qarea01,qarea02;
qarea01 = triangle01.area() + triangle02.area();
qarea02 = triangle03.area() + triangle04.area();
if(qarea02 < qarea01)//凹四边形面积比较特殊,要找凹点,这里偷懒直接判段小的输出
return qarea02;
else
return qarea01;
}
public boolean Is_quadrilateral() {
if(this.quadrilateral_point.size()<4){
return false;
}
if (this.quadrilateral_point.get(0).PPP_IsOn_SanmeLine(this.quadrilateral_point.get(1), this.quadrilateral_point.get(2)) ||
this.quadrilateral_point.get(0).PPP_IsOn_SanmeLine(this.quadrilateral_point.get(1), this.quadrilateral_point.get(3)) ||
this.quadrilateral_point.get(0).PPP_IsOn_SanmeLine(this.quadrilateral_point.get(2), this.quadrilateral_point.get(3)) ||
this.quadrilateral_point.get(1).PPP_IsOn_SanmeLine(this.quadrilateral_point.get(2), this.quadrilateral_point.get(3))) {
return false;//2:0,0 2,1 0,4 8,2 8,0
}
else {
for (int i = 0; i < this.quadrilateral_line.size() - 1; i++) {
for (int j = i+1; j < this.quadrilateral_line.size(); j++) {
if(j-1!=i && (j+1)%this.quadrilateral_line.size()!=i)
{
Point JD = this.quadrilateral_line.get(i).Get_jd(this.quadrilateral_line.get(j));
if (JD.isOnLine(this.quadrilateral_line.get(i)) && JD.isOnLine(this.quadrilateral_line.get(j))) {
return false;
}
}
}
}
}
return true;
}
public void setQuadrilateral_line(ArrayList<Line> quadrilateral_line) {
this.quadrilateral_line = quadrilateral_line;
}
public void Onequarilateral(Line l) {
Point[] points = new Point[4];
ArrayList<Line> lines = new ArrayList<>();
lines.add(this.Graphics_line.get(0));lines.add(this.Graphics_line.get(1));
lines.add(this.Graphics_line.get(2));lines.add(this.Graphics_line.get(3));
int[] p_in = new int[4];
//判断交点在线上还是端点
for (int i = 0; i < 4; i++) {
p_in[i] = 0;
//求直线与四边形的交点
points[i] = lines.get(i).Get_jd(l);
if(points[i]!=null) {
//判断交点是否在边上,不包括端点
if(points[i].is_Online(this.Graphics_line.get(i)))
p_in[i] = 1;
}
}
//有顶角在线上
for (int i = 0; i < 4; i++) {
if(this.Graphics_point.get(i).dxDistance(l)==0) {
if(this.Graphics_point.get((i+2)%4).dxDistance(l)==0) {
fengeArea(this.Graphics_point.get(i), this.Graphics_point.get((i + 2) % 4), this.Graphics_point.get((i + 1) % 4));
return;
}
else if(p_in[(i+1)%4] == 1) {
fengeArea(this.Graphics_point.get(i), this.Graphics_point.get((i + 1) % 4), points[(i + 1) % 4]);
return;
}
else if(p_in[(i+2)%4] == 1) {
fengeArea(this.Graphics_point.get(i), this.Graphics_point.get((i + 3) % 4), points[(i + 2) % 4]);
return;
}
else {
System.out.println("1");
return;
}
}
}
//两个临边
for (int i = 0; i < 4; i++) {
if(p_in[i] == 1 && p_in[(i+1)%4] == 1) {
fengeArea(this.Graphics_point.get((i + 1) % 4), points[i], points[(i + 1) % 4]);
return;
}
}
//两个对边
for (int i = 0; i < 2; i++) {
if(p_in[i] == 1 && p_in[i+2] == 1) {
triangle tr1 = new triangle(this.Graphics_point.get(i), points[i], points[(i + 2) % 4]);
triangle tr2 = new triangle(this.Graphics_point.get((i+3)%4), this.Graphics_point.get(i), points[(i + 2) % 4]);
double area1 = tr1.area() + tr2.area();
System.out.println("1:"+area1);
double area = this.area();
System.out.println("2:"+area);
double area2 = area - area1;
area1 = method.sswr(area1);
area2 = method.sswr(area2);
System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
return;
}
}
System.out.println("0");
return;
}
}
static class pentagon extends Graphics{//五边形
public void fengeArea(Point a, Point b, Point c) {
triangle tr1 = new triangle(a,b,c);
double area1 = tr1.area();
double area = this.area();
double area2 = area - area1;
area1 = method.sswr(area1);
area2 = method.sswr(area2);
System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
}
public void fengeArea(Point a, Point b, Point c, Point d) {
quadrilateral ql1 = new quadrilateral(a, b, c, d);
double area1 = ql1.area();
double area = this.area();
double area2 = area - area1;
area1 = method.sswr(area1);
area2 = method.sswr(area2);
System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
}
private ArrayList<Point> pentagon_point;
private ArrayList<Line> pentagon_line;
public pentagon(ArrayList<Point> pentagon_point){
this.pentagon_point = pentagon_point;
}
public boolean Is_pentagon() {
if(this.pentagon_point.size()<5){
return false;
}
if (this.pentagon_point.get(0).PPP_IsOn_SanmeLine(this.pentagon_point.get(1), this.pentagon_point.get(2)) ||
this.pentagon_point.get(1).PPP_IsOn_SanmeLine(this.pentagon_point.get(2), this.pentagon_point.get(3)) ||
this.pentagon_point.get(2).PPP_IsOn_SanmeLine(this.pentagon_point.get(3), this.pentagon_point.get(4)) ||
this.pentagon_point.get(3).PPP_IsOn_SanmeLine(this.pentagon_point.get(4), this.pentagon_point.get(0))) {
return false;//2:0,0 2,1 0,4 8,2 8,0
}
else {
for (int i = 0; i < this.pentagon_line.size() - 1; i++) {
for (int j = i+1; j < this.pentagon_line.size(); j++) {
if(j-1!=i && (j+1)%this.pentagon_line.size()!=i)
{
Point JD = this.pentagon_line.get(i).Get_jd(this.pentagon_line.get(j));
if (JD.isOnLine(this.pentagon_line.get(i)) && JD.isOnLine(this.pentagon_line.get(j))) {
return false;
}
}
}
}
}
return true;
}
public void Onepentagon(Line line) {
Point[] pn = new Point[5];
int[] p_in = new int[5];
for (int i = 0; i < this.Graphics_line.size(); i++) {
p_in[i] = 0;//初始没有交点
pn[i] = this.Graphics_line.get(i).Get_jd(line);//求边于线是否有交点
if(pn[i] != null) {
//判断交点是否在边上,不包括端点
if(pn[i].is_Online(this.Graphics_line.get(i)))
p_in[i] = 1;
}
}
//有顶角在线上
for (int i = 0; i < this.Graphics_point.size(); i++) {
if(this.Graphics_point.get(i).dxDistance(line)==0) {
for (int j = 0; j < this.Graphics_point.size()-3; j++) {//直线通过对角切割
if(this.Graphics_point.get((i+j+2)%this.Graphics_point.size()).dxDistance(line)==0) {
if(j==0) {
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get((i+j+2)%this.Graphics_point.size()
), this.Graphics_point.get((i+j+2-1)%this.Graphics_point.size()));
return;
}
else {
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get((i+j+2)%this.Graphics_point.size()
), this.Graphics_point.get((i+j+2+1)%this.Graphics_point.size()));
return;
}
}
}
for (int j = 0; j < this.Graphics_line.size()-2; j++) {
if(p_in[(i+j+1)%this.Graphics_line.size()] == 1) {
if(i+j+1>1 && i+j+1<this.Graphics_point.size()-2){
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get((i+j+1-1)% this.Graphics_point.size()),
this.Graphics_point.get((i+j+1)%this.Graphics_point.size()), pn[((i+j+1)%this.Graphics_line.size())]);
return;
}
else{
if(j==0){
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get((i+j+1)%this.Graphics_point.size()
), pn[(i+j+1)%this.Graphics_line.size()]);
return;
}
else if(i+j+1==this.Graphics_line.size()-2) {
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get(((i+j+1)+1)%this.Graphics_point.size()),pn[(i+j+1)%this.Graphics_line.size()]);
return;
}
}
}
}
System.out.println("1");
return;
}
}
//两个临边
for (int i = 0; i < this.Graphics_point.size(); i++) {
if(p_in[i] == 1 && p_in[(i+1)% this.Graphics_line.size()] == 1) {
fengeArea(this.Graphics_point.get((i + 1) % this.Graphics_point.size()), pn[i], pn[(i + 1) % this.Graphics_point.size()]);
return;
}
}
}
public double area(){
double a=0;
for (int i = 1; i < this.Graphics_point.size()-1; i++) {
triangle t = new triangle(this.Graphics_point.get(0), this.Graphics_point.get(i), this.Graphics_point.get(i+1));
a+=t.area();
}
return method.sswr(a);
}
}
}
④报表
⑤总结与分析
关于学习正确使用正则表达式的博客以及我学习正则表达式的相关视频我会在总结中给出。这道题还是跟前两题的关联还是蛮大的。需要注意的就是选项三中的分割面积,考虑清楚全部情况,不要漏了某一种情况。
题目集5——7-2 点线形系列5-凸五边形的计算-2
①题目及分析
用户输入一组选项和数据,进行与五边形有关的计算。
以下五边形顶点的坐标要求按顺序依次输入,连续输入的两个顶点是相邻顶点,第一个和最后一个输入的顶点相邻。
选项包括:
4:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),判断它们两个之间是否存在包含关系(一个多边形有一条或多条边与另一个多边形重合,其他部分都包含在另一个多边形内部,也算包含)。
两者存在六种关系:1、分离(完全无重合点) 2、连接(只有一个点或一条边重合) 3、完全重合 4、被包含(前一个多边形在后一个多边形的内部)5、交错 6、包含(后一个多边形在前一个多边形的内部)。
各种关系的输出格式如下:
1、no overlapping area between the previous triangle/quadrilateral/ pentagon and the following triangle/quadrilateral/ pentagon
2、the previous triangle/quadrilateral/ pentagon is connected to the following triangle/quadrilateral/ pentagon
3、the previous triangle/quadrilateral/ pentagon coincides with the following triangle/quadrilateral/ pentagon
4、the previous triangle/quadrilateral/ pentagon is inside the following triangle/quadrilateral/ pentagon
5、the previous triangle/quadrilateral/ pentagon is interlaced with the following triangle/quadrilateral/ pentagon
6、the previous triangle/quadrilateral/ pentagon contains the following triangle/quadrilateral/ pentagon
5:输入十个点坐标,前、后五个点分别构成一个凸多边形(三角形、四边形、五边形),输出两个多边形公共区域的面积。注:只考虑每个多边形被另一个多边形分割成最多两个部分的情况,不考虑一个多边形将另一个分割成超过两个区域的情况。
6:输入六个点坐标,输出第一个是否在后五个点所构成的多边形(限定为凸多边形,不考虑凹多边形),的内部(若是五边形输出in the pentagon/outof the pentagon,若是四边形输出in the quadrilateral/outof the quadrilateral,若是三角形输出in the triangle/outof the triangle)。输入入错存在冗余点要排除,冗余点的判定方法见选项5。如果点在多边形的某条边上,输出"on the triangle/on the quadrilateral/on the pentagon"。
以上4、5、6选项输入的五个点坐标可能存在冗余,假设多边形一条边上两个端点分别是x、y,边线中间有一点z,另一顶点s:
1)符合要求的输入:顶点重复或者z与xy都相邻,如:x x y s、x z y s、x y x s、s x y y。此时去除冗余点,保留一个x、一个y。
2) 不符合要求的输入:z不与xy都相邻,如:z x y s、x z s y、x s z y
输入格式:
基本格式:选项+":"+坐标x+","+坐标y+" "+坐标x+","+坐标y。点的x、y坐标之间以英文","分隔,点与点之间以一个英文空格分隔。
输出格式:
输出的数据若小数点后超过3位,只保留小数点后3位,多余部分采用四舍五入规则进到最低位。小数点后若不足3位,按原始位数显示,不必补齐。例如:1/3的结果按格式输出为 0.333,1.0按格式输出为1.0
②分析
这道题,我没做出来所以这里只写了一点点。这里就不解释代码了。太菜了不敢多索话。
③代码
点击查看代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class Main{
public static void main(String[] args) {
String str = input.input_String();
boolean flag = input.input_check(str);
if(flag&&str.charAt(1)==':'){
ArrayList<Double> Point_List = input.Point_list(str);
switch (str.charAt(0)){
case '1' :
if(Point_List.size()!=10){
System.out.println("wrong number of points");
}
else{
ArrayList<Point> points = method.get_point_from_input(Point_List);
pentagon W = new pentagon(points);
W.setGraphics_point(points);
W.pentagon_line = W.setGraphics_P_Line();
// W.setGraphics_Line(W.pentagon_line);
// for (int i = 0; i < W.pentagon_point.size(); i++) {
// System.out.println(W.pentagon_point.get(i).x+" "+W.pentagon_point.get(i).y);
// }
// for (int i = 0; i < W.pentagon_line.size(); i++) {
// System.out.println(W.pentagon_line.get(i).a.x+","+W.pentagon_line.get(i).a.y+
// " "+W.pentagon_line.get(i).b.x+","+W.pentagon_line.get(i).b.y);
// }
if(W.Is_pentagon()){
System.out.println("true");
}else{
System.out.println("false");
}
}
break;
case '2' :
if(Point_List.size()!=10){
System.out.println("wrong number of points");
}
else{
ArrayList<Point> points = method.get_point_from_input(Point_List);
pentagon W = new pentagon(points);
W.setGraphics_point(W.pentagon_point);
W.pentagon_line = W.setGraphics_P_Line();
W.setGraphics_Line(W.pentagon_line);
if(W.Is_pentagon()){
if(W.Is_convex()){
System.out.print("true"+" ");
double C = method.format_output(W.C_Graphics());
double S = method.format_output(W.area());
System.out.println(C+" "+S);
}else{
System.out.print("false");
}
}
else{
System.out.println("not a pentagon");
}
}
break;
case '3' :
if(Point_List.size()!=14){
System.out.println("wrong number of points");
}
else{
ArrayList<Point> points = method.get_point_from_input(Point_List);
Line line = new Line(points.get(0), points.get(1));
if(points.get(0).Same_Point(points.get(1))) {
System.out.println("points coincide");
System.exit(0);
}
points.remove(0);points.remove(0);
ArrayList<Point> new_points = method.delete_Point(points);
pentagon pg = new pentagon(new_points);
pg.setGraphics_point(pg.pentagon_point);
pg.pentagon_line = pg.setGraphics_P_Line();
pg.setGraphics_Line(pg.pentagon_line);
if(new_points.size() == 5){
ArrayList<Point> list1 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
list1.add(new_points.get(i));
}
if(pg.line_Is_onGraphics(line))
System.out.println("The line is coincide with one of the lines");
else
pg.Onepentagon(line);
}
else if(new_points.size() == 4){
quadrilateral ql = new quadrilateral(new_points);
ql.setGraphics_point(ql.quadrilateral_point);
ql.quadrilateral_line = pg.setGraphics_P_Line();
ql.setGraphics_Line(ql.quadrilateral_line);
if(ql.line_Is_onGraphics(line))
System.out.println("The line is coincide with one of the lines");
else {
ql.Onequarilateral(line);
}
}
else if(new_points.size() == 3) {
triangle tl = new triangle(new_points);
tl.setGraphics_point(tl.triangle_point);
tl.triangle_line = pg.setGraphics_P_Line();
tl.setGraphics_Line(tl.triangle_line);
// for (int i = 0; i < tl.Graphics_point.size(); i++) {
// System.out.println(tl.Graphics_point.get(i).x+","+tl.Graphics_point.get(i).y);
// }
// for (int i = 0; i < tl.triangle_point.size(); i++) {
// System.out.println(tl.triangle_point.get(i).x+","+tl.triangle_point.get(i).y);
// }
// for (int i = 0; i < tl.triangle_line.size(); i++) {
// System.out.println(tl.Graphics_line.get(i).a.x+","+tl.Graphics_line.get(i).a.y+" "+tl.Graphics_line.get(i).b.x+","+tl.Graphics_line.get(i).b.y+" ");
// }
// for (int i = 0; i < tl.triangle_line.size(); i++) {
// System.out.println(tl.triangle_line.get(i).a.x+","+tl.triangle_line.get(i).a.y+" "+tl.triangle_line.get(i).b.x+","+tl.triangle_line.get(i).b.y+" ");
// }
if(tl.line_Is_onGraphics(line))
System.out.println("The line is coincide with one of the lines");
else {
tl.OneTriangle(line);
}
}
else {
System.out.println("not a polygon");
}
}
break;
case '4':
if(Point_List.size()!=20){
System.out.println("wrong number of points");
}
else{
ArrayList<Point> points = method.get_point_from_input(Point_List);
ArrayList<Point> list1 = new ArrayList<>();
for (int i = 0; i < points.size()/2; i++) {
list1.add(points.get(i));
}
ArrayList<Point> list2 = new ArrayList<>();
for (int i = 5; i < points.size(); i++) {
list2.add(points.get(i));
}
ArrayList<Point> new_list1 = method.delete_Point(list1);
ArrayList<Point> new_list2 = method.delete_Point(list2);
Graphics tx1 = Graphics.create(new_list1);
tx1.setGraphics_point(new_list1);
tx1.Graphics_line = tx1.getGraphics_line();
Graphics tx2 = Graphics.create(new_list2);
tx2.setGraphics_point(new_list2);
tx2.Graphics_line = tx2.getGraphics_line();
tx1.BJ_Graphics(tx2);
}
break;
case '5':
if(Point_List.size()!=20){
System.out.println("wrong number of points");
}
else{
ArrayList<Point> points = method.get_point_from_input(Point_List);
ArrayList<Point> list1 = new ArrayList<>();
for (int i = 0; i < points.size()/2; i++) {
list1.add(points.get(i));
}
ArrayList<Point> list2 = new ArrayList<>();
for (int i = 5; i < points.size(); i++) {
list2.add(points.get(i));
}
ArrayList<Point> new_list1 = method.delete_Point(list1);
ArrayList<Point> new_list2 = method.delete_Point(list2);
Graphics tx1 = Graphics.create(new_list1);
tx1.setGraphics_point(new_list1);
tx1.Graphics_line = tx1.getGraphics_line();
Graphics tx2 = Graphics.create(new_list2);
tx2.setGraphics_point(new_list1);
tx2.Graphics_line = tx2.getGraphics_line();
}
break;
case '6':
if(Point_List.size()!=12){
System.out.println("wrong number of points");
}
else{
ArrayList<Point> points = method.get_point_from_input(Point_List);
Point M = points.get(0);
points.remove(0);
ArrayList newlist = method.delete_Point(points);
if(newlist.size()==5){
pentagon W = new pentagon(newlist);
W.setGraphics_point(newlist);
W.pentagon_line = W.setGraphics_P_Line();
W.setGraphics_Line(W.pentagon_line);
if(W.Is_pentagon()){
if(W.Point_Is_onGraphics(M)){
System.out.println("on the pentagon");
}else if(W.Point_Is_inGraphics(M)){
System.out.println("in the pentagon");
}else{
System.out.println("outof the pentagon");
}
}
}else if(newlist.size()==4){//6:6.01,1 0,0 6,0 7,0 8,3 6,6
quadrilateral ql = new quadrilateral(newlist);
ql.setGraphics_point(newlist);
ql.quadrilateral_line = ql.setGraphics_P_Line();
ql.setGraphics_Line(ql.quadrilateral_line);
if(ql.Is_quadrilateral()){
if(ql.Point_Is_onGraphics(M)){
System.out.println("on the quadrilateral");
}else if(ql.Point_Is_inGraphics(M)){
System.out.println("in the quadrilateral");
}else {
System.out.println("outof the quadrilateral");
}
}
}else if(newlist.size()==3){
triangle tr = new triangle(newlist);
tr.setGraphics_point(newlist);
tr.triangle_line = tr.setGraphics_P_Line();
tr.setGraphics_Line(tr.triangle_line);
if(!tr.Graphics_point.get(0).PPP_IsOn_SanmeLine(tr.Graphics_point.get(1),tr.Graphics_point.get(2))){
if(tr.Point_Is_onGraphics(M)){
System.out.println("on the triangle");
}else if(tr.Point_Is_inGraphics(M)){
System.out.println("in the triangle");
}else {
System.out.println("outof the triangle");
}
}
}
}
break;
default:
System.out.println("wrong format");
System.exit(0);
}
}
else{
System.out.println("wrong format");
System.exit(0);
}
}
static class input{
public static String input_String(){
Scanner set = new Scanner(System.in);
return set.nextLine();
}
public static boolean input_check(String str){
//结尾输入:应该返回wrong format;
String[] str1 = str.split(":");
if (str1.length != 2) {
System.out.print("Wrong Format");
System.exit(0);
}
String str2 = str1[1];
// System.out.printf(str2);
String[] str3 = str2.split(" ");
for (String d : str3) {
String[] x = d.split(",");
if (x.length != 2) {
System.out.print("Wrong Format");
System.exit(0);
}
for (String j : x) {
if (!j.matches("^[+-]?(0|(0\\.\\d+)?|[1-9][0-9]*(\\.\\d+)?)$")) {
System.out.print("Wrong Format");
System.exit(0);
}
}
}
return true;
}
public static ArrayList<Double> Point_list(String str){
String[] str1 = str.split(":");
String str2 = str1[1];
ArrayList<String> list = new ArrayList<>();
ArrayList<Double> list1 = new ArrayList<>();
int index = 0;
String record;
for (int j= 0; j < str2.length(); j++) {
if (str2.charAt(j) == ' ' || str2.charAt(j) == ',') {
if (index == 0) {
record = str2.substring(index, j);
} else {
record = str2.substring(index + 1, j);
}
list.add(record);
index = j;
}
}
if((str2.charAt(str2.length()-1)!=' ')){
record = str2.substring(index + 1);
list.add(record);
}
for (String s : list) {
double g = Double.parseDouble(s);
list1.add(g);
}
return list1;
}
}
static class Point{
public double x;
public double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public boolean Same_Point(Point b){
return this.x == b.x && this.y == b.y;
}
public double Distance(Point b){
return Math.sqrt((b.x-this.x)*(b.x-this.x)+(b.y-this.y)*(b.y-this.y));
}
public double dxDistance(Line M){
double d;
double x1,x2,x3,y1,y2,y3;
x1=this.x;y1=this.y;x2=M.a.x;y2=M.a.y;x3=M.b.x;y3=M.b.y;
d=Math.abs((y3-y2)*x1+(x2-x3)*y1+x3*y2-y3*x2) /
Math.sqrt(Math.pow(y3-y2, 2)+Math.pow(x3-x2, 2));
return d;
}
public void setX(double x) {
this.x = x;
}
public void setY(double y) {
this.y = y;
}
public boolean isOnLine(Line M){
double d1 = this.Distance(M.a);
double d2 = this.Distance(M.b);
double D = M.a.Distance(M.b);
return Math.abs(d1 + d2 - D) < 0.000001;
}
public boolean is_Online(Line M){
double d1 = this.Distance(M.a);
double d2 = this.Distance(M.b);
double D = M.a.Distance(M.b);
return Math.abs(d1 + d2 - D) < 0.000001 && !this.Same_Point(M.a) && !this.Same_Point(M.b);
}
public boolean PPP_IsOn_SanmeLine(Point A,Point B){
Line AB = new Line(A,B);
return this.dxDistance(AB) == 0;
}
public boolean CheckPoint(Point p){
if( Math.abs(this.x - p.x )< 0.000001 && Math.abs(this.y - p.y )< 0.000001){
return true;
}
else
return false;
}
}
static class Line{
Point a;
Point b;
public Line() {
}
public Line(Point a, Point b) {
this.a = a;
this.b = b;
}
public boolean xch(Line N){//线重合
return this.a.dxDistance(N) == 0 && this.b.dxDistance(N) == 0;
}
public Point Get_jd(Line N){//两直线交点
double x1,x2,x3,x4,y1,y2,y3,y4;
x1 = this.a.x;x2=this.b.x;x3=N.a.x;x4=N.b.x;
y1 = this.a.y;y2=this.b.y;y3=N.a.y;y4=N.b.y;
double m = (y2-y1)*(x3-x4)-(y4-y3)*(x1-x2);
double x = ((x4 * y3 - x3 * y4)*(x1 - x2) - (x2 * y1 - x1 * y2)*(x3 - x4)) / m;
double y = ((x2 * y1 - x1 * y2)*(y4 - y3) - (x4 * y3 - x3 * y4)*(y2 - y1)) / m;
return new Point(x,y);
}
public boolean Line_px(Line N){//两直线平行
double x1,y1,x2,y2,x3,y3,x4,y4;
x1 = this.a.x; x2 = this.b.x; x3 = N.a.x; x4 = N.b.x;
y1 = this.a.y; y2 = this.b.y; y3 = N.a.y; y4 = N.b.y;
double k1,k2;
if(x1==x2 && x3 == x4){
return true;
}
else {
k1 = (y2-y1)/(x2-x1);
k2 = (y4-y3)/(x4-x3);
return k1 == k2;
}
}
public double Line_length(){
return Math.sqrt((this.a.x- this.b.x)*(this.a.x- this.b.x)+(this.a.y- this.b.y)*(this.a.y- this.b.y));
}
public boolean istongce(Point E, Point F) {
double x1,x2;
double A = this.b.y - this.a.y;
double B = this.a.x - this.b.x;
double C = this.b.x * this.a.y - this.a.x * this.b.y;
x1 = A * E.x + B * E.y + C;
x2 = A * F.x + B * F.y + C;
if(x1 * x2 > 0)
return true;
else
return false;
}
}
public static double sjS(Point b, Point c, Point d){
double d1 = b.Distance(c);
double d2 = c.Distance(d);
double d3 = d.Distance(b);
double C = d1 + d2 +d3;
double S = Math.abs(Math.pow((C/2)*(C/2-d1)*(C/2-d2)*(C/2-d3),0.5));
double s = Double.parseDouble(String.format("%.3f",S));
return s;
}
static class method{
public static double sswr(double s) {
String str=String.valueOf(s);
String substring = str.substring(str.indexOf(".") + 1);
int str_len;
if(substring.length()>3)
{
str_len=3;
}else {
str_len=substring.length();
}
String formats="%."+str_len+"f";
String out=String.format(formats,s);
double res=Double.parseDouble(out);
return res;
}
public static double Ssbx(Point c, Point d, Point e, Point f){
double ab = c.Distance(d); double bc = d.Distance(e);
double cd = e.Distance(f); double da = f.Distance(c);
double s = (ab+bc+cd+da)/2;
double S = Math.sqrt((s-ab)*(s-bc)*(s-cd)*(s-da));
return S;
}
public static boolean superposition(Point a,Point b){
if(a.x == b.x && a.y == b.y){
return true;
}
else
return false;
}
public static ArrayList<Point> get_point_from_input(ArrayList<Double> list){
ArrayList<Point> points = new ArrayList<>();
for (int i = 0; i < list.size(); i=i+2) {
Point newpoint = new Point(list.get(i),list.get(i+1));
points.add(newpoint);
}
return points;
}
public static double format_output(double a){
return Double.parseDouble(String.format("%.3f",a));
}
//6:8.01,0 0,0 6,0 7,0 8,0 8,6
public static ArrayList<Point> delete_Point(ArrayList<Point> points){
for (int i = 0; i < points.size(); i++) {
for (int j = i+1; j < points.size(); j++) {
if(points.get(i).Same_Point(points.get(j))){
points.remove(j);
}
}
}
ArrayList<Point> list1 = new ArrayList<>();
list1.addAll(points);
for (int i = 0; i < points.size()-1;i++) {
if(points.get(i%points.size()).PPP_IsOn_SanmeLine(points.get((i+1)%points.size()),points.get((i+2)%points.size()))){
if(points.get(i%points.size()).is_Online(new Line(points.get((i+1)%points.size()),points.get((i+2)%points.size())))){
list1.remove(i%points.size());
}
else if(points.get((i+1)%points.size()).is_Online(new Line(points.get((i)%points.size()),points.get((i+2)%points.size())))){
list1.remove((i+1)%points.size());
}
else if(points.get((i+2)%points.size()).is_Online(new Line(points.get(i%points.size()),points.get((i+1)%points.size())))) {
list1.remove((i + 2) % points.size());
}
}
}
return list1;
}//3:6,0 6,6 0,0 6,0 8,0 8,3 8,6
public static void mjsc(double S,double s1){
double s2 = S-s1;
double S1 = Double.parseDouble(String.format("%.3f",s1));
double S2 = Double.parseDouble(String.format("%.3f",s2));
System.out.printf(min(S1, S2) + " " + max(S1, S2));
}//输出最大面积+最小面积
}
static class Graphics{
public ArrayList<Point> Graphics_point = new ArrayList<>();
public ArrayList<Line> Graphics_line = new ArrayList<>();
public boolean ch_Graphics(Graphics tx2){
for(Point str : tx2.Graphics_point) {
if(!this.Graphics_point.contains(str)) {
return false;
}
}
return true;
}
public boolean nb_Graphics(Graphics tx2){
for (int i = 0; i < this.Graphics_point.size(); i++) {
if(!tx2.Point_Is_inGraphics(this.Graphics_point.get(i)));
return false;
}return true;
}
public boolean isSameTo(Graphics g){
if(this.Graphics_point.size() != g.Graphics_point.size()) return false;
for(int i=0;i<this.Graphics_point.size();i++){
boolean ok = false;
for(int j=0;j<g.Graphics_point.size();j++){
if(this.Graphics_point.get(i).Same_Point(g.Graphics_point.get(j))) ok = true;
}
if(!ok) return false;
}
return true;
}
public void BJ_Graphics(Graphics tx2){
String name1 = null,name2=null;
if(this.Graphics_point.size()==3){
name1 = "triangle";
}
if(this.Graphics_point.size()==4){
name1 = "quadrilateral";
}
if(this.Graphics_point.size()==5){
name1 = "pentagon";
}
if(tx2.Graphics_point.size()==3){
name2 = "triangle";
}
if(tx2.Graphics_point.size()==4){
name2 = "quadrilateral";
}
if(tx2.Graphics_point.size()==5){
name2 = "pentagon";
}
if(this.isSameTo(tx2)){
System.out.println("the previous "+name1+" coincides with the following "+name2);
}
if(this.nb_Graphics(tx2)){
System.out.println("the previous "+name1+" is inside the following "+name2);
}
if(tx2.nb_Graphics(this)){
System.out.println("the previous "+name1+" contains the following "+name2);
}
}
public static Graphics create(ArrayList<Point> points) {
Graphics tx = null;
if(points.size() == 5){
tx = new pentagon(points);
}else if(points.size() ==4 ){
tx = new quadrilateral(points);
}else if(points.size() == 3){
tx = new pentagon(points);
}
return tx;
}
public boolean Is_convex() {//2:0,0 1,0 2,1 1,2 0,2
boolean flag = false;
for (int i = 0; i < this.Graphics_line.size(); i++) {
int cot = i;
ArrayList<Point> newlist = new ArrayList<>(this.Graphics_point);
if (i == this.Graphics_point.size() - 1) {
newlist.remove(i);
newlist.remove(0);
} else {
newlist.remove(i);
newlist.remove(i);
}
for (int j = 0; j < newlist.size()-1; j++) {
if (!this.Graphics_line.get(i).istongce(newlist.get(j), newlist.get(j+1)))
return false;
}
}
return true;
}
public double C_Graphics(){
double C = 0.0;
for (Line line : this.Graphics_line) {
C = C + line.a.Distance(line.b);
}
return C;
}
// public double area(){//凸多边形面积
// double S1 = 0.0;double S2 = 0.0;
// for (int i = 0; i < this.Graphics_point.size()-2; i++) {
// int j = i + 1;
// double s = area_triangle(this.Graphics_point.get(0), this.Graphics_point.get(j), this.Graphics_point.get(j + 1));
// S1 += s;
// }
// return S1;
// }
public boolean line_Is_onGraphics(Line M){
boolean flag = false;
for (Line line : Graphics_line) {
if (M.a.dxDistance(line) == 0 && M.b.dxDistance(line) == 0) {
flag = true;
break;
}
}
return flag;
}
public boolean Point_Is_onGraphics(Point M){
boolean flag = false;
for (Line line : Graphics_line) {
if (M.is_Online(line)) {
flag = true;
break;
}
}
return flag;
}
public static double sjS(Point b, Point c, Point d){
double d1 = b.Distance(c);
double d2 = c.Distance(d);
double d3 = d.Distance(b);
double C = d1 + d2 +d3;
double S = Math.abs(Math.pow((C/2)*(C/2-d1)*(C/2-d2)*(C/2-d3),0.5));
double s = Double.parseDouble(String.format("%.3f",S));
return s;
}
public double Area(){//凸多边形面积
double S1 = 0.0;double S2 = 0.0;
for (int i = 0; i < this.Graphics_point.size()-2; i++) {
int j = i + 1;
double s = sjS(this.Graphics_point.get(0), this.Graphics_point.get(j), this.Graphics_point.get(j + 1));
S1 += s;
}
return S1;
}
public boolean Point_Is_inGraphics(Point M){
double S = this.Area();
double S1 = 0.0;
for (int i = 0; i < this.Graphics_point.size(); i++) {
double s = sjS( M, this.Graphics_point.get(i), this.Graphics_point.get((i + 1)%this.Graphics_point.size()));
S1 += s;
}
return Math.abs(S1 - S) < 0.000001;
}
public void setGraphics_point(ArrayList<Point> Graphics_point) {
this.Graphics_point = Graphics_point;
}
public ArrayList<Line> getGraphics_line() {
return Graphics_line;
}
public ArrayList<Line> setGraphics_P_Line(){//需要将点集和线集初始化
ArrayList<Line> G_Line = new ArrayList<>();
for (int i = 0; i < this.Graphics_point.size(); i++) {
Line newline = new Line(this.Graphics_point.get(i%this.Graphics_point.size()),
this.Graphics_point.get((i+1)%this.Graphics_point.size()));
// System.out.println(newline.a.x+","+newline.a.y+" "+newline.b.x+","+newline.b.y);
G_Line.add(newline);
}
return G_Line;
}
public void setGraphics_Line(ArrayList<Line> polygon_Line) {
this.Graphics_line = polygon_Line;
}
public ArrayList<Point> get_jd(Line M){
ArrayList<Point> jiaodian = new ArrayList<>();
//线重合
for (int i = 0; i < this.Graphics_line.size(); i++) {
if(M.xch(this.Graphics_line.get(i))){
return jiaodian;
}
}
for (int i = 0; i < this.Graphics_line.size(); i++) {
if(!M.Line_px(this.Graphics_line.get(i))){
Point jd = M.Get_jd(this.Graphics_line.get(i));
// System.out.println(getline2jdx+" "+getline2jdy+" 线2");
boolean flagjd2 = jd.isOnLine(this.Graphics_line.get(i));
// System.out.println(flagjd2);
if(flagjd2){
if(jiaodian.size()==0){
jiaodian.add(jd);
}
else {
int flag = 1;
for (int j = 0; j < jiaodian.size(); j++) {
if(jiaodian.get(j).Same_Point(jd))
{
flag=0;
// System.out.printf("2重复啦");
break;
}
}
if(flag == 1){
jiaodian.add(jd);
}
}
}
}
continue;
}
return jiaodian;
}
}
static class triangle extends Graphics{//三角形
public void OneTriangle(Line line){
List<Line> lineList = new ArrayList<>();
lineList.add(this.Graphics_line.get(0));lineList.add(this.Graphics_line.get(1));lineList.add(this.Graphics_line.get(2));
Line l1 = this.Graphics_line.get(0);Line l2 = this.Graphics_line.get(1); Line l3 = this.Graphics_line.get(2);
List<Point> list = new ArrayList<>();
int cnt = 0;
Point u;
for(int i=0;i<lineList.size();i++){
u = lineList.get(i).Get_jd(line);
boolean flag = false;
if( u != null){
if(u.is_Online(lineList.get(i))){
flag = true;
}
for (Point p : list){
if( p.CheckPoint(u) ){
flag = false;
break;
}
}
if(flag){
list.add(u);
//System.out.println(u.getX() + " " + u.getY() );
cnt++;
}
}
}
if(cnt < 2){
System.out.println(cnt);
}
else {
double s1,s2,s;
System.out.print("2 ");
Line lline = new Line(list.get(0), list.get(1));
Point p1 = list.get(0),p2 = list.get(1);
Point m;
if(p1.dxDistance(l1)==0 && p2.dxDistance(l2)==0 || p2 .dxDistance(l2)==0 ||p2.dxDistance(l2)==0 && p1.dxDistance(l1)==0)
m = l1.b;
else if(p1.dxDistance(l1)==0 && p2.dxDistance(l3)==0 || p2.dxDistance(l3)==0 && p1.dxDistance(l1)==0 )
m = l1.a;
else
m = l2.b;
triangle triangle1 = new triangle(m,list.get(0),list.get(1));
s1 = triangle1.area();
s1 = method.sswr(s1);
s = this.area();
s = method.sswr(s);
s2 = s - s1;
s2 = method.sswr(s2);
System.out.println(Math.min(s1,s2) + " " + Math.max(s1,s2));
}
}
public double area() {
double s = this.C_Graphics()/2;
return Math.sqrt(s * (s - this.triangle_line.get(0).Line_length()) *
(s - this.triangle_line.get(1).Line_length()) * (s - this.triangle_line.get(2).Line_length()));
}
public ArrayList<Point> triangle_point;
public ArrayList<Line> triangle_line;
public triangle(ArrayList<Point> triangle_point){
this.triangle_point = triangle_point;
}
public triangle(Point a, Point b, Point c) {
ArrayList<Point> list =new ArrayList<>();
list.add(a);list.add(b);list.add(c);
ArrayList<Line> list1 = new ArrayList<>();
Line AB = new Line(a,b);
Line BC = new Line(b,c);
Line CA = new Line(c,a);
list1.add(AB);list1.add(BC);list1.add(CA);
this.triangle_point = list;
this.Graphics_point = list;
this.triangle_line = list1;
this.Graphics_line = list1;
}
public static double area(Point a, Point b, Point c){
double d1 = a.Distance(b);
double d2 = b.Distance(c);
double d3 = c.Distance(a);
double C = d1 + d2 +d3;
return Math.abs(Math.pow((C/2)*(C/2-d1)*(C/2-d2)*(C/2-d3),0.5));
}
public boolean PD(){
if(this.triangle_point.size()<3){
return false;
}
if(this.triangle_point.get(0).PPP_IsOn_SanmeLine(this.triangle_point.get(1), this.triangle_point.get(2))){
return false;
}
return true;
}
public void setTriangle_point(ArrayList<Point> triangle_point) {
this.triangle_point = triangle_point;
}
public void setTriangle_line(ArrayList<Line> triangle_line) {
this.triangle_line = triangle_line;
}
public boolean sjxpd(ArrayList<Point> points) {
Point a = points.get(0), b = points.get(1), c = points.get(2), d = points.get(3);
Line AB = new Line(a,b);
Line BC = new Line(b,c);
Line CA = new Line(c,a);
Line BD = new Line(b,d);
if(d.dxDistance(AB)==0&&c.dxDistance(AB)!=0){
if(a.Same_Point(d)||b.Same_Point(d)){
return true;
}else{
return a.isOnLine(BD);
}
}
else if (d.dxDistance(BC)==0&&a.dxDistance(BC)!=0) {
if(b.Same_Point(d)||c.Same_Point(d)){
return true;
}else{
return c.isOnLine(BD);
}
} else if(d.dxDistance(CA)==0&&b.dxDistance(CA)!=0){
if(c.Same_Point(d)||a.Same_Point(d)){
return true;
}else{
return d.isOnLine(CA);
}
}
else if(a.dxDistance(BC)==0&&d.dxDistance(BC)!=0){
if(a.Same_Point(b)||c.Same_Point(a)){
return true;
}else{
return b.isOnLine(CA);
}
}else
return false;
}
}
static class quadrilateral extends Graphics{//四边形
public ArrayList<Point> quadrilateral_point = new ArrayList<>();
public ArrayList<Line> quadrilateral_line = new ArrayList<>();
public quadrilateral(ArrayList<Point> quadrilateral_point){
this.quadrilateral_point = quadrilateral_point;
}
public quadrilateral(Point a, Point b, Point c, Point d) {
this.quadrilateral_point.add(a);this.quadrilateral_point.add(b);this.quadrilateral_point.add(c);this.quadrilateral_point.add(d);
}
public double quadrilateral_area() {
triangle triangle01 = new triangle(this.quadrilateral_line.get(0).a, this.quadrilateral_line.get(0).b, this.quadrilateral_line.get(1).b);
triangle triangle02 = new triangle(this.quadrilateral_line.get(2).a, this.quadrilateral_line.get(2).b, this.quadrilateral_line.get(3).b);
triangle triangle03 = new triangle(this.quadrilateral_line.get(1).a, this.quadrilateral_line.get(1).b, this.quadrilateral_line.get(2).b);
triangle triangle04 = new triangle(this.quadrilateral_line.get(3).a, this.quadrilateral_line.get(3).b, this.quadrilateral_line.get(0).b);
double qarea01,qarea02;
qarea01 = triangle01.area() + triangle02.area();
qarea02 = triangle03.area() + triangle04.area();
if(qarea02 < qarea01)//凹四边形面积比较特殊,要找凹点,这里偷懒直接判段小的输出
return qarea02;
else
return qarea01;
}
public void fengeArea(Point a, Point b, Point c) {
triangle tr1 = new triangle(a,b,c);
double area1 = tr1.area();
double area = this.area();
double area2 = area - area1;
area1 = method.sswr(area1);
area2 = method.sswr(area2);
System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
}
public double area() {
triangle triangle01 = new triangle(this.quadrilateral_line.get(0).a, this.quadrilateral_line.get(0).b,this.quadrilateral_line.get(1).b);
triangle triangle02 = new triangle(this.quadrilateral_line.get(2).a, this.quadrilateral_line.get(2).b,this.quadrilateral_line.get(3).b);
triangle triangle03 = new triangle(this.quadrilateral_line.get(1).a, this.quadrilateral_line.get(1).b,this.quadrilateral_line.get(2).b);
triangle triangle04 = new triangle(this.quadrilateral_line.get(3).a, this.quadrilateral_line.get(3).b,this.quadrilateral_line.get(0).b);
double qarea01,qarea02;
qarea01 = triangle01.area() + triangle02.area();
qarea02 = triangle03.area() + triangle04.area();
if(qarea02 < qarea01)//凹四边形面积比较特殊,要找凹点,这里偷懒直接判段小的输出
return qarea02;
else
return qarea01;
}
public boolean Is_quadrilateral() {
if(this.quadrilateral_point.size()<4){
return false;
}
if (this.quadrilateral_point.get(0).PPP_IsOn_SanmeLine(this.quadrilateral_point.get(1), this.quadrilateral_point.get(2)) ||
this.quadrilateral_point.get(0).PPP_IsOn_SanmeLine(this.quadrilateral_point.get(1), this.quadrilateral_point.get(3)) ||
this.quadrilateral_point.get(0).PPP_IsOn_SanmeLine(this.quadrilateral_point.get(2), this.quadrilateral_point.get(3)) ||
this.quadrilateral_point.get(1).PPP_IsOn_SanmeLine(this.quadrilateral_point.get(2), this.quadrilateral_point.get(3))) {
return false;//2:0,0 2,1 0,4 8,2 8,0
}
else {
for (int i = 0; i < this.quadrilateral_line.size() - 1; i++) {
for (int j = i+1; j < this.quadrilateral_line.size(); j++) {
if(j-1!=i && (j+1)%this.quadrilateral_line.size()!=i)
{
Point JD = this.quadrilateral_line.get(i).Get_jd(this.quadrilateral_line.get(j));
if (JD.isOnLine(this.quadrilateral_line.get(i)) && JD.isOnLine(this.quadrilateral_line.get(j))) {
return false;
}
}
}
}
}
return true;
}
public void setQuadrilateral_line(ArrayList<Line> quadrilateral_line) {
this.quadrilateral_line = quadrilateral_line;
}
public void Onequarilateral(Line l) {
Point[] points = new Point[4];
ArrayList<Line> lines = new ArrayList<>();
lines.add(this.Graphics_line.get(0));lines.add(this.Graphics_line.get(1));
lines.add(this.Graphics_line.get(2));lines.add(this.Graphics_line.get(3));
int[] p_in = new int[4];
//判断交点在线上还是端点
for (int i = 0; i < 4; i++) {
p_in[i] = 0;
//求直线与四边形的交点
points[i] = lines.get(i).Get_jd(l);
if(points[i]!=null) {
//判断交点是否在边上,不包括端点
if(points[i].is_Online(this.Graphics_line.get(i)))
p_in[i] = 1;
}
}
//有顶角在线上
for (int i = 0; i < 4; i++) {
if(this.Graphics_point.get(i).dxDistance(l)==0) {
if(this.Graphics_point.get((i+2)%4).dxDistance(l)==0) {
fengeArea(this.Graphics_point.get(i), this.Graphics_point.get((i + 2) % 4), this.Graphics_point.get((i + 1) % 4));
return;
}
else if(p_in[(i+1)%4] == 1) {
fengeArea(this.Graphics_point.get(i), this.Graphics_point.get((i + 1) % 4), points[(i + 1) % 4]);
return;
}
else if(p_in[(i+2)%4] == 1) {
fengeArea(this.Graphics_point.get(i), this.Graphics_point.get((i + 3) % 4), points[(i + 2) % 4]);
return;
}
else {
System.out.println("1");
return;
}
}
}
//两个临边
for (int i = 0; i < 4; i++) {
if(p_in[i] == 1 && p_in[(i+1)%4] == 1) {
fengeArea(this.Graphics_point.get((i + 1) % 4), points[i], points[(i + 1) % 4]);
return;
}
}
//两个对边
for (int i = 0; i < 2; i++) {
if(p_in[i] == 1 && p_in[i+2] == 1) {
triangle tr1 = new triangle(this.Graphics_point.get(i), points[i], points[(i + 2) % 4]);
triangle tr2 = new triangle(this.Graphics_point.get((i+3)%4), this.Graphics_point.get(i), points[(i + 2) % 4]);
double area1 = tr1.area() + tr2.area();
double area = this.area();
double area2 = area - area1;
area1 = method.sswr(area1);
area2 = method.sswr(area2);
System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
return;
}
}
System.out.println("0");
return;
}
}
static class pentagon extends Graphics{//五边形
public void fengeArea(Point a, Point b, Point c) {
triangle tr1 = new triangle(a,b,c);
double area1 = tr1.area();
double area = this.area();
double area2 = area - area1;
area1 = method.sswr(area1);
area2 = method.sswr(area2);
System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
}
public void fengeArea(Point a, Point b, Point c, Point d) {
quadrilateral ql1 = new quadrilateral(a, b, c, d);
double area1 = ql1.area();
double area = this.area();
double area2 = area - area1;
area1 = method.sswr(area1);
area2 = method.sswr(area2);
System.out.println("2 " + Math.min(area1,area2) + " " + Math.max(area1,area2));
}
private ArrayList<Point> pentagon_point;
private ArrayList<Line> pentagon_line;
public pentagon(ArrayList<Point> pentagon_point){
this.pentagon_point = pentagon_point;
}
public boolean Is_pentagon() {
if(this.pentagon_point.size()<5){
return false;
}
if (this.pentagon_point.get(0).PPP_IsOn_SanmeLine(this.pentagon_point.get(1), this.pentagon_point.get(2)) ||
this.pentagon_point.get(1).PPP_IsOn_SanmeLine(this.pentagon_point.get(2), this.pentagon_point.get(3)) ||
this.pentagon_point.get(2).PPP_IsOn_SanmeLine(this.pentagon_point.get(3), this.pentagon_point.get(4)) ||
this.pentagon_point.get(3).PPP_IsOn_SanmeLine(this.pentagon_point.get(4), this.pentagon_point.get(0))) {
return false;//2:0,0 2,1 0,4 8,2 8,0
}
else {
for (int i = 0; i < this.pentagon_line.size() - 1; i++) {
for (int j = i+1; j < this.pentagon_line.size(); j++) {
if(j-1!=i && (j+1)%this.pentagon_line.size()!=i)
{
Point JD = this.pentagon_line.get(i).Get_jd(this.pentagon_line.get(j));
if (JD.isOnLine(this.pentagon_line.get(i)) && JD.isOnLine(this.pentagon_line.get(j))) {
return false;
}
}
}
}
}
return true;
}
public void Onepentagon(Line line) {
Point[] pn = new Point[5];
int[] p_in = new int[5];
for (int i = 0; i < this.Graphics_line.size(); i++) {
p_in[i] = 0;//初始没有交点
pn[i] = this.Graphics_line.get(i).Get_jd(line);//求边于线是否有交点
if(pn[i] != null) {
//判断交点是否在边上,不包括端点
if(pn[i].is_Online(this.Graphics_line.get(i)))
p_in[i] = 1;
}
}
//有顶角在线上
for (int i = 0; i < this.Graphics_point.size(); i++) {
if(this.Graphics_point.get(i).dxDistance(line)==0) {
for (int j = 0; j < this.Graphics_point.size()-3; j++) {//直线通过对角切割
if(this.Graphics_point.get((i+j+2)%this.Graphics_point.size()).dxDistance(line)==0) {
if(j==0) {
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get((i+j+2)%this.Graphics_point.size()
), this.Graphics_point.get((i+j+2-1)%this.Graphics_point.size()));
return;
}
else {
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get((i+j+2)%this.Graphics_point.size()
), this.Graphics_point.get((i+j+2+1)%this.Graphics_point.size()));
return;
}
}
}
for (int j = 0; j < this.Graphics_line.size()-2; j++) {
if(p_in[(i+j+1)%this.Graphics_line.size()] == 1) {
if(i+j+1>1 && i+j+1<this.Graphics_point.size()-2){
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get((i+j+1-1)% this.Graphics_point.size()),
this.Graphics_point.get((i+j+1)%this.Graphics_point.size()), pn[((i+j+1)%this.Graphics_line.size())]);
return;
}
else{
if(j==0){
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get((i+j+1)%this.Graphics_point.size()
), pn[(i+j+1)%this.Graphics_line.size()]);
return;
}
else if(i+j+1==this.Graphics_line.size()-2) {
fengeArea(this.Graphics_point.get(i),this.Graphics_point.get(((i+j+1)+1)%this.Graphics_point.size()),pn[(i+j+1)%this.Graphics_line.size()]);
return;
}
}
}
}
System.out.println("1");
return;
}
}
//两个临边
for (int i = 0; i < this.Graphics_point.size(); i++) {
if(p_in[i] == 1 && p_in[(i+1)% this.Graphics_line.size()] == 1) {
fengeArea(this.Graphics_point.get((i + 1) % this.Graphics_point.size()), pn[i], pn[(i + 1) % this.Graphics_point.size()]);
return;
}
}
}
public double area(){
double a=0;
for (int i = 1; i < this.Graphics_point.size()-1; i++) {
triangle t = new triangle(this.Graphics_point.get(0), this.Graphics_point.get(i), this.Graphics_point.get(i+1));
a+=t.area();
}
return method.sswr(a);
}
}
}
④报表
期中测试
①题目及分析
题目一:
设计一个类表示平面直角坐标系上的点Point,私有属性分别为横坐标x与纵坐标y,数据类型均为实型数,除构造方法以及属性的getter与setter方法外,定义一个用于显示信息的方法display(),用来输出该坐标点的坐标信息,格式如下:(x,y),数值保留两位小数。为简化题目,其中,坐标点的取值范围设定为(0,200]。若输入有误,系统则直接输出Wrong Format
设计一个类表示平面直角坐标系上的线Line,私有属性除了标识线段两端的点point1、point2外,还有一个字符串类型的color,用于表示该线段的颜色,同样,除构造方法以及属性的getter与setter方法外,定义一个用于计算该线段长度的方法getDistance(),还有一个用于显示信息的方法display(),用来输出线段的相关信息,输出格式如下:
** 题目要求:在主方法中定义一条线段对象,从键盘输入该线段的起点坐标与终点坐标以及颜色,然后调用该线段的display()方法进行输出。**
题目二:
在“点与线(类设计)”题目基础上,对题目的类设计进行重构,以实现继承与多态的技术性需求。
对题目中的点Point类和线Line类进行进一步抽象,定义一个两个类的共同父类Element(抽象类),将display()方法在该方法中进行声明(抽象方法),将Point类和Line类作为该类的子类。
再定义一个Element类的子类面Plane,该类只有一个私有属性颜色color,除了构造方法和属性的getter、setter方法外,display()方法用于输出面的颜色,输出格式如下:The Plane's color is:颜色
在主方法内,定义两个Point(线段的起点和终点)对象、一个Line对象和一个Plane对象,依次从键盘输入两个Point对象的起点、终点坐标和颜色值(Line对象和Plane对象颜色相同),然后定义一个Element类的引用,分别使用该引用调用以上四个对象的display()方法,从而实现多态特性。示例代码如下:
题目三:
在“点与线(继承与多态)”题目基础上,对题目的类设计进行重构,增加容器类保存点、线、面对象,并对该容器进行相应增、删、遍历操作。
在原有类设计的基础上,增加一个GeometryObject容器类,其属性为ArrayList
增加该类的add()方法及remove(int index)方法,其功能分别为向容器中增加对象及删除第index - 1(ArrayList中index>=0)个对象
在主方法中,用户循环输入要进行的操作(choice∈[0,4]),其含义如下:
1:向容器中增加Point对象
2:向容器中增加Line对象
3:向容器中增加Plane对象
4:删除容器中第index - 1个数据,若index数据非法,则无视此操作
0:输入结束
③代码
题目一代码:
点击查看代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner set = new Scanner(System.in);
String str = set.next();
double x1 = Double.parseDouble(str);
if(x1<0||x1>200){
System.out.println("Wrong Format");
System.exit(0);
}
String str2 = set.next();
double y1 = Double.parseDouble(str2);
if(y1<0||y1>200){
System.out.println("Wrong Format");
System.exit(0);
}
String str3 = set.next();
double x2 = Double.parseDouble(str3);
if(x2<0||x2>200){
System.out.println("Wrong Format");
System.exit(0);
}
String str4 = set.next();
double y2 = Double.parseDouble(str4);
if(y2<0||y2>200){
System.out.println("Wrong Format");
System.exit(0);
}
double X1 = Format.op_format(x1);
double Y1 = Format.op_format(y1);
double X2 = Format.op_format(x2);
double Y2 = Format.op_format(y2);
String colorset = set.next();
Line AB = new Line(new Point(X1,Y1),new Point(X2,Y2));
AB.setColor(colorset);
double S = Format.op_format(AB.getDistance());
System.out.println("The line's color is:"+AB.getColor());
System.out.println("The line's begin point's Coordinate is:");
System.out.println("("+String.format("%.2f",X1)+","+String.format("%.2f",Y1)+")");
System.out.println("The line's end point's Coordinate is:");
System.out.println("("+String.format("%.2f",X2)+","+String.format("%.2f",Y2)+")");
System.out.println("The line's length is:"+S);
}
}
class Input{
}
class Format{
public static double op_format(double D){
double d = Double.parseDouble(String.format("%.2f", D));
return d;
}
}
class Point{
private double x;
private double y;
public Point(){
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void display() {
System.out.println("(" + x + "," + y + ")");
}
}
class Line extends Point{
private Point point1;
private Point point2;
private String color;
public Line() {
}
public Line(Point point1, Point point2, String color) {
this.point1 = point1;
this.point2 = point2;
this.color = color;
}
public Line(Point point1, Point point2) {
this.point1 = point1;
this.point2 = point2;
}
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public Point getPoint2() {
return point2;
}
public void setPoint2(Point point2) {
this.point2 = point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance(){
double x1 = this.point1.getX();
double x2 = this.point2.getX();
double y1 = this.point1.getY();
double y2 = this.point2.getY();
double D = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
double d = Math.sqrt(D);
return d;
}
public String toString() {
return "Line{point1 = " + point1 + ", point2 = " + point2 + ", color = " + color + "}";
}
}
题目二代码:
点击查看代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner set = new Scanner(System.in);
String str = set.next();
double x1 = Double.parseDouble(str);
if(x1<0||x1>200){
System.out.println("Wrong Format");
System.exit(0);
}
String str2 = set.next();
double y1 = Double.parseDouble(str2);
if(y1<0||y1>200){
System.out.println("Wrong Format");
System.exit(0);
}
String str3 = set.next();
double x2 = Double.parseDouble(str3);
if(x2<0||x2>200){
System.out.println("Wrong Format");
System.exit(0);
}
String str4 = set.next();
double y2 = Double.parseDouble(str4);
if(y2<0||y2>200){
System.out.println("Wrong Format");
System.exit(0);
}
double X1 = Format.op_format(x1);
double Y1 = Format.op_format(y1);
double X2 = Format.op_format(x2);
double Y2 = Format.op_format(y2);
String colorset = set.next();
Line AB = new Line(new Point(X1,Y1),new Point(X2,Y2));
AB.setColor(colorset);
System.out.println("("+String.format("%.2f",X1)+","+String.format("%.2f",Y1)+")");
System.out.println("("+String.format("%.2f",X2)+","+String.format("%.2f",Y2)+")");
Element d = AB;
d.display();
Plane A = new Plane(colorset);
A.display();
}
}
class Input{
}
class Format{
public static double op_format(double D){
double d = Double.parseDouble(String.format("%.2f", D));
return d;
}
}
abstract class Element{
public abstract void display();
}
class Point extends Element{
private double x;
private double y;
public Point(){
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public void setX(double x) {
this.x = x;
}
public double getY() {
return y;
}
public void setY(double y) {
this.y = y;
}
public void display() {
System.out.println("(" + x + "," + y + ")");
}
}
class Line extends Element{
private Point point1;
private Point point2;
private String color;
public Line() {
}
public Line(Point point1, Point point2, String color) {
this.point1 = point1;
this.point2 = point2;
this.color = color;
}
public Line(Point point1, Point point2) {
this.point1 = point1;
this.point2 = point2;
}
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public Point getPoint2() {
return point2;
}
public void setPoint2(Point point2) {
this.point2 = point2;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getDistance(){
double x1 = this.point1.getX();
double x2 = this.point2.getX();
double y1 = this.point1.getY();
double y2 = this.point2.getY();
double D = (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
double d = Math.sqrt(D);
return d;
}
public String toString() {
return "Line{point1 = " + point1 + ", point2 = " + point2 + ", color = " + color + "}";
}
@Override
public void display() {
System.out.println("The line's color is:"+this.getColor());
System.out.println("The line's begin point's Coordinate is:");
System.out.println("("+String.format("%.2f",this.point1.getX())+","+String.format("%.2f",this.getPoint1().getY())+")");
System.out.println("The line's end point's Coordinate is:");
System.out.println("("+String.format("%.2f",this.point2.getX())+","+String.format("%.2f",this.point2.getY())+")");
System.out.println("The line's length is:"+String.format("%.2f",this.getDistance()));
}
}
class Plane extends Element{
private String color;
public Plane() {
}
public Plane(String color) {
this.color = color;
}
@Override
public void display() {
System.out.println("The Plane's color is:"+this.color);
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String toString() {
return "Plane{color = " + color + "}";
}
}
题目三代码:
点击查看代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class GeometryObject{
private List<Element> elementList = new ArrayList<>();
public List<Element> getElementList() {
return elementList;
}
public void add(Element element){
elementList.add(element);
}
public void remove(int index){
elementList.remove(index-1);
}
}
abstract class Element{
abstract void display();
}
class Plane extends Element{
private String color;
Plane(){}
Plane(String color){
this.color = color;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
public void display(){
System.out.println("The Plane's color is:"+color);
}
}//面类
class Point extends Element{
private double x;
private double y;
Point(){}
Point(double x , double y ){
this.x = x;
this.y = y;
}
public void display(){
System.out.println(String.format("(%.2f,%.2f)", x ,y));
}
public void setX(double x) {
this.x = x;
}
public double getX() {
return x;
}
public void setY(double y) {
this.y = y;
}
public double getY() {
return y;
}
}//点类
class Line extends Element{
private Point point1;
private Point point2;
String color;
Line(){
}
Line(Point point1,Point point2,String color){
this.point1 = point1;
this.point2 = point2;
this.color = color;
}
public double getDistance(){
return Math.sqrt(Math.pow((point1.getX() - point2.getX()), 2) + Math.pow((point1.getY() - point2.getY()), 2));
}
public void display(){
System.out.println("The line's color is:"+color);
System.out.println("The line's begin point's Coordinate is:");
point1.display();
System.out.println("The line's end point's Coordinate is:");
point2.display();
System.out.println(String.format("The line's length is:%.2f",getDistance()));
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public Point getPoint1() {
return point1;
}
public void setPoint2(Point point2) {
this.point2 = point2;
}
public Point getPoint2() {
return point2;
}
public void setColor(String color) {
this.color = color;
}
public String getColor() {
return color;
}
}//线类
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
GeometryObject geometryObject = new GeometryObject();
int choice = in.nextInt();
Element element;
while(choice != 0) {
switch(choice) {
case 1:
double x1,y1;
x1 = in.nextDouble();
y1 = in.nextDouble();
Point p = new Point(x1,y1);
element = p;
geometryObject.add(element);
break;
case 2:
String color;
Point p1 = new Point(in.nextDouble(),in.nextDouble());
Point p2 = new Point(in.nextDouble(),in.nextDouble());
color = in.nextLine();
color = in.nextLine();
Line line = new Line(p1,p2,color);
element = line;
geometryObject.add(element);
break;
case 3:
color = in.nextLine();
color = in.nextLine();
Plane plane = new Plane(color);
element = plane;
geometryObject.add(element);
break;
case 4:
int index = in.nextInt();
if(index >= 0 && index <= geometryObject.getElementList().size())
geometryObject.remove(index);
}
choice = in.nextInt();
}
for (Element e : geometryObject.getElementList()){
e.display();
}
}
}
④报表(由于在三题是迭代题目就在这用一个第三题总的报表表示)
⑤总结与分析
很难过的时我在写第二题时,第三个测试点卡了我很久,明明很简单的一个题目却一直过不去,后面发现原来是我的输入判断有问题!!!(0.200]这个!!!我给的条件包括了0。sad很难过,一下子12分没了。我下次会注意的!!!
踩坑心得
主要还是期中考试的那个12分亏得难受,写题目时一定要注意自己的态度,要注意细节,不该犯的错误不犯,一步一步慢慢写。
改进建议
题目集4需要改进的点还是蛮多的,设计清楚类的各种属性。并要熟练掌握数学的思维能力,设计好方法。运用起来就会很方便。
总结
通过这段时间的学习,我深刻认识到自己的编程能力并不是特别强,逻辑思维能力偏弱。在期中考试时,我的第二题测试点卡了我有点久,一直找不出来bug在哪,测试样例都可以过但是就是拿不到满分。在写题目集4中,其中一个选项分割面积时,使用的暴力破解,没想那么多,导致代码过长,无法提交上去,后面发现自己的代码中有很多重复的东西,可以做判断并进行强化简写,我仅仅想了一种简单的修改就将缩短了很长的一段代码。以及题集5的编写过程也是十分痛苦,因为之前的代码比较凌乱,需要重新写类(包括图形类、五边形、四边形、三角形)以及在题集5的第二题中,思考的时间花的比较少,条理又不是很清晰,整个题集5就只拿了60+的分数,我今后会加强学习,学习借鉴优秀程序员的编程思维。锻炼逻辑思维能力,提高自己的编程能力水平,独立完成项目、作业等练习。不断巩固自己的知识。
标签:get,double,Point,System,博客,new,第二次,line From: https://www.cnblogs.com/wudi123/p/16825472.html