标题:[求助]关于for循环的2个题目~字母菱形和数字统计
只看楼主
广陵绝唱
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:29
帖 子:3607
专家分:1709
注 册:2008-2-15
得分: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();
}
2008-04-09 23:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-207210-1-1.html




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

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