当前位置:网站首页>【PTA】L1-002 打印沙漏

【PTA】L1-002 打印沙漏

2022-04-23 20:20:00 也许会吧

预备知识点

等差数列
通项和求和公式
在这里插入图片描述

原题链接

L1-002 打印沙漏

解题思路

运用等差数列计算行数

ac代码

#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));//上三角行数
	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;
}

版权声明
本文为[也许会吧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_55475680/article/details/124351462