欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

打印沙漏

程序员文章站 2022-03-13 16:02:00
...

打印沙漏

#include <iostream>
using namespace std;
/*求和公式s = n * a1 + n * (n-1)/2 * d*/ 
int main(){
	int n;char ch;
	cin >> n >> ch;
	int c = 0;
	while(2 * c * c + 4 * c + 1 <= n)
	  c++;
	
	//  cout << c << endl;
	  int kg = 0;
	  int gs = 1 + (c - 1) * 2;
	for(int i = 1; i <= c; i++){
		for(int j = 1; j <= kg;j++) 
			cout << " ";
		kg++;
		
		for(int k = 1; k <= gs;k++) 
			cout << ch;
		gs -= 2;
		cout << endl;	
	}
//	cout << kg << endl;
	kg-=2;gs+=2;
//	cout << kg << " " << gs << endl;
	for(int i = 2; i <= c; i++){
		gs += 2;
		for(int j = 1; j <= kg;j++)
			cout << " ";
		kg--;
		for(int k = 1; k <= gs;k++) 
			cout << ch;
		
		cout << endl;
		
	}
	cout << n - 2 * c * c  + 1 << endl;
	return 0;
} 
相关标签: 天梯赛