例如:1:0,0 1,1
如果不符合基本格式,输出"Wrong " 。
如果符合基本格式,但输入点的数量不符合要求,输出"wrongof " 。
不论哪个选项,如果格式、点数量都符合要求,但构成任一条线的两个点坐标重合,输出" ",
输出格式:
见题目描述 。
类图:
大概思路:
通过legal函用来判断输入数据是否合法,把选项看作一个类,具体写法和第一题差不多,但写到这时候发现用一个函数把字符串转换成数字类型太麻烦,直接强制转换,正负号也可以保留下来 。
这道题复杂的是算法
计算斜率:k=(y2-y1)/(x2-x1);
计算一点到另外两点构成的直线的距离:需要先算直线方程,再求点到该直线的距离 :
判断三点是否在一线,即判断三点中任意两点组成的线段的斜率是否相等,这个比较简单,用(y1-y2)*(x-x2)==(x1-x2)*(y-y2)判断即可
判断两条线是否平行,即判断斜率是否存在且相等,比较两条线k=(y2-y1)/(x2-x1);
判断两线关系,如果不平行,则计算交点位置,并判断交点是否在线上 。交点公式参考文章:两点确定一条直线,已知四个点确定的两条直线,求这两条直线的交点的博客-CSDN博客
源码(具体代码分析写在注释里):
import java.util.Scanner;public class Main{public static void main(String[] args) {Scanner s = new Scanner(System.in);String str = s.nextLine();P z = new P();boolean a =false;for(int i=0;i 1 && x.charAt(1) != '.')return false;}if (x.matches(w))return true;elsereturn false;}void One(String x) {String[] dj = x.split(" ");String[] point1 = dj[0].split(",");String[] point2 = dj[1].split(",");if (!dj[0].equals(dj[1])) {//防止点重合if (point1.length == 2 && point2.length == 2) {//判断有x,y的值if (legal(point1[0]) && legal(point1[1]) && legal(point2[0]) && legal(point2[1])) {//判断输入数据合法double x1 = Double.parseDouble(point1[0]);double y1 = Double.parseDouble(point1[1]);double x2 = Double.parseDouble(point2[0]);double y2 = Double.parseDouble(point2[1]);if (dj.length == 2)if (x1 == x2)System.out.print("Slope does not exist");elseSystem.out.print((y2 - y1) / (x2 - x1));//一切正确,输出斜率if (dj.length > 2)System.out.print("wrong number of points");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("points coincide");}void Two(String x) {String[] dj = x.split(" ");String[] point1 = dj[0].split(",");String[] point2 = dj[1].split(",");if (dj.length < 3)//防止点数量不够,导致不存在后面的元素System.out.print("wrong number of points");String[] point3 = dj[2].split(",");if (!dj[1].equals(dj[2])) {if (point1.length == 2 && point2.length == 2 && point3.length == 2) {if (legal(point1[0]) && legal(point1[1]) && legal(point2[0]) && legal(point2[1]) && legal(point3[0])&& legal(point3[1])) {double x0 = Double.parseDouble(point1[0]);//将输入的子字符串转换成数据double y0 = Double.parseDouble(point1[1]);double x1 = Double.parseDouble(point2[0]);double y1 = Double.parseDouble(point2[1]);double x2 = Double.parseDouble(point3[0]);double y2 = Double.parseDouble(point3[1]);if (dj.length == 3)System.out.print(Math.abs((y2 - y1) * (x0 - x1) - (x2 - x1) * (y0 - y1))//点到直线的公式/ Math.sqrt(Math.pow((y1 - y2), 2) + Math.pow((x1 - x2), 2)));if (dj.length > 3)System.out.print("wrong number of points");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("points coincide");}void Three(String x) {String[] dj = x.split(" ");String[] point1 = dj[0].split(",");if (dj.length < 3)System.out.print("wrong number of points");String[] point2 = dj[1].split(",");String[] point3 = dj[2].split(",");if (!dj[1].equals(dj[2])&& !dj[0].equals(dj[2])&& !dj[0].equals(dj[1])) {if (point1.length == 2 && point2.length == 2 && point3.length == 2) {if (legal(point1[0]) && legal(point1[1]) && legal(point2[0]) && legal(point2[1]) && legal(point3[0])&& legal(point3[1])) {double x0 = Double.parseDouble(point1[0]);double y0 = Double.parseDouble(point1[1]);double x1 = Double.parseDouble(point2[0]);double y1 = Double.parseDouble(point2[1]);double x2 = Double.parseDouble(point3[0]);double y2 = Double.parseDouble(point3[1]);if (dj.length == 3 &&((y2- y1) / (x0 - x1) == (y0 - y1) / (x2 - x1)||(y0- y1) / (x2 - x1) == (y2 - y1) / (x0 - x1)))//判断三点是否一线System.out.print("true");if ((y2- y1) / (x0 - x1) != (y0 - y1) / (x2 - x1)||(y0- y1) / (x2 - x1) != (y2 - y1) / (x0 - x1))System.out.print("false");if (dj.length > 3)System.out.print("wrong number of points");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("Wrong Format");} elseSystem.out.print("points coincide");}void Four(String x) {String[] dj = x.split(" ");String[] point1 = dj[0].split(",");String[] point2 = dj[1].split(",");if (dj.length
- 一级 2020年06月 Python真题解析#中国电子学会#全国青少年软件编程
- 推荐几个ai聊天软件给大家
- 五 点云深度学习系列博客: 注意力机制原理概述
- 每日一题 pwnable.kr flag
- 为什么你自律一段时间后,还是会被打回原形?
- 二手车贷款利息一般几厘
- 怎么提取视频中的音频?这几种提取音频方法了解一下
- Diffusion Model一发力,GAN就过时了???
- 每天8个小时睡眠够吗 8小时睡眠不一定适合每个人
- 上 数据结构及算法 | Java数据结构——BST二叉搜索树