2
我想创建一个C++程序来计算3张的正数格式化数字到n位小数没有四舍五入
的平均其中(x,Y,Z)> 0和(X,Y,Z)< = 10
我有这样的代码:
#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;
int main()
{
int x,y,z;
cin >> x >> y >> z;
double x1,y1,z1,ma;
x1 = x;
y1 = y;
z1 = z;
if(x>0 && x<=10 && y>0 && y<=10 && z>0 && z<=10)
ma = (x1+y1+z1)/3;
else
return 0;
printf("%.2f" , ma);
return 0;
}
对于x = 9,Y = 9,并且z = 5的平均值为23/3 = 7.666666666666667并且当我格式化为2位小数,其结果将是7.67,但我想出现7.66而不是7.67。
请问,有人可以帮助我吗?
谢谢!