PAT1027打印沙漏

用递推公式推半天的都没搞出来,还是天哥牛皮啊

// 2 * n ^ 2 - 1
#include <bits/stdc++.h>
using namespace std;
int main(){
	int n,i = 1,d = 0;
	int sum = 0;
	int line;
	char y;
	
	cin>>n>>y;
	while(1){
		if(sum > n){
			d = n - (2 *(i - 2)*(i - 2) - 1);
			break;
		}
		sum = 2 * i * i - 1;
		i++;
	}
//	cout<<n<<" "<<sum<<" "<<d<<" ";
	i = i - 2;//column
//	cout<<i;
	for(int c = i; c >= 1; c--){// 3 2 1 three column
		for(int v = 0; v < i - c; v++){
			cout<<" ";
		}
		for(int v = 0; v < 2 * c - 1; v++){
			cout<<y;
		}
		cout<<endl;		
	}
	for(int c = 2; c <= i; c++){//2 3 two
		for(int v = 0; v < i - c; v++){
			cout<<" ";
		}
		for(int v = 0; v < 2 * c - 1;v++){
			cout<<y; 
		}
		cout<<endl;
	}
	cout<<d<<endl;
	return 0;
	
}


猜你喜欢

转载自blog.csdn.net/i_hope_soar/article/details/80033882