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

来源:互联网 发布:网络麻将换牌软件赌博 编辑:程序博客网 时间:2024/05/19 07:26

Problem Description
求实数的绝对值。
Input
输入数据有多组,每组占一行,每行包含一个实数。
Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
Sample Input
123-234.00
Sample Output
123.00234.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;}









2 0
原创粉丝点击