当前位置: 代码迷 >> C语言 >> [求助]关于for循环的2个题目~字母菱形和数字统计
  详细解决方案

[求助]关于for循环的2个题目~字母菱形和数字统计

热度:231   发布时间:2008-04-09 23:51:16.0
第二题
#include<stdio.h>
#define X { char c;while(c=getchar()!='\n'&&c!=EOF);}
int hang()
{
        int n;
        do
        {
                system("cls");
                printf("请输入菱形的行数,请输入17以内正奇数:\n");
                scanf("%d",&n);
                X;
                if(n<3||n>17||n%2==0)
                {
                        printf("您的输入有误,请重新输入。");
                        sleep(1);
                }
        }
        while(n<3||n>17||n%2==0);
        return n;
}


int main(void)
{
        int i,j,k,n;
        char x[27]={'0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
        n=hang();
        puts(" ");
        for(i=0;i<=n/2+1;i++)
        {
                k=i;
                for(j=0;j<30-i;j++)
                        printf(" ");
                for(j=0;j<2*i-1;j++)
                {
                        printf("%c",x[k]);
                        k++;
                }
                puts(" ");
        }
        for(i=n/2;i>=0;i--)
        {
                k=i;
                for(j=0;j<30-i;j++)
                        printf(" ");
                for(j=0;j<2*i-1;j++)
                {
                        printf("%c",x[k]);
                        k++;
                }
                puts(" ");
        }


        getch();
}
----------------解决方案--------------------------------------------------------
  相关解决方案