[英]Why does printf print the number of spaces I assigned to the int space?
#include <stdio.h>
#include <cs50.h>
int
main(void)
{
int height;
do {
height = get_int("Size: \n");
} while (height < 1 || height > 50);
for (int row = 1; row < height + 1; row++) {
for (int space = height - row; space > 0; space--) {
printf(" ");
}
}
return 0;
}
為什么我的代碼有效? 我正在做cs50的馬里奧的pset1。 我不明白為什么printf(" ")
准確地打印出int space = height-row
中的空格數——如果我沒有指定它, printf
如何知道打印存儲在int
space 中的數字?
printf()
不知道space
的值。 在這種情況下, for (int space = height - row; ... )
循環只是運行printf()
語句height-row
次,並且每次它運行時都恰好打印一個空格。
這是我的最高評論的序言。
printf
不“知道”要打印多少空格。 你的printf(" ");
將打印一個空格。 但是,它在循環中被調用。 所以,空格的個數就是循環迭代的次數。
這是您的程序的重構版本,其中顯示了各種height
值和它們打印的空格數。 我已將" "
替換為"#"
以便可以看到它們。
另外,我相信你想要printf("\n");
內循環結束后。 這允許它生成像 output 這樣的金字塔。
無論如何,這是代碼:
#include <stdio.h>
//#include <cs50.h>
int
main(void)
{
int height;
#if 0
do {
printf("Size:\n");
scanf("%d",&height);
//height = get_int("Size: \n");
} while (height < 1 || height > 50);
#endif
for (height = 0; height <= 20; ++height) {
if (height > 0)
printf("\n");
printf("height: %d\n",height);
int count = 0;
for (int row = 1; row < height + 1; row++) {
for (int space = height - row; space > 0; space--) {
#if 1
printf("#");
#endif
++count;
}
printf("\n");
}
printf("count: %d\n",count);
}
return 0;
}
這是程序的 output:
請注意,程序總是在末尾打印一個空行。 這是因為您進行的一次迭代太過分了。
height: 0
count: 0
height: 1
count: 0
height: 2
#
count: 1
height: 3
##
#
count: 3
height: 4
###
##
#
count: 6
height: 5
####
###
##
#
count: 10
height: 6
#####
####
###
##
#
count: 15
height: 7
######
#####
####
###
##
#
count: 21
height: 8
#######
######
#####
####
###
##
#
count: 28
height: 9
########
#######
######
#####
####
###
##
#
count: 36
height: 10
#########
########
#######
######
#####
####
###
##
#
count: 45
height: 11
##########
#########
########
#######
######
#####
####
###
##
#
count: 55
height: 12
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 66
height: 13
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 78
height: 14
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 91
height: 15
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 105
height: 16
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 120
height: 17
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 136
height: 18
#################
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 153
height: 19
##################
#################
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 171
height: 20
###################
##################
#################
################
###############
##############
#############
############
###########
##########
#########
########
#######
######
#####
####
###
##
#
count: 190
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.