您的位置:首页 > 其它

经典算法——求绝对值溢出问题

2016-03-29 12:52 441 查看
Problem Description

求实数的绝对值。

Input

输入数据有多组,每组占一行,每行包含一个实数。

Output

对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。

Sample Input

123
-234.00


Sample Output

123.00
234.00


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






内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: