HDU

来源:互联网 发布:mpp 文件查看 mac 编辑:程序博客网 时间:2024/06/07 10:13

学了一招,某位四舍五入 ,就是加上0.x5*0.1(比精度大1),在乘以精度范围,变成int。

给定十二个数,要求保留两位,四舍五入

#include <iostream>#include <cstdio>#include <cstdlib>#include <queue>using namespace std;int main(){    int n;   cin>>n;     while(n--)     {   double all=0;         double s;         for(int i=1;i<=12;i++)           {cin>>s;           all+=s;}         all/=12;         int m=(all+0.005)*100;         if(m%100==0)// 小数点第一位是0,是整数。            printf("$%.0f\n",all);         else if(m%10==0)            printf("$%.1f\n",all);         else            printf("$%.2f\n",all);     }    return 0;}
原创粉丝点击