> 文章列表 > 浮点数比较

浮点数比较

浮点数比较

目录

使用java的BigDecimal 比较:

实战


使用java的BigDecimal 比较:

import java.math.BigDecimal;public class DoubleLean {public static void main(String[] args) {BigDecimal aa=new BigDecimal("1.23");BigDecimal bb=new BigDecimal("1.21");BigDecimal cc=aa.subtract(bb);BigDecimal dd=new BigDecimal("0.02");if (cc.compareTo(dd)==0){System.out.println("相等");}else{System.out.println("不相等");}}
}

 

实战

直线

本题总分:10 分

【问题描述】

        在平面直角坐标系中,两点可以确定一条直线。如果有多点在一条直线上,那么这些点中任意两点确定的直线是同一条。

        给定平面上 2 × 3 个整点 {(x, y)|0 ≤ x < 2, 0 ≤ y < 3, x ∈ Z, y ∈ Z},即横坐标是 0 到 1 (包含 0 和 1) 之间的整数、纵坐标是 0 到 2 (包含 0 和 2) 之间的整数的点。这些点一共确定了 11 条不同的直线。

        给定平面上 20 × 21 个整点 {(x, y)|0 ≤ x < 20, 0 ≤ y < 21, x ∈ Z, y ∈ Z},即横坐标是 0 到 19 (包含 0 和 19) 之间的整数、纵坐标是 0 到 20 (包含 0 和 20) 之间的整数的点。请问这些点一共确定了多少条不同的直线。

【答案提交】

        这是一道结果填空的题,你只需要算出结果后提交即可。本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分。

【答案】:40257

不会做

思路:

涉及到直线问题我们需要考虑斜率和截距:

直线: y = k x + b, k 是斜率, b是截距,我们可以用一个类来存斜率和截距。

第一步:在给定范围内,任意两个点之间构成一条直线

第二步:求出非重边的条数

需要注意的点:

① 因为斜率可能是小数,所以要用浮点型数据进行比较,对于 d o u b l e   a , b,如果 ∣a−b∣≤1e−8 则 a , b 相等。

② 非重边:斜率或者截距不相等

③ 因为垂直于x轴的直线有20条,斜率和截距都相等,因此最后统计非重边条数的时候要+20

AC代码:(补题)

#这个方法速度快,但是精度要求高的话可能会错

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.*;public class Main
{static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int)200010;static Line[] line = new Line[N];static int n;static Mycomparator cmp = new Mycomparator();public static void main(String[] args ) throws IOException, ParseException{for (int x1 = 0; x1 <= 19; x1++){for (int y1 = 0; y1 <= 20; y1++){for (int x2 = 0; x2 <= 19; x2++){for (int y2 = 0; y2 <= 20; y2++){if (x1 != x2){double k = (double) (y2 - y1) / (x2 - x1);double b = y1 - k * x1;line[n++] = new Line(k, b);}}}}}Arrays.sort(line, 0, n,cmp);int res = 1;for (int i = 1; i < n; i++){// 求斜率或者截距不等if (Math.abs(line[i].k - line[i - 1].k) > 1e-8 || Math.abs(line[i].b - line[i - 1].b) > 1e-8) {res++;}}pw.println(res + 20); // 最后要加上20pw.flush();}
}class rd
{static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException  { return reader.readLine(); }static String next() throws IOException{while (!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException  { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException  { return Long.parseLong(next());}static BigInteger nextBigInteger() throws IOException { return new BigInteger(rd.nextLine()); }
}class PII
{int x,y;public PII(int x, int y){this.x = x;this.y = y;}
}class Line
{double k;double b;public Line(double k, double b){this.k = k;this.b = b;}
}class Mycomparator implements Comparator<Line>
{@Overridepublic int compare(Line o1, Line o2){if(o1.k < o2.k)  return 1;if(o1.k == o2.k){if(o1.b < o2.b)  return 1;return -1;}return -1;}
}

#这个方法速度慢,但是精度要求高的话肯定对

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.ParseException;
import java.util.*;public class Main
{static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));static int N = (int)200010;static Line[] line = new Line[N];static int n;static Mycomparator cmp = new Mycomparator();public static void main(String[] args ) throws IOException, ParseException{for (int x1 = 0; x1 <= 19; x1++){for (int y1 = 0; y1 <= 20; y1++){for (int x2 = 0; x2 <= 19; x2++){for (int y2 = 0; y2 <= 20; y2++){if (x1 != x2){double k = (double) (y2 - y1) / (x2 - x1);double b = y1 - k * x1;line[n++] = new Line(k, b);}}}}}Arrays.sort(line, 0, n,cmp);int res = 1;for (int i = 1; i < n; i++){// 求斜率或者截距不等if (Math.abs(line[i].k - line[i - 1].k) > 1e-8 || Math.abs(line[i].b - line[i - 1].b) > 1e-8) {res++;}}pw.println(res + 20); // 最后要加上20pw.flush();}
}class rd
{static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));static StringTokenizer tokenizer = new StringTokenizer("");static String nextLine() throws IOException  { return reader.readLine(); }static String next() throws IOException{while (!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());return tokenizer.nextToken();}static int nextInt() throws IOException  { return Integer.parseInt(next()); }static double nextDouble() throws IOException { return Double.parseDouble(next()); }static long nextLong() throws IOException  { return Long.parseLong(next());}static BigInteger nextBigInteger() throws IOException { return new BigInteger(rd.nextLine()); }
}class PII
{int x,y;public PII(int x, int y){this.x = x;this.y = y;}
}class Line
{double k;double b;public Line(double k, double b){this.k = k;this.b = b;}
}class Mycomparator implements Comparator<Line>
{@Overridepublic int compare(Line o1, Line o2){BigDecimal k1 = new BigDecimal(String.valueOf(o1.k));BigDecimal k2 = new BigDecimal(String.valueOf(o2.k));BigDecimal b1 = new BigDecimal(String.valueOf(o1.b));BigDecimal b2 = new BigDecimal(String.valueOf(o2.b));if(k1.compareTo(k2) < 0)  return 1;if(k1.compareTo(k2) > 0)  return -1;if(k1.compareTo(k2) == 0){if(b1.compareTo(b2) < 0)  return 1;return -1;}return -1;}
}