当前位置: 代码迷 >> C语言 >> 解网上的一道题,可我代码太长,各位高手帮忙看看有无更好方法,谢
  详细解决方案

解网上的一道题,可我代码太长,各位高手帮忙看看有无更好方法,谢

热度:163   发布时间:2007-12-20 11:13:26.0
解网上的一道题,可我代码太长,各位高手帮忙看看有无更好方法,谢
原题如下:
3. 打印一个 N*N 的方阵,N为每边           N=15  打印出下面图形
字符的个数(3<N<20), 要求最               TTTTTTTTTTTTTTT
外一层为"T", 第二层为"J", 从第三层               TJJJJJJJJJJJJJT
起每层依次打印数字 1,2,3,...                     TJ11111111111JT
(右图以N为15为例)                           TJ12222222221JT
                                                  TJ12333333321JT
                                                  TJ12344444321JT
                                                  TJ12345554321JT
                                                  TJ12345654321JT
                                                  TJ12345554321JT
                                                  TJ12344444321JT
                                                  TJ12333333321JT
                                                  TJ12222222221JT
                                                  TJ11111111111JT
                                                  TJJJJJJJJJJJJJT
                                                  TTTTTTTTTTTTTTT


我的代码是这么写的,运行时一切正确,可是代码太长,怀疑有更好的方法,请指点新手一下,谢谢各位了。

#include <stdio.h>

main ()
{
    short i, j, n;
    printf("Please input a number(3<n<20):\n");
    do
    {
        scanf("%d",&n);
        if ( n<3 || n>21 )
        {
            printf("Please input a number(3<number<21):\n");
        }
    }
    while ( n<3 || n>21 );

    for ( i=0; i<n; i++ )
    {
        for ( j=0; j<n; j++ )
        {
            if ( i==0 || i==n-1 || j==0 || j==n-1 )
            {
                printf("N");
            }
            else if ( i==1 || i==n-2 || j==1 || j==n-2 )
            {
                printf("J");
            }
            else if ( i==2 || i==n-3 || j==2 || j==n-3 )
            {
                printf("1");
            }
            else if ( i==3 || i==n-4 || j==3 || j==n-4 )
            {
                printf("2");
            }
            else if ( i==4 || i==n-5 || j==4 || j==n-5 )
            {
                printf("3");
            }
            else if ( i==5 || i==n-6 || j==5 || j==n-6 )
            {
                printf("4");
            }
            else if ( i==6 || i==n-7 || j==6 || j==n-7 )
            {
                printf("5");
            }
            else if ( i==7 || i==n-8 || j==7 || j==n-8 )
            {
                printf("6");
            }
            else if ( i==8 || i==n-9 || j==8 || j==n-9 )
            {
                printf("7");
            }
            else if ( i==9 || i==n-10 || j==9 || j==n-10 )
            {
                printf("8");
            }
            else if ( i==10 || i==n-11 || j==10 || j==n-11 )
            {
                printf("9");
            }
        }
        printf("\n");
    }

}
搜索更多相关的解决方案: 代码  

----------------解决方案--------------------------------------------------------
前两行和最后两行可用FOR循环解决
中间数字也可以
----------------解决方案--------------------------------------------------------
试了一下才知道比想的罗嗦~!
#include <stdio.h>
#include <math.h>
main()
{
    int m,n=0,i,j,k;
   
    printf("Input N(3<N<20):");
    scanf("%D",&n);
   
    for(j=0;j<n;++j)printf("T");
    printf("\nTJ");
    for(j=0;j<n-4;++j)printf("J");
    printf("JT\n");
   
    m=(n-4)/2;
    for(i=-m;i<=m;++i)
    {
        printf("TJ");
        for(j=1;j<=m-abs(i);++j)printf("%d",j);
        if(i!=0)for(k=j*2;k<=(m)*2+m%2;++k)printf("%d",m-abs(i)+1);
        for(j=m-abs(i)+1;j>=1;--j)printf("%d",j);
        if(i==0&&n%2==0)for(k=0;k<n;++k)printf("\b");
        else
        printf("JT\n");
    }
   
    printf("TJ");
    for(j=0;j<n-4;++j)printf("J");
    printf("JT\n");
    for(j=0;j<n;++j)printf("T");
    return 0;
}
----------------解决方案--------------------------------------------------------
  相关解决方案