解题思路:
没想到什么简便的方法,那就只能暴力解题。
注意事项:
从 X 1 推到 X Y。
按行来输出,设置一个d=0,没输出一次 d++ 用 d==a 来判断是否结束
用 递归的思想 实现 整体输出。
虽然代码有点长。但很好理解。
参考代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int N=sc.nextInt();
for(int i=0;i<N;i++)
{
int a=sc.nextInt();
int b=sc.nextInt();
int m=0,n=a-2;
int d=0;
if(b==1)
f(a,m,n,d);
else
{
boolean s=false;
for(int j=0;j<b;j++)
{
if(!s)
{
m=0;n=a-2;
d=0;
f(a,m,n,d);
s=true;
}
else
{
m=0;n=a-3;
d=0;
f(a-2,m+1,n-1,d);
s=false;
}
}
}
if(b%2==0)
{
System.out.print("X");
for(int j=0;j<a-2;j++)
System.out.print(" ");
System.out.println("X");
}
System.out.println();
}
}
public static void f(int a,int m,int n,int d)
{
d++;
if(d<(a/2+1))
{
for(int i=0;i<m;i++)
System.out.print(" ");
System.out.print("X");
for(int i=0;i<n;i++)
System.out.print(" ");
System.out.print("X");
System.out.println();
m++;
n=n-2;
if(d!=a)
f(a,m,n,d);
}
else if(d==(a/2+1))
{
for(int i=0;i<m;i++)
System.out.print(" ");
System.out.print("X");
System.out.println();
m--;
n=n+2;
if(d!=a)
f(a,m,n,d);
}
else
{
for(int i=0;i<m;i++)
System.out.print(" ");
System.out.print("X");
for(int i=0;i<n;i++)
System.out.print(" ");
System.out.print("X");
System.out.println();
m--;
n=n+2;
if(d!=a)
f(a,m,n,d);
}
}
}
0.0分
0 人评分
【亲和数】 (C语言代码)浏览:492 |
C二级辅导-阶乘数列 (C语言代码)浏览:688 |
1013题解浏览:553 |
星期判断机 (C语言代码)浏览:859 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:556 |
C语言程序设计教程(第三版)课后习题10.1 (C语言代码)浏览:530 |
C二级辅导-同因查找 (C语言代码)浏览:563 |
C语言程序设计教程(第三版)课后习题12.5 (C语言代码)浏览:762 |
众数问题 (C语言代码)浏览:668 |
生日日数 (C语言代码)浏览:1498 |