java:
1 import java.io.*; 2 3 public class Main { 4 static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 5 static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); 6 public static void main(String[] args) throws IOException { 7 String[] s=in.readLine().split("\\s"); 8 int n=Integer.parseInt(s[0]); 9 int l=1,r=23,mid=0; 10 while(l<r){ 11 mid=(l+r+1)>>1; 12 if((mid<=1?1:1+2*(mid-1)*(mid+1))<=n) l=mid; 13 else r=mid-1; 14 } 15 int i=r,sum=2*r-1; 16 while(i>0){ 17 for(int m=0; m<(sum-(2*i-1))>>1; m++) out.print(' '); 18 for(int j=1; j<=2*i-1; j++) out.print(s[1]); 19 i--; 20 out.println(); 21 } 22 23 i=2; 24 while(i<=r){ 25 for(int m=0; m<(sum-(2*i-1))/2; m++) out.print(' '); 26 for(int j=1; j<=2*i-1; j++) out.print(s[1]); 27 i++; 28 out.println(); 29 } 30 out.println(n-(r<=1?1:1+2*(r-1)*(r+1))); 31 out.flush(); 32 } 33 34 }
python:
import sys def total(x): return 1 + 2 * (x - 1) * (x + 1) def item(x): return 2 * x - 1 n, s = map(str, sys.stdin.readline().split()) n = int(n) l, r, mid = 1, 23, 0 while l < r: mid = (l + r + 1) // 2 if total(mid) <= n: l = mid else: r = mid - 1 i, num = r, item(r) while i > 0: print((num - item(i)) // 2 * ' ', end='') print(item(i) * s) i -= 1 i = 2 while i <= r: print((num - item(i)) // 2 * ' ', end='') print(item(i) * s) i += 1 print(n - total(r))
标签:1027,沙漏,int,打印,mid,while,static,new,out From: https://www.cnblogs.com/Ly-abu/p/17565247.html