问题
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;
}
回答1:
Inside your main loop you have:
// Instruction for printing 1 space
// Sub loop for printing x+1 number of hashes
// Instruction for printing a new line
So each time it loops round it's correctly drawing the hashes and a new line but your not telling it to draw any more than 1 space each time as unlike the subloop for the hashes the instruction doesn't increase with each pass.
回答2:
int main(void){
int height;
int spaces;
int dashes;
do
{
printf("Height:");
height = GetInt();
}
while (height <= 0 || height >= 23);
//loop for the rows
for (int i = 1; i <= height; i++){ //fixed the <= part of the operator
//loop for the spaces and dashes per row
for (spaces = (height - i); spaces >= 0; spaces--){
printf(" ");
}
for (dashes = 1; dashes <= (i + 1); dashes++){
printf("#");
}
printf("\n");
}
return 0;
}
回答3:
#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");
}
}
回答4:
#include<stdio.h>
#include<cs50.h>
int main (void)
{
int height;
int c = 0;
do
{
printf("Height?\n");
height = GetInt();
}while(height < 0 || height > 23);
for(int a = height; a > 0; a--)
{
for(int b = 0; b < a - 1; b++)
{
printf(" ");
}
printf("#");
c++;
for (int d = 0; d < c; d++)
{
printf("#");
}
printf("\n");
}
}
回答5:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
int spaces;
int hash;
do
{
height = get_int("Height: ");
}
while (height < 0 || height > 23);
for (int i = 0; i < height; i++)
{
// For loop to print out the spaces
for (spaces = (height - i); spaces >= 2; spaces--)
{
printf(" ");
}
// For loop to print out hashes
for (hash = 0; hash <= (i + 1); hash++)
{
printf("#");
}
printf("\n");
}
}
回答6:
Here is how I solved the version of this problem with both sides, but you can make it work with only one, just use 2 for-loops instead of three.
If we have to take a number 1 – 8 as an input which is the height of our pyramid, we can use one for loop to print out each row of the pyramid.
Inside this loop, we will need another two for loops that print spaces and hashes.
And since we need the other side of the pyramid as well, we will use three loops in total within our main loop.
Why three loops and not four? Because we don’t actually need to print space on the right side of the pyramid.
Beside the height variable we need another one that will work as a counter, since we cannot manipulate our height variable.
These for loops work in opposite direction, i.e. the more spaces we have, less hashes we print and vice versa.
So, the final CS50 Pset 1 Mario More solution looks exactly like this:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int height;
do {
height = get_int("Height: ");
}
while (height < 1 || height > 8);
if (height > 0 || height < 9) {
int counter = 0;
for (int row=0; row<height; row++) {
if (counter != height) {
for (int spaces = (height-1) - counter; spaces > 0; spaces--) {
printf(" ");
}
for (int hashes = 0; hashes <= counter; hashes++) {
printf("#");
}
//ignore this block below if you want only one side
printf(" ");
for (int hashes = 0; hashes <= counter; hashes++) {
printf("#");
}
//##################################################
printf("\n");
counter++;
}
}
}
}
I actually made a blog post about this since I like to keep notes in case that you need more information.
回答7:
#include <stdio.h>
#include <cs50.h>
int main(void){
int s=0;
int hash=0;
int ht;
int h;
do{
printf("Height: ");
ht=GetInt();
for (h = ht; h > 0 ; h--){
printf("\n");
hash = ht - (h - 1);
s = ht - hash;
int i;
for (i = 0; i <= s; i++){
printf(" ");
}
for (i = 0; i <= hash; i++){
printf("#");
}
printf(" ");
for (i = 0; i <= hash; i++){
printf("#");
}
}
return 0;
}
while(ht > 0 || ht < 23);
return 0;
}
回答8:
//this is how you only make the right aligned pyramid
#include <stdio.h>
#include <cs50.h>
int main(void){
int s=0;
int hash=0;
int ht;
int h;
do{
printf("Height: ");
ht=GetInt();
for (h = ht; h > 0 ; h--){
printf("\n");
hash = ht - (h - 1);
s = ht - hash;
int i;
for (i = 0; i <= s; i++){
printf(" ");
}
for (i = 0; i <= hash; i++){
printf("#");
}
}
return 0;
}
while(ht > 0 || ht < 23);
return 0;
}
回答9:
//Author: Andriyevski Serhiy
#include <stdio.h>
#include <cs50.h>
int main(void)
{
// declaring my variables
int height;
int all_row;
int space;
int hash;
// prompts user for integer until integer is 0 to 23
do
{
printf("Please choose a number from 0 to 23:");
height = GetInt();
}
while ((height < 0) || (height > 23));
// this is the loop that will create the number of rows in the pyramid entered by the user
for (all_row = 1; all_row <= height; all_row++)
{
for (space = (height - all_row); space > 0; space--)
{
printf(" ");
}
for (hash = 1; hash <= (all_row + 1); hash++)
{
printf("#");
}
printf("\n");
}
return 0;
}
来源:https://stackoverflow.com/questions/32303244/how-to-make-the-pyramid-cs50-mario-program-formed-by-this-code-to-be-right-ali