【题解】外币兑换
原题速达
题目描述:
小明刚从美国回来,发现手上还有一些未用完的美金,于是想去银行兑换成人民币。可是听说最近人民币将会升值,并从金融机构得到了接下来十二个月可能的美元对人民币汇率,现在,小明想要在接下来一年中把美金都兑换成人民币,请问最多能得到多少人民币?
输入格式:
输入的第一行是一个实数N(1.00<=N<=100.00),表示小明现有的美金数量。
接下来一行,包含12个实数ai,表示接下来十二个月的美元对人民币汇率。
输出格式:
输出一个小数R,表示小明最多能获得的人民币数量,结果保留两位小数。
样例输入:
46.91
6.31 6.32 6.61 6.65 5.55 5.63 6.82 6.42 6.40 5.62 6.78 5.60
样例输出:
319.93
解法一:尴尬的贪心
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
double money,maxn=-1,r;
cin>>money;
for(int i=1;i<=12;i++)
{
cin>>r;
maxn=max(maxn,money*r);
}
printf("%0.2f",maxn);
return 0;
}
解法二:sort排序
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
double money;
double c[15];
int main()
{
scanf("%lf", &money);
for(int i=1; i<=12; i++)
{
scanf("%lf", &c[i]);
}
sort(c+1,c+1+12);
printf("%.2f\n",money*c[12]);
return 0;
}
感谢您的阅读(´・ᴗ・`)点个赞吧!