1027 打印沙漏 (20分)
https://pintia.cn/problem-sets/994805260223102976/problems/994805294251491328
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> using namespace std; int main() { int n,min=1001,c,d,e=0; char chr,chr1=' '; cin>>n>>chr; for(int i=1;i<=23;i++) { if((n-(2*i*i-1))<min&&(n-(2*i*i-1)>=0)) { c=i; min=n-(2*i*i-1); } } for(d=2*c-1;d>=1;d-=2) { if(d!=2*c-1) for(int j=1;j<=e;j++) cout<<chr1; for(int j=1;j<=d;j++) cout<<chr; cout<<endl; if(d!=1) e++; } for(d=3;d<=2*c-1;d+=2) { e--; if(d!=2*c-1) for(int j=1;j<=e;j++) cout<<chr1; for(int j=1;j<=d;j++) cout<<chr; cout<<endl; } cout<<min; return 0; }