如何使由此代码形成的金字塔(CS50 Mario程序)正确对齐? [英] How to make the pyramid (CS50 Mario Program) formed by this code to be right aligned?
本文介绍了如何使由此代码形成的金字塔(CS50 Mario程序)正确对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
请帮助我创建使用高度对齐的哈希和空格正确打印高度为 n的金字塔。我已经在下面发布了代码本身。该程序会正确要求用户输入,但不会建立右对齐的金字塔。如果有人可以解决此问题,请提供帮助。
Please help me create the pyramid with height "n" print correctly using hashes and spaces that is right-aligned. I have posted the code itself below. The program correctly asks for the user input, but doesn't build the pyramid right-aligned. If anyone can fix this, please help.
#include <stdio.h>
#include <cs50.h>
int main(void)
{
int i=0;
int n=0;
do
{
printf("Height:");
n=GetInt();
} while (n<0 || n>23);
for (i=0; i<n; i++)
{
printf(" ");
for (int x=0; x<i+2; x++)
{
printf("#");
}
printf("\n");
}
return 0;
}
推荐答案
#include<cs50.h>
#include<stdio.h>
#include<stdio.h>
void half_pyramid(int n);
int main(void){
int height;
do{
height = get_int("Height: ");
}while(height<=0||height>23);
half_pyramid(height);
}
void half_pyramid(int n)
{
int spaces;
int dashes;
for(int i = 2; i<=n+1;i++){
for(spaces = (n-i); spaces>=0;spaces--){
printf(" ");
}
for (dashes = 1; dashes <= i; dashes++){
printf("#");
}
printf("\n");
}
}
这篇关于如何使由此代码形成的金字塔(CS50 Mario程序)正确对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文