首页 > 试题广场 >

World Cup Betting (20)

[编程题]World Cup Betting (20)
  • 热度指数:3327 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
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.0 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.0*2.5*65%-1)*2 = 37.98 yuans (accurate up to 2 decimal places).

输入描述:
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.


输出描述:
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.
示例1

输入

1.1 2.5 1.7<br/>1.2 3.0 1.6<br/>4.1 1.2 1.1

输出

T T W 37.98
用float就过了..
#include <bits/stdc++.h>
using namespace std;
int main(){
    int res[3],j;
    char mp[3] = {'W','T','L'};
    float ans=1.0;
    float odds[3],maxx;//每局赔率
    for(int i = 0; i < 3; i++){
        maxx = -1.0;
        for(j = 0; j < 3; j++){
            scanf("%f",&odds[j]);
            if(maxx<odds[j]) maxx = odds[j],res[i] = j;
        }
        ans*=odds[res[i]];
    }
    printf("%c %c %c %.2f\n",mp[res[0]],mp[res[1]],mp[res[2]],(ans*0.65-1)*2);
    return 0;
}

发表于 2019-04-02 11:18:36 回复(1)
真的是一道精度问题很严重的题目。。。。目前还不是搞得很懂,double进行了四舍五入处理,加的0.005还是无法AC,看上面说要再比0.005大一点点,是什么原因?测试数据原因吗?最后改成float之后不进行四舍五入直接AC了
发表于 2018-06-11 23:17:23 回复(0)
//我采取四舍五入保留两位小数,牛客pat双过
//我电脑跑题中用例输出的结果是37.97,晕~
//另精度问题:牛客只能用float,pat都行
//代码我就不贴了,题水

发表于 2018-03-03 11:58:45 回复(2)
发表于 2018-07-28 12:11:47 回复(0)
//水的不能再水了
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		float c[][]=new float[3][3];
		int imax=0;
		int jmax=0;
		int kmax=0;
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				c[i][j]=in.nextFloat();
			}
		}
		double max=0;
		for(int i=0;i<3;i++){
			for(int j=0;j<3;j++){
				for(int k=0;k<3;k++){
					double temp=(c[0][i]*c[1][j]*c[2][k]*0.65-1)*2;
					if(temp>max){
						max=temp;
						imax=i;
						jmax=j;
						kmax=k;
					}
					
				}
			}
			
		}String s[]={"W","T","L"};
		DecimalFormat f=new DecimalFormat("0.00");
		System.out.println(s[imax]+" "+s[jmax]+" "+s[kmax]+" "+f.format(max));
		
	}

}


发表于 2016-10-28 19:56:32 回复(0)
printf(“%.2f”),采用“四舍六入五成双”的规则,如果非要“四舍五入”,可采用判断精确的下一位是否为5加上即可,转成string判断
代码实现如下
stringstream ss;
ss<<ans;
string s=ss.str();
if(s.find(".")+3<s.length()&&s[s.find(".")+3]=='5')
{
    ans+=0.005;
}
printf("%.2f",ans);



编辑于 2021-03-25 10:47:50 回复(0)
#include<iostream> 
using namespace std;
int main(){
	
	double a,b,c;
	double s=0.65;
	for(int i=0;i<3;i++){
		cin>>a>>b>>c;
		if(a>=b&&a>=c){
			cout<<"W";
			s*=a;
		}
		if(b>=a&&b>=c){
			cout<<"T";
			s*=b;
		}
		if(c>=a&&c>=b){
			cout<<"L";
			s*=c;
		}
		cout<<" ";
	}
	s-=1;
	s*=2;
	printf("%.2f",s+0.00001); //加上0.00001就过了 
}

发表于 2020-06-23 17:49:34 回复(0)
#include<cstdio>
char s[4]={'W','T','L'};
char res[4];
int main()
{
    double k=1.0,ans;
    for(int i=0;i<3;i++)
    {
       double temp[3];
       int max=0,j;
       for(j=0;j<3;j++)
       {
           scanf("%lf",temp+j);
           if(temp[j]>temp[max])
               max=j;
       }
    k*=temp[max];
    res[i]=s[max];
    }
    ans=(k*0.65-1)*2;
    for(int i=0;i<3;i++)
        printf("%c ",res[i]);
    printf("%.2lf\n",ans+0.001);
    return 0;
}

发表于 2020-03-30 15:42:18 回复(0)
1. 对于(4.1*3.0*2.5*0.65-1)*2=37.975, 浮点运算变成了37.974999999999994,加一个小量0.0001再舍入即可
2.Java中可以使用BigDecimal精确计算,四舍五入RoundingMode.HALF_UP
发表于 2020-02-28 14:54:13 回复(0)
lst = ['W','T','L']
first = list(map(float,input().split()))
second = list(map(float,input().split())) 
third = list(map(float,input().split()))
m1,m2,m3 = max(first),max(second),max(third)
i1,i2,i3 = first.index(m1),second.index(m2),third.index(m3)
profit = str(round((m1*m2*m3*0.65-1)*2,3))
temp=[]
for i in profit:
    temp.append(i)
if temp[len(temp)-1]=='5':
    index1 = 0
    for i in range(0,len(temp)):
        if temp[i]=='.':
            index1 = i
            break
    wei = len(temp)-index1-2
    p1 = ''
    for i in range(0,len(temp)-1):
        p1 = p1+temp[i]
    profit = "%.2f"%(float(p1) + pow(0.1,wei))
else:   
    profit = '%.2f'%float(profit)
print(lst[i1]+" "+lst[i2]+" "+lst[i3]+" "+profit)
如果说这道题有难点的话,估计就是那个四舍五入的时候怎么解决机器截断处理吧
发表于 2018-12-05 23:08:18 回复(0)
思路: 加上 0.000001就很有灵性了

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
#define NUM 9

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct odds
{
    double WTL[3];
    string MaxTitle; 
    double max;
};

string SWTL = "WTL";

int main(int argc, char** argv) {
    //ifstream cin("test.txt");
    struct odds odd[3] = {0};
    
    for(int i=0; i< 3; i++)
    {
        cin >> odd[i].WTL[0] >> odd[i].WTL[1] >> odd[i].WTL[2];
        for(int j=0; j<3; j++)
        {
            if(odd[i].WTL[j] > odd[i].max)
            {
                odd[i].max = odd[i].WTL[j];
                odd[i].MaxTitle = SWTL[j];
            }
        }
        cout << odd[i].MaxTitle << " ";
    }
    printf("%.2lf\n", (odd[0].max * odd[1].max * odd[2].max * 0.65 - 1) *2 + 0.000000001);
    return 0;
}

发表于 2018-08-19 18:53:33 回复(1)
#include <cstdio>
#include <cmath>

int main(){
    double a, b, c, profit = 1.000;
    for(int i = 0;i < 3;i++){
        scanf("%lf %lf %lf", &a, &b, &c);
        if(a > b && a > c){
            printf("W ");
            profit *= a;
        }else if( b > a && b >c){
            printf("T ");
            profit *= b;
        }else{
            printf("L ");
            profit *= c;
        }
    }
    printf("%.2f", (profit * 0.65 - 1) * 2 + 0.001);

    return 0;
}

发表于 2018-02-28 10:45:31 回复(1)
//数组初始化使用double类型会产生精度不对问题,以下代码使用float正确

#include <iostream>
#include <vector>
#include <iomanip>

using namespace std;

vector<vector<float>> ThreeGames;//初始化输入的数组
vector<vector<float>> maxodds;
float maxprofit = 0;


int main()
{
    ThreeGames.resize(3, vector<float>(3, 0));//定义数组大小
    maxodds.resize(3, vector<float>(2, 0));

    for (int i = 0; i < 3; i++)//输入数组
        for (int j = 0; j < 3; j++)
            cin >> ThreeGames[i][j];

    for (int i = 0; i < 3; i++)//选出每行最大值
        for (int j = 0; j < 3; j++)
            if (ThreeGames[i][j]>maxodds[i][0])
            {
                maxodds[i][0] = ThreeGames[i][j];
                maxodds[i][1] = j+1;
            }

    maxprofit = (maxodds[0][0] * maxodds[1][0] * maxodds[2][0] * 0.65 - 1) * 2;//计算最大收益

    for (int i = 0; i < 3;i++)//输出结果
        switch ((int)maxodds[i][1])
        {
            case 1:cout << "W "; break; 
            case 2:cout << "T "; break;
            case 3:cout << "L "; break;
            default:break;
        }
    cout << setiosflags(ios::fixed);//保留小数点后两位
    cout << setprecision(2)<<maxprofit << endl;

    system("pause");
    return 0;
}

发表于 2018-02-21 12:13:08 回复(0)
确保输出精度,四舍五入

#include <iostream>
#ifdef ONLINE_JUDGE
#else
#include <fstream>
#endif // ifdef

using namespace std;

int main()
{
#ifdef ONLINE_JUDGE
#else
    ifstream cin("case.txt");
#endif // 

    char status[3] = { 'W', 'T', 'L' };
    double sum = 1;
    double max = 0;
    int max_id = 0;

    double x = 0;
    for (int i = 0;i < 3;++i)
    {
        max = 0;
        for (int j = 0; j < 3;++j)
        {
            cin >> x;
            if (x > max)
            {
                max = x;
                max_id = j;
            }
        }
        sum *= max;
        cout << status[max_id] << " ";
    }
    sum = (sum * 0.65 - 1) * 2 + 0.0005;
    printf("%.2f\n", sum);
    getchar();
    return 0;
}

发表于 2017-11-28 11:38:03 回复(1)
嗯,太水了
#include <iostream>
#include <cstdio>
using namespace std;

struct Score{
    float num[3];
    void read(){
        cin >> num[0] >> num[1] >> num[2];
    }
    int get_max_id(){
        float max = 0.0, idx;
        for(int i=0;i<3;i++){
            if(max <= num[i]){
                max = num[i];
                idx = i;
            }
        }
        return idx;
    }
};

int main(){
    char choose[3] = {'W', 'T', 'L'};
    Score s[3];
    float money = 1.0;
    for(int i=0;i<3;i++){
        s[i].read();
        money *= s[i].num[s[i].get_max_id()];
    }
    for(int i=0;i<3;i++)
        printf("%c ", choose[s[i].get_max_id()]);
    printf("%.2f\n", (money*0.65 - 1)*2);
    return 0;
}

发表于 2017-08-05 20:51:51 回复(0)
import java.text.DecimalFormat;
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		float[] max = new float[3];
		String[] num = new String[3];
		String[] odd = {"W","T","L"};
		int tmp = 0;
		for(int i=0;i<3;++i){
			for(int j=0;j<3;++j){
				float now = in.nextFloat();
				if(max[i]<now){
					max[i]=now;
					tmp = j;
				}
			}
			num[i] = odd[tmp];    
		}
		in.close();
		double out = (max[0]*max[1]*max[2]*(0.65)-1)*2;
		// 表示成只有两位小数
		DecimalFormat f = new DecimalFormat("0.00");
		System.out.print(num[0]+" "+num[1]+" "+num[2]+" "+f.format(out));
	}
}

发表于 2017-06-15 15:33:16 回复(0)
注意一下结果四舍五入就行。
例如:
37.975 四舍五入应该是37.98
令res = 37.975
则:
(int)(res * 100 + 0.500001)/100.0 保留两位小数即为 37.98
发表于 2015-12-29 19:33:24 回复(0)
Tips:PAT里要四舍五入,这里不需要直接保留俩位就可以了~呵呵
发表于 2015-07-16 23:08:36 回复(3)