当前位置: 代码迷 >> C语言 >> 打印菱形问题代码整理(五种解法)
  详细解决方案

打印菱形问题代码整理(五种解法)

热度:149   发布时间:2008-04-20 18:28:14.0
感谢LZ的收集啊,大家一起写出来的居然有这么多思路,不简单啊
----------------解决方案--------------------------------------------------------
真的很长见识,都很有创意的。尤其是宏定义
----------------解决方案--------------------------------------------------------
闲着无聊俺也写了一个:
程序代码:

#include <stdio.h>
#define  ABS(x)   ((x)>0?(x):(-(x)))
#define  N  10
int main()
{
    int i,j;

    for(i=0;i<2*N-1;i++)
    {
        j=ABS(N-i-1);
        printf("%*c%-*c\b*\n",j+1,' ',2*(N-j)-1,'*');
    }
    return 0;
}



[[it] 本帖最后由 VxWorks 于 2008-4-20 20:30 编辑 [/it]]
----------------解决方案--------------------------------------------------------
赞楼上

[color=white]
----------------解决方案--------------------------------------------------------
偶也玩一个

main(o,O,Q){o==1&&(o=(O=scanf("%d",&Q)-1)+2,Q+=2),printf("%*c%*c\n",Q-o+2,42,2*o-4,o-2?42:0),(O=o==Q-1?1:O)?main(o-1,O,Q):main(o+1,O,Q);}

请用TC
----------------解决方案--------------------------------------------------------
玩递归main有意思吗?不过是隐藏for循环中的显式迭代把程序变难看而已.
程序代码:

#include <stdio.h>
#define  ABS(x)   ((x)>0?(x):(-(x)))
#define  N  10
int main(int i)
{return i<2*N? printf("%*c%-*c\b*\n",ABS(N-i)+1,' ',2*(N-ABS(N-i))-1,'*'),main(++i): 0;}

----------------解决方案--------------------------------------------------------
我是菜菜,看不懂。。。。看书去
----------------解决方案--------------------------------------------------------
可以输入随意数的代码:(vc++6.0编译通过)
#include<stdio.h>
int main(void)
{
    int i, j, number;

    printf("Input your number: ");
    scanf("%i", &number);

    for(i = 0; i < (2*number-1); ++i)
    {

        if( i < number -1)
        {
            for(j = 0; j < (2*number-1); ++j)
            {
                if( j == number-1 - i  || j == number-1 + i )
                    printf("*");
                else
                    printf(" ");
            }
            printf("\n");
        }
        else if( i == number -1)
        {
            for( j = 0; j < (2*number-1); ++j)
            {
                if( j == number-1-i  || j == number-1 +i )
                    printf("*");
                else
                    printf(" ");
            }
            printf("\n");
        }
        else
        {
            for( j = 0; j < (2*number-1); ++j)
            {
                if( ((i - number) == (j-1)) || ((i - number) == ( 2*number-3 - j)) )
                    printf("*");
                else
                    printf(" ");
            }
            printf("\n");
        }

    }

    return 0;
}
----------------解决方案--------------------------------------------------------
楼上要继续加强学习

[color=white]
----------------解决方案--------------------------------------------------------
???
----------------解决方案--------------------------------------------------------
  相关解决方案