PTA:7-57 打印沙漏 (20分)--解析

7-57 打印沙漏 (20分)

在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,rest=0,i, k;
	char ch;
	cin >> n >> ch;
	for(i=1; ; i++){
		if(2*i*i-1>n){
			rest = n-(2*(i-1)*(i-1)-1);
			break;
		}
	} 
	i--;
	k = i;
//	cout << "i=" << k << endl;
	while(k){
		for(int j=0; j<i-k; j++){
			cout << " "; 
		}	
		for(int j=0; j<2*k-1; j++){
			cout << ch;
		}		
		cout << endl;
		k--;
	}
	k = 2;
	while(k<=i){
		for(int j=0; j<i-k; j++){
			cout << " "; 
		}
		for(int j=0; j<2*k-1; j++){
			cout << ch;
		}
		cout << endl;
		k++;
	}
	cout << rest;
	return 0;
} 

欢迎大家批评改正!让我们共同进步,加油!!!

发布了39 篇原创文章 · 获赞 2 · 访问量 3430

猜你喜欢

转载自blog.csdn.net/weixin_43581819/article/details/103971853