c++输出

输出保留n个有效数字

#include<iomanip>
cout << setprecision(n) << res << endl;

输出保留n位小数

#include<iomanip>
cout << setiosflags(ios::fixed);
cout << setprecision(n) << res << endl;

输出预设宽度n

#include<iomanip>
cout << setw(n) << res << endl;

输出预设宽度n,并将空缺位置用c填充

#include<iomanip>
cout << setw(n) << setfill('#') <<res << endl;

eg:

hw机试题-小球5次下落高度

#include<iostream>
#include<iomanip>
using namespace std;

int main(){
    int n ;
    cin >> n;
    double h = n/1.0;
    cout << setiosflags(ios::fixed);
    cout << setprecision(6) <<23*h/8 << endl;
    cout << h/32 << endl;
}
2.875000
0.031250
全部评论

相关推荐

迟缓的斜杠青年巴比Q...:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务