Accelerated C++<4-2>
来源:互联网 发布:伯德生涯数据 编辑:程序博客网 时间:2024/04/29 04:22
//Write a program to calculate the squares of int
values up to 100. The program should write two columns: The first lists the value; the second contains the square of that value. Use setw
to manage the output so that the values line up in columns.
Hint
Determine the widest outputs for both columns.
Solution
The widest number that is being squared is 100, which is three digits. 100 squared is 10000, which is 5 digits, so the solution belows sets a width of 4 prior to sending the base to output and set a width of 6 prior to sending the result to output. Setting the widths one larger than the values contained will preserve at least one space to the left of each value.
#include <iomanip>
#include <iostream>
using std::cout;
using std::endl;
using std::setw;
int main()
{
for (int i = 1; i < 101; ++i)
{
cout << setw(4) << i << setw(6) << (i * i) << endl;
}
}
- Accelerated C++<4-2>
- Accelerated C++<4-4>
- Accelerated C++<4-1>
- Accelerated C++<4-3>
- Accelerated C++<4-5>
- Accelerated C++<4-6>
- Accelerated C++<4-7>
- Accelerated C++<3-2>
- accelerated 2-5-4
- Accelerated C++ Exercise 4-2
- 读《Accelerated C++》有感
- 【Accelerated C++】读书笔记(一)
- 【Accelerated C++】读书笔记(二)
- 【Accelerated C++】读书笔记(三)
- 【Accelerated C++】读书笔记(四)
- 【Accelerated C++】读书笔记(五)
- 【Accelerated C++】读书笔记(六)
- 【Accelerated C++】重点回顾
- Python学习笔记:字符串格式化方式
- Matlab中size()是什么意思?
- bind函数—绑定地址和端口
- Spring的jdbcTemplate操作
- 题目1054:字符串内排序 九度OJ
- Accelerated C++<4-2>
- 二叉树
- 设计模式——设计模式三大分类以及六大原则
- 华为笔试编程题(逆序求和)
- 旅游规划-单源最短路问题基于Dijkstra来考虑距离以及等距离时按收费更新来更新路径
- Java内存溢出详解及解决方案
- Android去除AppBarLayout阴影
- javaee 整合flex
- 阿里2017年春招技术面牛客网分享