我需要从用户读取整数值,然后按照在垂直图中用“#”输入的顺序组织它们。 一切正常,除了三件事:
3。有时候,例如7和8都输出相同数量的#s
我感谢那些花时间帮助我的人,我对Coding还是陌生的。预先谢谢你。
所需的示例I / O:
15
16
15
12
12
12
8
6
3
2
19
21
17
15
12
11
10
9
8
7
7
Output:
#
#
##
##
###
# ###
### ####
### ####
### ####
###### #####
###### ######
###### #######
###### ########
####### #########
####### ###########
######## ###########
######### ###########
######### ###########
########## ###########
######################
######################
我的代码的示例输入/输出:
1
2
3
4
5
6
7
8
8
9
###########
####
#####
######
#######
########
#########
##########
###########
我的代码:
#include <stdio.h>
int main () {
int input, num, max=0, nums=0;
int numbers[80];
while (input != EOF) {
input = scanf("%d", &num);
if (num > max) {
max=num;}
numbers[nums] = num;
nums++;
}
int histogram[nums][max];
for (int i = 0; i < nums; i++){
if (numbers[i] != 0){
for (int j=0; j <= numbers[i]; j++) {
histogram[i][j] = 1;
}
}
}
for (int j = max; j > 0 ; j--){
for (int i = 0; i < nums; i++) {
if (histogram[i][j]==1) {
printf("#");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
答案 0 :(得分:0)
首先,在分配输入之前检查输入是否为EOF,之后再输入。一种快速/肮脏的方法是:
while ((input = scanf("%d", &num)) != EOF) {
...
}
第二,您的j
循环不匹配。我建议使它们都从width-1变为0,如下所示:
for (int j=0; j < numbers[i]; j++) {
for (int j = max-1; j >= 0 ; j--){