我有一个任务输入一个奇数位数字并打印成沙漏形状。 例如,如果输入数字为3,则打印应该如下所示。
int[] lastNum = arr[arr.length - 1];
System.out.println(lastNum[0]);
我成功打印出这样的内容:
123321
2332
3
2332
123321
但在那之后我卡住了。这是我的代码
12321
12321
答案 0 :(得分:0)
这是你想要的吗?
#include <stdio.h>
unsigned int digits(unsigned int number)
{
unsigned int digits_counter = 0;
while (number)
{
number /= 10;
++digits_counter;
}
return digits_counter;
}
void printNumber(unsigned int minLevel, unsigned int number)
{
if(number > minLevel)
{
printf("%d");
}
else
{
printf("%*c", digits(number), ' ');
}
}
void hourglass_line(unsigned int minLevel, unsigned int level)
{
int number;
/// rising
for(number = 1; number <= level; ++number)
{
printNumber(minLevel, number);
}
// falling
for(number = level; number >= 1; --number)
{
printNumber(minLevel, number);
}
printf("\n");
}
void hourglass(unsigned int level)
{
int minLevel;
/// upper side and center
for (minLevel = 0; minLevel < level; ++minLevel)
{
hourglass_line(minLevel, level);
}
// lower side
for (minLevel = level-2; minLevel >= 0; --minLevel)
{
hourglass_line(minLevel, level);
}
}
int main()
{
unsigned int number;
printf("give me a number: \n");
scanf("%d", &number);
hourglass(number);
return 0;
}