PAT A1011 World Cup Betting
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
word | meaning |
---|---|
trophy | n. 奖品 |
all manner of | 各种各样的 |
Lottery | 彩票 |
odds assigned | 概率分布 |
times | 乘以 |
-
思路1:
使用<iostream>
中的max_element
, 选择每行最大的累乘起来,带入公式即可 -
code1:
#include <vector>
#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;
char sign[3] = {'W','T','L'};
vector<double> v[3];
int main(){
double tmpin;
for(int i = 0; i < 3; ++i){
for(int j = 0; j < 3; ++j){
scanf("%lf", &tmpin);
v[i].push_back(tmpin);
}
}
double interest = 1.0;
for(int i = 0; i < 3; ++i){
// 等价:vector<int>::iterator max = max_element(begin(v[1]), end(v[1])); //在algorithm头文件下
// vector<int>::iterator max = max_element(v[1].begin(), v[1].end());
auto max = max_element(v[i].begin(), v[i].end()); //添加-std=c++11
// cout << *max << max - v[0].begin(); //输出元素/位置
printf("%c ", sign[max-v[i].begin()]);
interest *= *max;
}
//!!!:WRONG 1:不需要加0.005???
// printf("%.2f", (interest*0.65 - 1) * 2 + 0.005); //加 0.005是为了实现4舍5入
printf("%.2f", (interest*0.65 - 1) * 2);//输出不需要lf
return 0;
}
- code2: 简化
#include <algorithm>
using namespace std;
char sign[3] = {'W','T','L'};
double ans[4];
int main(){
double tmpin;
double interest = 1.0;
for(int i = 0; i < 3; ++i){
for(int j = 0; j < 3; ++j){
scanf("%lf", &ans[j]);
}
auto max = max_element(ans, ans+3) ;
printf("%c ", sign[max-ans]);
interest *= *max;
}
printf("%.2f", (interest*0.65 - 1) * 2);//输出不需要lf
return 0;
}
- TIPS1:
algorithm
中的min_element(first, last, cmp)
和max_element(first, last, cmp)
1)这两个函数都可以返回一个数组(或vector)中最大最小值的迭代器it,通过*it
就可以找到相应的元素,it - begin()
即再数组中的位置
2)最后一个参数可以是函数指针,也可以是函数对象,定义<
的意义(比较规则),可省略(即按照默认:1<2)