PAT练习题(甲级) 1011 World Cup Betting (20分)(Java实现)

题干

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.

Chinese Football Lottery provided a “Triple Winning” game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results – namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner’s odd would be the product of the three odds times 65%.

For example, 3 games’ odds are given as the following:

 W    T    L
1.1  2.5  1.7
1.2  3.1  1.6
4.1  1.2  1.1

To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1×3.1×2.5×65%−1)×2=39.31 yuans (accurate up to 2 decimal places).

Input Specification:
Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to W, T and L.

Output Specification:
For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.

Sample Input:

1.1 2.5 1.7
1.2 3.1 1.6
4.1 1.2 1.1

Sample Output:

T T W 39.31

题意

  • 废话也不多说,就像买彩票。
  • 给出三场比赛的输赢平的比重,Sample Input中第一列是赢,第二列是平,第三列是输,每一行代表一场比赛。
  • 每一行的数字就像玩骰子,买大买小,买定离手。
  • 要求是输出每场比赛投注的最大的那个结果,也就是每行数字最大的那个对应的结果,第一列返回"W",第二列返回"T",第三列返回"L"。
  • 最后输出 (4.1×3.1×2.5×65%−1)×2=39.31 就行了,也就是每行最大的数乘一起,再乘0.65,再减1,再乘2。

解题思路

  • 还是先读进来,有很多人用Scanner,也可以,因为这题简单,如果用BufferedReader会执行效率更快。
  • 怎么输出"W"这几个呢,只要设置好标志位,然后进行输出就行了。

注意事项

  • 首先记住答题的时候不要有package名,因为我自己打代码的时候一不小心就把自己的代码复制上去试试,然后以为有bug,找半天才发现我把package也复制进去了,如果想复制自己的代码进行测试的时候,大家要记得去掉package,还有把类名改成Main,但还是建议自己写,毕竟是学习过程。
  • 输出最大利润的时候要保留两位小数。
  • 有啥不懂的看代码。
  • 答题的时候尽量减少空间的消耗,从而达到提升运行速度的目的。

代码实现

BufferedReader方法

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author: 胡奇夫
 * @QQ: 1579328766
 * @CreateTime: 2020-05-20-15-20
 * @Descirption: Pat1011 World Cup Betting (20分)
 */
public class Testeleven {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] result1 = {"W","T","L"};//结果的数组
        double result2 = 1.0;//每行最大数的乘积
        for(int i = 0;i<3;i++)
        {
            int j = 0;
            int k = 0;
            double max = 0;
            for(String s:br.readLine().split(" "))//减少空间消耗
            {
                double temp = Double.parseDouble(s);
                if(temp>max) {
                    k = j;
                    max = temp;
                }
                j++;
            }
            System.out.print(result1[k]+" ");//输出那几个英文
            result2 *= max;
        }
        System.out.printf("%.2f",(result2*0.65-1)*2);//保留两位小数
    }
}

Scanner方法

import java.io.IOException;
import java.util.Scanner;

/**
 * @Author: 胡奇夫
 * @QQ: 1579328766
 * @CreateTime: 2020-05-20-16-23
 * @Descirption: Pat甲级 1011 World Cup Betting (20分)
 */
public class Testeleven0 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        String[] result = {"W","T","L"};
        double sum = 1.0;
        for (int i = 0; i < 3; i++) {
            double max = 0.0;
            int n = 0;
            for (int j = 0; j < 3; j++) {
                double t = sc.nextDouble();
                if (t > max) {
                    max = t;
                    n = j;
                }
            }
            System.out.print(result[n] + " ");
            sum *= max;
        }
        System.out.printf("%.2f", (sum*0.65 - 1) * 2);
    }
}

运行时间

BufferedReader方法

BufferedReader方法

Scanner方法

Scanner方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值