#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;
}