-7

ユーザーに高さの値を入力してもらう必要があります。23 より大きいか 0 より小さい場合は、もう一度尋ねる必要があります。次に、マリオブラザーズのゲームの終わりにある階段のような # の種類のピラミッドを出力する必要があります (右側に 2 つのハッシュタグがあります。例を参照してください)。問題は、コードが実行され、# が出力されず、コードが終了することです。これはオンライン コース用なので、コードだけを投稿しないでください。完全なコードを投稿する場合は、それを説明してください。

ピラミッドの例: https://www.dropbox.com/s/4fyg2ls0eml7asi/Screenshot%20from%202014-03-20%2002%3A15%3A08.png

私が従ったフローチャート: http://i.imgur.com/otuDOtK.png

#include <stdio.h>
//#include "cs50.h"

int main(void){
    //int h = 0;
    printf("Enter height between 0 and 23: ");
    int h = scanf("%d", &h);
    while (h < 0 || h > 23){
        printf("Retry: ");
        h = scanf("%d", &h);
    }
    int l = 1;
    if (h == 0){
        //printf("ln21\n"); //Debug
        return;
    }
    int s = h - 1;
    int b = l + 1;
    if (s == 0){
        if (b == 0){
            printf("\n");
    h = h - 1;
    l = l + 1;
    if (h == 0){
        return;
    }
        }
    }
    else{
    printf("#");
    b = b - 1;
    }
}
4

1 に答える 1

0
#include <stdio.h>

int main(){
    int height = 8;//Number of repetitions of basic.
    int max_width = height + 1;//Number of block(#) in the largest display
    int i, j, w;
    for(i = 0; i < height; ++i){
        printf("%*s", max_width -(i+2), "");//Print a space in front of the block
        for(w = 0; w < i+2; ++w){
            printf("#");//display a block of each floor
        }
        printf("\n");
    }
    printf("\n");
    return 0;
}

#include <stdio.h>
#include <string.h>

int main(){
    int height = 8;
    int max_width = height + 1;
    char wall[max_width + 1];
    int i;
    memset(wall, '#', sizeof(wall)-1);// make a block of maximum.
    wall[sizeof(wall)-1] = '\0';
    for(i = 0; i < height; ++i){
        printf("%*.*s\n", max_width, i+2, wall);//View of block size and the required blank
    }
    return 0;
}
于 2014-03-20T04:02:01.387 に答える