A1011 World Cup Betting 20分
题目描述:
题目大意:
给出三场比赛的赔率,输出赔率的最大那个字母并计算两元总的利益。
思路:
①:边输入边比较,找出赔率最大的位置下标,用t存储起来。
②:可以利用字符数组,找出下标后输出对应下标的字母。
AC代码1:
#include<cstdio>
char b[4]={'0','W','T','L'};
int main()
{
double ans=1.0;
for(int i=1;i<=3;i++){
double temp=0.0,a;
int t;
for(int j=1;j<=3;j++){
scanf("%lf",&a);
if(a>temp){
temp=a;
t=j;
}
}
printf("%c ",b[t]);
ans*=temp;
}
printf("%.2f",(ans*0.65-1)*2);
return 0;
}
这个代码时间复杂度为O(n²)。那么如何把时间复杂度降到O(n)呢?
AC代码2:
#include<cstdio>
struct node{
double w,t,l;
}e[10];
char a[4]={'0','W','T','L'};
int k[4]={0,1,2,3};
double b[4];
double ans=1.0;
int cmp(double a,double b,double c)
{
if(a>b&&a>c) return 1;
if(b>a&&b>c) return 2;
return 3;
}
int main()
{
for(int i=1;i<=3;i++){
scanf("%lf%lf%lf",&e[i].w,&e[i].t,&e[i].l);
int t=cmp(e[i].w,e[i].t,e[i].l);
printf("%c ",a[t]);
if(t==1) b[i]=e[i].w;
else if(t==2) b[i]=e[i].t;
else b[i]=e[i].l;
ans*=b[i];
}
printf("%.2f",(ans*0.65-1)*2);
return 0;
}