HDU 4493--水题+控制精度

题意:

给出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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值