#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
float x;
float res;
while (cin >> x)
{
res = x>=0 ? x : -1 * x;
printf("%.2f\n",res);
}
return 0;
}将x,res由float类型改为double类型就行:
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
double x;
double res;
while (cin >> x)
{
res = x>=0 ? x : -1 * x;
printf("%.2f\n",res);
}
return 0;
}

本文介绍了一种通过C++程序来计算输入实数的绝对值的方法,并提供了完整的代码实现。程序能够处理多组输入数据,输出保留两位小数的绝对值。
8273

被折叠的 条评论
为什么被折叠?



