结果样式
------------------------------------
Province Area(km2) Pop.(10K)
------------------------------------
Anhui 139600.00 6461.00
Beijing 16410.54 1180.70
Chongqing 82400.00 3144.23
Shanghai 6340.50 1360.26
Zhejiang 101800.00 4894.00
------------------------------------
解题
按题目要求格式与内容输出即可
这个主要是格式控制,可以使用不同语言的格式控制语句进行格式化输出。
关键点
- 宽度设置
- (字符)占位补齐
- 对齐方式
- 也可以使用浮点数的方式进行输出
参考代码
//7-5.c
#include <cstdio>
int main()
{
printf("%s
","------------------------------------" );
printf("%-11s %s %s
", "Province","Area(km2)","Pop.(10K)");
printf("%s
","------------------------------------" );
printf("%-11s %9s %s
", "Anhui","139600.00","6461.00");
printf("%-11s %9s %s
", "Beijing","16410.54","1180.70");
printf("%-11s %9s %s
", "Chongqing","82400.00","3144.23");
printf("%-11s %9s %s
", "Shanghai","6340.50","1360.26");
printf("%-11s %9s %s
", "Zhejiang","101800.00","4894.00");
printf("%s
","------------------------------------" );
return 0;
}