林惜城


私信TA

用户名:reminder

访问量:27413

签 名:

等  级
排  名 94
经  验 8463
参赛次数 0
文章发表 95
年  龄 0
在职情况 学生
学  校 西安电子科技大学
专  业

  自我简介:

哈姆


解题思路:

%6.2f就是宽度为6,小数位精度为2的浮点数。按题目要求格式写一个打印的函数,再调用。记得加上iomanip头文件。


注意事项:

setw()每输出一个数字都要写一句,setprecision()可以写在开头一劳永逸。


参考代码:

#include <iostream>
#include <iomanip>

using namespace std;

void printFloat(float num, short width, short precision); // 打印指定宽度和精度的浮点数
int main() {
	float val = 0;
	cin >> val;
	printFloat(val,6, 2);  // 宽度6,精度2
	return 0;
}
void printFloat(float num, short width, short precision) {
	// setw()每输出一个数字都要写一句,setprecision()可以写在开头
	cout << fixed << setprecision(precision);
	cout << setw(width) << num << endl
	     << setw(width) << num << " " << setw(width) << num << endl
	     << setw(width) << num << " " << setw(width) << num << " " << setw(width) << num << endl;
}


 

0.0分

1 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区