0
点赞
收藏
分享

微信扫一扫

A1011 World Cup Betting (20 分| 查找元素,附详细注释,逻辑分析)


写在前面

  • 思路分析
  • 3个数一组形式读取
  • 读取完1组后输出最大值代表的字母,然后同时ans累乘该最大值,
  • 根据公式输出结果
  • 题目简单,10分钟a题

测试用例

input:
1.1 2.5 1.7
1.2 3.1 1.6
4.1 1.2 1.1

output:
T T W 39.31

ac代码

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
char c[4] = {"WTL"};
double ans = 1.0;
string res = "";
for(int i=0; i<3; i++)
{
vector<double> v(3);
for(int j=0; j<3; j++)
scanf("%lf", &v[j]);
int max_inx = max_element(v.begin(), v.end())-v.begin();
ans *= v[max_inx];
res = res + c[max_inx] + " ";
}
printf("%s%.2f", res.c_str(), (ans*0.65-1)*2);
return 0;
}

  • 参考代码1

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
string ss = "WTL";
double ans = 1.0;
for(int i=0; i<3; i++)
{
vector<double> v(3);
for(int j=0; j<3; j++)
scanf("%lf", &v[j]);
int max_inx = max_element(v.begin(), v.end())-v.begin();
ans *= v[max_inx];
printf("%c ", ss[max_inx]);
}
printf("%.2f",(ans*0.65-1)*2);
return 0;
}

  • 参考代码2

#include <cstdio>
using namespace std;
int main()
{
char c[4] = {"WTL"};
double ans = 1.0;
for(int i = 0; i < 3; i++)
{
double maxvalue = 0.0;
int maxchar = 0;
for(int j = 0; j < 3; j++)
{
double temp;
scanf("%lf", &temp);
if(maxvalue <= temp)
{
maxvalue = temp;
maxchar = j;
}
}
ans *= maxvalue;
printf("%c ", c[maxchar]);
}
printf("%.2f", (ans * 0.65 - 1) * 2);
return 0;
}

知识点小结

  • ​max_element​​示例

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[]= {3,1,5,7,2};
int max_inx = max_element(arr, arr+5)-arr;
cout << "[Int Array] Max inx: " << max_inx << endl << "Max element: " << arr[max_inx] << endl;

vector<int> varr= {3,1,5,7,2};
max_inx = max_element(varr.begin(), varr.end())-varr.begin();
cout << "[Vector Int] Max inx: " << max_inx << endl << "Max element: " << varr[max_inx];

return 0;
}


举报

相关推荐

0 条评论