我的任务是打印出一个三角形数字的数字,直到金字塔形状的最终值,但我一生都无法让它均匀分布和居中,而且我不能只使用 %6i
或其他东西,因为它必须适用于我们输入的任何数字。我尝试过合理化,使用单独的变量根据用户定义的数字计算间距,但都不起作用。
我们的结果应如下图所示,以 15 为例。
截止日期为午夜,我们已经为此准备了好几天。我的代码有点长:
//Include the usual libraries
#include <stdio.h>
//Declare the main function
int main (void)
{
//Declare variables
int space, n, triNum, ast, num, triDisplay = 0, number = 1;
//Welcome the user to your program
printf("Welcome to Phase 1!\nThis program will display a triangular number of your choosing in 'graphical' format.\n");
//Prompt the user for the triangular number
printf("Enter the triangular number: ");
scanf("%i", &triNum);
//This loop keeps track of how many times the nested loops have run.
n = 1;
while(n<=triNum)
{
//This loop prints the spaces before the asterisks, and decreases the amount printed after each line.
space=n;
while(space>0, space<triNum)
{
printf(" ");
space++;
}//End of second loop
//This loop prints the asterisks and increases the amount printed by one for each line.
ast=n;
while(ast>0)
{
printf("* ");
ast--;
}//End of third loop
printf("\n") ;
n++;
}//End of first loop
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//This loop is identical to the first loop, but is used to display a different set of loops
n = 1;
while(n<=triNum)
{
space=n;
while(space>0, space<triNum)
{
printf(" ");
space++;
}
//This loop prints the number associated with each asterisk in triangle format
num = 1;
while(num>0, num<=n)
{
printf("%i ",number);
number++;
num++;
}
n++;
printf("\n");
}
//This loop calculates the actual value of the triangular number
n=1;
while(n<=triNum)
{
triDisplay += n;
n++;
}
printf("\nThe triangular number of %i is %i.\n", triNum, triDisplay);
//Thank the user for using your program
printf("Thank you for using this program! Have a nice day.\n");
//End function
return 0;
}
这就是我们目前的样子。
最佳答案
看起来你非常接近,你失败的原因似乎是你的数字之间总是有一个空格。第一张图片的数字之间有多个空格,弄清楚如何根据数字中的位数计算空格数,您就应该拥有它。
问题规范是否说明应包含多少个空格,或者金字塔的最终对齐方式到底是什么?
这非常接近您所需要的,唯一的区别是每行的开头有一个额外的 2 个空格,因为从底行开始的 3 位数字仍然使用 5 个空格。因此,您可能需要弄清楚如何为每行中的第一个数字精确使用 x
位,其中 x
等于底行中第一个数字的位数,本例中为 3。
while (n <= triNum)
{
space = n * 3;
while (space>0, space<(triNum * 3))
{
printf(" ");
space++;
}
//This loop prints the number associated with each asterisk in triangle format
num = 1;
while (num>0, num <= n)
{
printf("%5i ", number);
number++;
num++;
}
n++;
printf("\n");
}
输出:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
让我解释一下我改变了什么。我建议您在更新代码时遵循,以便您理解并可以修复最后一点并确保它适用于其他数字。
我注意到的第一件事是你一开始就没有获得足够的空间。鉴于每个数字最多为 3 位数字,我认为您应该添加 3 个空格而不是 1 个,所以我更改了
while (space>0, space<triNum)
至
while (space>0, space<(triNum * 3))
这看起来好一点,但是现在底行的空格太多了,所以我想当我们调整空格数时我们也应该减去 3 而不是 1,所以我改变了
space = n;
至
space = n * 3;
这看起来确实是前面的空格数是正确的,但事情似乎仍然没有对齐。所以看看你正确的金字塔,我发现每个数字似乎都占据了6个空格,所以我改变了
printf("%i ", number);
至
printf("%5i ", number);
这就是现在的情况。它适用于我放入的许多不同示例,但绝不是详尽无遗。我认为只要你最终打印的数字不超过 5 位就可以了。如果空格的数量确实不重要(即必须最少并且仍然具有平衡的行),那么您可以将 printf()
中的宽度增加到可以容纳您必须的最大数量的值容纳。
关于c - 如何使这个 C 三角形数字生成器的数字居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49248207/