首页 > 编程语言 > 详细

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

时间:2016-03-29 14:53:09      阅读:331      评论:0      收藏:0      [点我收藏+]

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


技术分享

技术分享


技术分享




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

原文:http://blog.csdn.net/geekmanong/article/details/51004906

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!