| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 634 人关注过本帖
标题:解网上的一道题,可我代码太长,各位高手帮忙看看有无更好方法,谢
只看楼主 加入收藏
kinglon
Rank: 1
等 级:新手上路
帖 子:44
专家分:0
注 册:2007-9-25
收藏
 问题点数:0 回复次数:2 
解网上的一道题,可我代码太长,各位高手帮忙看看有无更好方法,谢
原题如下:
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");
    }

}
搜索更多相关主题的帖子: 代码 
2007-12-20 11:13
万兽无缰
Rank: 1
等 级:新手上路
威 望:1
帖 子:296
专家分:0
注 册:2007-8-27
收藏
得分:0 
前两行和最后两行可用FOR循环解决
中间数字也可以

女朋友问我想怎么死~~~
             我说我想"爽死"
2007-12-20 11:26
freeforever
Rank: 4
等 级:业余侠客
威 望:3
帖 子:368
专家分:201
注 册:2005-11-2
收藏
得分:0 
试了一下才知道比想的罗嗦~!
#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;
}

其实我也很无聊!
2007-12-20 22:00
快速回复:解网上的一道题,可我代码太长,各位高手帮忙看看有无更好方法,谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.032078 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved