c语言编程练习题:7-82 打印沙漏
作者:yunjinqi   类别:    日期:2023-05-31 09:56:27    阅读:461 次   消耗积分:0 分    

image.png

#include <stdio.h>
int main(){
    int N,i,j;
    char s ;
    int num_s;
    if (scanf("%d %c",&N,&s)!=EOF && N<=1000){
        if (N>=5){
            int num=0; 
            int count=1;
            int value=1;
            while (value<N){
                num+=1;
                count+=2;
                value+=2*count;
            }
            value = value - 2*count;
            count = count -2;
            // printf("num is %d\n",num);
            // printf("count is %d\n",count);
            // printf("value is %d\n",value);
            // 开始打印正面
            for (i=0;i<num;i++){
                // 打印空白
                // printf("up i value is %d",i);
                num_s = count-2*i;
                for (j=0;j<i;j++){
                    printf(" ");
                }
                // 打印字符
                for (j=0;j<num_s;j++){
                    printf("%c",s);
                }
                printf("\n");
                
            }
            // 开始打印反面
            for (i=1;i<num;i++){
                // 打印空白
                num_s = 2*i+1;
                int t = num-i-1;
                for (j=0;j<t;j++){
                    printf(" ");
                }
                // 打印字符
                for (j=0;j<num_s;j++){
                    printf("%c",s);
                }
                printf("\n");
            }
            printf("%d",N-value);
        }else{
            printf("%c\n",s);
            printf("%d",N-1);
        }

        
    }else{
        printf("input wrong");
    }
    return 0;
}


版权所有,转载本站文章请注明出处:云子量化, http://www.woniunote.com/article/207
上一篇:c语言编程练习题:7-81 2018我们要赢
下一篇:c语言编程练习题:7-83 币值转换