只需要求出最大可以有多大然后输出沙漏图形最后输出剩余就行了,码量有点大。
s = input().split(" ")
n , c = int(s[0]) , s[1]
cnt = 999
while (cnt // 2 + 1) * (cnt // 2 + 1) * 2 - 1 > n :
cnt -= 2
for i in range(cnt // 2 + 1) :
for j in range(1 , cnt + 1) :
if j <= i or j >= cnt - i + 1 :
print(" " , end = "")
else :
print(c , end = "")
print()
for i in range(cnt // 2 - 1 , -1 , -1) :
for j in range(1 , cnt + 1) :
if j <= i or j >= cnt - i + 1 :
print(" " , end = "")
else :
print(c , end = "")
print()
print(n - (cnt // 2 + 1) * (cnt // 2 + 1) * 2 + 1)
#include <bits/stdc++.h>
using namespace std ;
int n , cnt = 999 ;
char c ;
int main() {
cin >> n >> c ;
while((cnt / 2 + 1) * (cnt / 2 + 1) * 2 - 1 > n) {
cnt -= 2 ;
}
for(int i = 0 ; i <= cnt / 2 ; i ++) {
for(int j = 1 ; j <= cnt ; j ++) {
if(j <= i || j >= cnt - i + 1) {
cout << " " ;
} else {
cout << c ;
}
}
cout << "\n" ;
}
for(int i = cnt / 2 - 1 ; i >= 0 ; i --) {
for(int j = 1 ; j <= cnt ; j ++) {
if(j <= i || j >= cnt - i + 1) {
cout << " " ;
} else {
cout << c ;
}
}
cout << "\n" ;
}
cout << n - (cnt / 2 + 1) * (cnt / 2 + 1) * 2 + 1 ;
return 0 ;
}
- 时间复杂度:。
- 空间复杂度:。
- 难度:上位红/下位橙。
暂无评论