【PAT甲级 ArrayList存放class】1011 World Cup Betting (20 分) Java版 3/3通过 四舍五入保留两位小数
生活随笔
收集整理的這篇文章主要介紹了
【PAT甲级 ArrayList存放class】1011 World Cup Betting (20 分) Java版 3/3通过 四舍五入保留两位小数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
一共給9個數,3*3,找到每行最大,按照題目要求套算式,計算就行
坑 & 心得
沒啥坑,一次通過,用到的知識點是:自定義排序、四舍五入保留兩位小數
DecimalFormat df = new DecimalFormat("#0.00"); // 保留兩位小數System.out.println(df.format(max));代碼
import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);List<A> l1 = new ArrayList<A>();l1.add(new A(sc.nextDouble(), "W"));l1.add(new A(sc.nextDouble(), "T"));l1.add(new A(sc.nextDouble(), "L"));List<A> l2 = new ArrayList<A>();l2.add(new A(sc.nextDouble(), "W"));l2.add(new A(sc.nextDouble(), "T"));l2.add(new A(sc.nextDouble(), "L"));List<A> l3 = new ArrayList<A>();l3.add(new A(sc.nextDouble(), "W"));l3.add(new A(sc.nextDouble(), "T"));l3.add(new A(sc.nextDouble(), "L"));Collections.sort(l1);Collections.sort(l2);Collections.sort(l3);double max = ((l1.get(0).num * l2.get(0).num * l3.get(0).num * 0.65) - 1) * 2;DecimalFormat df = new DecimalFormat("#0.00");System.out.println(l1.get(0).str + " " + l2.get(0).str + " " + l3.get(0).str + " " + df.format(max));}}class A implements Comparable<A> {double num;String str;public A(double num, String str) {this.num = num;this.str = str;}@Overridepublic int compareTo(A a) {if (a.num > num)return 1;else if (a.num < num)return -1;elsereturn 0;} }結果
總結
以上是生活随笔為你收集整理的【PAT甲级 ArrayList存放class】1011 World Cup Betting (20 分) Java版 3/3通过 四舍五入保留两位小数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Java 排序】重写Compare方法
- 下一篇: 【PAT甲级 Date时间比较】1006