C++保留小数问题

C++中保留小数需要引入头文件iomanip,下面代码展示了如何保留两位小数。

#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;



int main() {
    int n;
    double a, b, c;
    double s = 0;
    double p;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> a >> b >> c;
        p = (a+b+c)/2;
        if (a + b <= c || a + c <= b || b + c <= a) {
            s = 0;
            //保留两位小数
            cout << setiosflags(ios::fixed) << setprecision(2) << s << endl;
        } else {
            s = sqrt(p*(p-a)*(p-b)*(p-c));
            cout << setiosflags(ios::fixed) << setprecision(2) << s << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_32273417/article/details/87925893