方法私聊讲解
解决代码如下:
JAVA版:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int m=1+(n-1)*2;
int a=1+(n-1)*4;
String s[]=new String [m];
for(int i=0;i<m;i++){
s[i]="";
}
int t=1;
int v=1;
int k=0;
for(int i=0;i<m;i++){
v=1;
if(i==0){
for(int j=0;j<m;j++){
s[i]+="@";
}
}
else{
for(int j=0;j<=k;j++){
//if(i==2)System.out.println(v);
if(v==1){
s[i]+="@";
v*=-1;
}
else{
s[i]+=" ";
v*=-1;
}
}
if(t==1){
for(int j=k+1;j<m;j++){
s[i]+=" ";
}
t*=-1;
}
else{
for(int j=k+1;j<m;j++){
s[i]+="@";
}
t*=-1;
}
if(t==-1){
k+=2;
}
}
}
for(int i=0;i<m;i++){
System.out.print(s[i]);
StringBuffer ss= new StringBuffer(s[i]);
ss.reverse();
System.out.println(ss.substring(1,ss.length()));
}
for(int i=m-2;i>=0;i--){
System.out.print(s[i]);
StringBuffer ss= new StringBuffer(s[i]);
ss.reverse();
System.out.println(ss.substring(1,ss.length()));
}
}
}
Python版:
def main():
n = int(input())
m = 1 + (n - 1) * 2
a = 1 + (n - 1) * 4
s = [""] * m
t = 1
v = 1
k = 0
for i in range(m):
v = 1
if i == 0:
s[i] = "@" * m
else:
for j in range(k + 1):
if v == 1:
s[i] += "@"
v *= -1
else:
s[i] += " "
v *= -1
if t == 1:
s[i] += " " * (m - k - 1)
t *= -1
else:
s[i] += "@" * (m - k - 1)
t *= -1
if t == -1:
k += 2
for i in range(m):
print(s[i] + s[i][::-1][1:])
for i in range(m - 2, -1, -1):
print(s[i] + s[i][::-1][1:])
if __name__ == "__main__":
main()