使用简单的代码在Java或Python上创建一个小时玻璃图案?

2024-06-28 14:57:09 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我想知道,有没有简单的代码可以使用Java或Python来生成一个带有奇数或偶数输入的小时玻璃图案?因为我的代码并不简单(我使用的是Python)。在

输出示例如下:

Expected Output

然后,我的代码是:

def evenGlassHour(target):
 jsp=1
 jtop=target
 jbot=2
 jbotspace=int(target/2)
 eventarget=int(target/2)
 temp=""
 for i in range(eventarget):
     for j in range(i):
         temp+=" "
     for jsp in range(jtop):
         temp+="@"
     jtop-=2
     temp+="\n"
 for i in range(eventarget-1):
     for j in range(jbotspace-2):
         temp+=" "
     for j in range(jbot+2):
         temp+="@"
     jbot+=2
     jbotspace-=1
     temp+="\n"

 print(temp)

def oddGlassHour(target):
 jsp=1
 jtop=target
 jbot=1
 jbotspace=int(target/2)
 oddtarget=int(target/2)
 temp=""
 for i in range(oddtarget):
     for j in range(i):
         temp+=" "
     for jsp in range(jtop):
         temp+="@"
     jtop-=2
     temp+="\n"
 for i in range(oddtarget+1):
     for j in range(jbotspace):
         temp+=" "
     for j in range(jbot):
         temp+="@"
     jbot+=2
     jbotspace-=1
     temp+="\n"

 print(temp)

target=int(input("Input : "))

if(target%2==0):
 evenGlassHour(target)
else:
 oddGlassHour(target)

这是我代码的结果:

^{pr2}$

Tags: 代码intargetfordefrangetempint
3条回答

使用中心调整字符串格式

灵感:https://stackoverflow.com/a/44781576

def render(size):
    char = "*"
    #build a center-justified format mask
    mask = '{:^%ds}' % (size)

    print("size:%s:\n" % (size))

    #count down your shrinking
    for i in range(size, 0, -2):
        print(mask.format(char * i))

    #trickier:  you've already printed the narrowest
    #and your next line is different depending on odd/even input 
    if size % 2:
        start = 3
    else:
        start = 4

    for i in range(start, size+1, 2):
        print(mask.format(char * i))
    print()


render(3)
render(5)
render(12)

输出:

^{pr2}$

可以对str.zfill和递归使用字符串格式:

def _glass(_input, _original, flag=True):
  if _input in {1, 2}:
    return ('00' if _input == 2 else '0').center(_original) if flag else ''
  if flag:
    return ('0'*(_input)).center(_original)+'\n'+_glass(_input-2, _original, flag=flag)
  return _glass(_input-2, _original, flag=flag)+'\n'+('0'*(_input)).center(_original)

def print_glasses(_input):
  print(_glass(_input, _input)+_glass(_input, _input, False))

^{pr2}$

输出:

000
 0 
000
--------------------
0000
 00 
0000
--------------------
00000
 000 
  0  
 000 
00000
--------------------
000000
 0000 
  00  
 0000 
000000
--------------------
0000000
 00000 
  000  
   0   
  000  
 00000 
0000000
--------------------

在java中,您可以编写如下内容:

public static void printPattern(int size) {
    int n = size; 
    boolean upper = true;
    for(int i = 0; size%2 == 0? i< size-1 : i<size; i++){            
        String str = String.join("", Collections.nCopies(n, "@"));
        String pad = String.join("", Collections.nCopies((size-n)/2 , " "));
        System.out.println(pad+str+pad);
        if(n-2>0 && upper){
            n-=2;
        }
        else {
            n+=2;
            upper = false;
        }           
    }
}

相关问题 更多 >