为什么输出是整数而不是双精度整数?

问题描述 投票:0回答:1
                           I just don't understand why...

  • 我所有的数据类型都是double,如果其中一些不是,则将它们隐式转换为double。
  • 没有警告和错误。

  • 关键是代码的输出是整数,而不是其余代码。

  • 您也可以在Coliru中看到SC
  • [我知道#include<bits/stdc++.h>不是标准库中的头文件,只是GCC的内部实现,但这不是重点。
  • 对不起,我无法翻译这段代码将解决的问题,因为它太长了,但是如果您有兴趣,可以咨询link

请帮助我。

您可以输入以下示例

6 6 7
01234 880
a1903 199
ydjh2 200
wehu8 300
dx86w 220
missing 400
ydhfu77 99
wehu8 55
ydjh2 98
dx86w 88
a1903 86
01234 39
ydhfu77 88
a1903 66
01234 58
wehu8 84
ydjh2 82
missing 99
dx86w 81

The代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<string, array<double, 5>> PAIR;
bool cmp(PAIR x, PAIR y) {
    if (round(x.second[3]) != round(y.second[3])) return x.second[3] > y.second[3];
    else return x.first < y.first;
}
int main() {
    map<string,  array<double, 5>> student;
    int p, m, n, online, score;
    string name;
    cin >> p >> m >> n;
    while (p--) {
        cin >> name >> online;
        student[name][0] = online;
    }
    while (m--) {
        cin >> name >> score;
        if (score) student[name][1] = score;
        else student[name][4] = 1;
    }
    while (n--) {
        cin >> name >> score;
        student[name][2] = score;
        int mid = student[name][1];
        if (mid > score) student[name][3] = mid * 0.4 + score * 0.6;
        else student[name][3] = score;
    }
    for (auto p = student.begin(); p != student.end(); ) {
        if (p->second[0] < 200 || round(p->second[3]) < 60 || p->second[0] > 900) student.erase(p++);
        else p++;
    }
    vector<PAIR> answer(student.begin(), student.end());
    sort(answer.begin(), answer.end(), cmp);
    for (auto v : answer) {
        if (!v.second[1] && !v.second[4]) cout  << v.first << " " << v.second[0] << " " << -1 << " " << v.second[2] << " " << round(v.second[3]) << endl;
        else cout  << v.first << " " << v.second[0] << " " << v.second[1] << " " << v.second[2] << " " << round(v.second[3]) << endl;
    }
    return 0;
}
c++
1个回答
0
投票

您仅看到完整的值,因为默认情况下,double的打印精度有限(例如6位数字),而尾随的零将被忽略。

也使用四舍五入的值:

round(v.second[3])

尝试添加

#include <iomanip>
std::cout << std::fixed << std::setprecision(2);
© www.soinside.com 2019 - 2024. All rights reserved.