题意:
给出12个月工资,算每月工资多少,最多输出2位小数,如果结果是1位小数输出1位,整数则不输出小数位。
输入:
2 100.00 489.12 12454.12 1234.10 823.05 109.20 5.27 1542.25 839.18 83.99 1295.01 1.75 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00 100.00
输出:
$1581.42 $100
分析:
控制精度,都*100,%100=0就是整数,%10=0就是一位小数,之后就是2位小数,,学一个函数,round()四舍五入函数。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
double a,sum=0;
for(int i=0; i<12; i++)
{
scanf("%lf",&a);
sum+=a;
}
sum/=12;
sum*=100;
int tmp=round(sum);
if(tmp%100==0)
printf("$%d\n",tmp/100);
else if(tmp%10==0)
printf("$%.1lf\n",tmp/100.0);
else
printf("$%.2lf\n",tmp/100.0);
}
return 0;
}