c语言题:“从键盘上输入任意一个整数,然后输出它的绝对值”怎么解答?

如题所述

    方法一:

    //用数学函数
    #include<stdio.h>
    #include<math.h>
    void main()
    {
    int a;
    scanf("%d",&a);
    printf("%d\n",abs(a));
    }

    方法二:
    //判断
    #include<stdio.h>
    void main()
    {
    int a;
    scanf("%d",&a);
    if(a>=0)
    printf("%d\n",a);
    else
    printf("%d\n",-a);
    }

Problem Description

求实数的绝对值。

Input

输入数据有多组,每组占一行,每行包含一个实数。输入文件直到EOF为止!

Output

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

Example Input

123
-234.00

Example Output

123.00234.00

答案:

#include <iostream>

#include <cmath>

#include <iomanip>

using namespace std;

int main()

{

double a;

while(cin >> a)

{

cout<< fixed <<setprecision(2) << abs(a) << endl;

}

return 0;

}

温馨提示:答案为网友推荐,仅供参考
相似回答