为了参加学校的社团风采展,怡山小学数学组的同学们决定画一座圆周率山,以宣传圆周率。
已知圆周率为:3.
1415926535 8979323846 2643383279 5028841971 6939937510
5820974944 5923078164 0628620899 8628034825 3421170679
8214808651 3282306647 0938446095 5058223172 5359408128
4811174502 8410270193 8521105559 6446229489 5493038196
输入格式:
输入山的高度,为一个不超过10的正整数。
输出格式:
以上尖下宽,左右对称的三角形形式,给出圆周率的前若干位(不含小数点)。注意:每行均以数字结尾,即数字右边无空格。
输入样例1:
1
输出样例1:
3
输入样例2:
4
输出样例2:
3
141
59265
3589793
这里需要用到setw()和setfill()函数
#define PI "314159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196"
#include <iostream>
#include<ostream>
#include<string>
#include<iomanip>
using namespace std;
int main() {
int n;
int i = 0;
//int count=0;
string c=PI;
//cout << c << endl;
cin >> n;
for (int a = 0; a < n; a++) {
cout << setw(n-a)<<setfill(' ');//用空格填充字符串的左侧
for (int b = (a*a); b < (a*a + 2*a+1); b++)
{
cout << c[b];
}
//cout << setw(n-a)<<setfill(' ');
cout << endl;
}
system("pause");
return 0;
}