c++ - 如何去掉小数点后的这些 0?
问题描述
我的输出应该是这样的:
n Recursive time_r(s) Iterative time_i(s)
1 1 0.000000 1 0.000000
2 1 0.000000 1 0.000000
3 2 0.000000 2 0.000000
4 3 0.000000 3 0.000001
. . .
43 433494437 2.015535 433494437 0.000002
44 701408733 3.270357 701408733 0.000002
45 1134903170 5.258732 1134903170 0.000002
但相反,我得到了这个:
1 1.0000 0.000000 1.0000 0.000000
2 1.0000 0.000000 1.0000 0.000000
3 2.0000 0.000000 2.0000 0.000000
4 3.0000 0.000000 3.0000 0.000001
. . .
43433494437.0000 2.015535433494437.0000 0.000002
44701408733.0000 3.270357701408733.0000 0.000002
451134903170.0000 5.2587321134903170.0000 0.000002
这是我的代码的样子,我最初让它完美地工作,但后来我被告知要返回一个 std::pair 所以我更改了代码,然后输出改变了。我在不需要零但没有雪茄的代码之前尝试了 std::noshowpoint 。
std::cout << std::fixed;
std::cout << "\nFibonacci Output" << std::endl;
std::cout << std::setw(4) << "n"
<< std::setw(15) << "Recursive"
<< std::setw(12) << "time_r(s)"
<< std::setw(16)
<< std::setw(15) << "Iterative"
<< std::setw(12) << "time_i(s)" << std::endl;
for (int i{ 1 }; i < 44; ++i) {
std::cout << std::setw(4) << i;
std::cout << std::setw(15) << time_r(i).first;
std::cout << std::setw(12) << time_r(i).second;
std::cout << std::setw(16) << std::setw(15) << time_i(i).first;
std::cout << std::setw(12) << time_i(i).second << std::endl;
}
解决方案
推荐阅读
- postgresql - Heroku Postgres 专业层数据库计划显示“没有备份?
- c# - 将 IIS 与 .NET Core 2.2 Web API 一起使用时自动生成的 web.config 中的硬编码路径
- javascript - 在jsfiddle中,运行时错误消息“error”:“key missing:title”是什么意思?
- reactjs - 尽管修改了临时对象数组而不是状态,但错误“在调度之间检测到状态突变”
- python - 在 spyder 中在线运行 Dropbox 中存在的 python 脚本(不在桌面中)
- python-3.x - 我是 Python 的首选学生,并且有一个关于链表的问题
- azure - Istio 侧车外置存储安装问题
- php - 从 3 个表中获取与具有 haMany 关系的一个表相关的数据
- python - 重新打包一个原来通过pip安装的修改库
- c# - 当文本框有焦点时,有没有办法让一些代码工作?