当前位置:网站首页>[PTA] l1-002 printing hourglass

[PTA] l1-002 printing hourglass

2022-04-23 20:23:00 Maybe

Prepare knowledge points

A sequence of equal differences
General term sum formula
 Insert picture description here

Original link

L1-002 Print hourglass

Their thinking

Calculate the number of rows by using the sequence of equal differences

ac Code

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
int main() {
    
	int t;
	char a;
	cin >> t;
	getchar();
	cin >> a;
	
	int n  = (t + 1) / 2;
	n = int(sqrt(n));// Number of upper triangular rows 
	t = t - n * n * 2 + 1; 
	
	for(int i = 0; i < n; i++){
    
		for(int j = 0; j < i; j++)
			cout << ' ';
		for(int j = 0; j < 2 * (n - i) - 1; j++)
		    cout << a;
		cout << endl;
	}
	for(int i = n - 2; i >= 0; i--){
    
		for(int j = i; j > 0; j--)
			cout << ' ';
		for(int j = 0; j < 2 * (n - i) - 1; j++)
			cout << a;
		cout << endl;
	}
	cout << t;
	return 0;
}

版权声明
本文为[Maybe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232020000365.html

随机推荐