3 条题解
-
3
说明: 考查输出格式控制。
思路分析: 根据题意,按顺序写代码即可,关键是输出格式的控制。
易错点: C++的setw和C语言的printf,对输出结果默认是右对齐。刚好适合本题,直接使用即可。
题目对输出结果要求相邻两个变量之间要用一个空格可开,所以在按8个字符的宽度进行输出时, 不要忘了变量之间的空格。
参考代码 - C++:
#include <iostream> #include <iomanip> using namespace std; int main() { int a, b, c; cin >> a >> b >> c; // 可以三行合并为一行写完,不过太长了 cout << setw(8) << a << ' '; cout << setw(8) << b << ' '; cout << setw(8) << c; return 0; }
参考代码 - C语言:
#include <stdio.h> int main() { int a, b, c; scanf("%d %d %d", &a, &b, &c); printf("%8d %8d %8d", a, b, c); return 0; }
-
1
解析
相信大家在使用时一定被输出如何设置宽度、如何用特殊符号占位困扰过,以至于在遇见类似问题时不得不用
#include< cstdio >
这个开头文件,去使用printf来解决问题。但其实C++中有这样一个函数可以解决问题的。setw(n)
函数是C++中关于在输出操作中使用的字段宽度设置函数,其中表示字段宽度,而它的开头文件一定要记住哦!!#include<iomanip>
注意,,的数据范围,这个非常重要!!!
参考代码:
#include<bits/stdc++.h> #include<iomanip> using namespace std; int main() { long long a,b,c; cin>>a>>b>>c; cout<<setw(8)<<a<<" "; cout<<setw(8)<<b<<" "; cout<<setw(8)<<c; return 0; }
- 1
信息
- ID
- 102
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 5
- 标签
- 递交数
- 328
- 已通过
- 131
- 上传者