我有1道题错了,求怎样修改

这是题目


这是代码:
#include<bits/stdc++.h>
using namespace std;
int main( )
{
double a,n;
cin>>a;
if(a>0)
{
cout<<fixed<<setprecision(2)<<-a<<endl;
}
else if(a<0)
{
a=-a;
cout<<fixed<<setprecision(2)<<a<<endl;
}
else
{
cout<<fixed<<setprecision(2)<<a<<endl;
}
return 0;
}
求如何修改

3 个赞

6666

4 个赞

666

3 个赞

没个有用的回复

3 个赞

可以把否则去掉0归到>=0里去

3 个赞

可以这么写:

#include<bits/stdc++.h>
using namespace std;
int main( ){
    double n;
    cin>>n;
    printf("%.2lf",abs(n));
    return 0;
}

你应该知道有个东西叫abs

4 个赞

你坐在那

3 个赞
#include<bits/stdc++.h>
using namespace std;
int main()
{
	double a,n;
	cin>>a;
	if(a>0)
	{
		cout<<fixed<<setprecision(2)<<a<<endl;
	}
	else if(a<0)
	{
		a=-a;
		cout<<fixed<<setprecision(2)<<a<<endl;
	}
	else
	{
		cout<<fixed<<setprecision(2)<<a<<endl;
	}
	return 0;
}
3 个赞