如果您输入偶数,则只会打印奇数,直到它达到 0。(不会打印 0)。例如,如果您输入 10,则输出将为 9、7、5、3、1。
这是我想出来的。我想知道我应该将 x 递减多少以获得所需的输出。
int x;
scanf("%d", &x);
while (x >= 0) {
printf("%d", x);
x = x - 2;
}
最佳答案
I'm wondering what should I decrement x by to get the desired output.
减 2 没问题,只要您始终从奇数开始。所以你可以把循环改成这样:
for ( int i = x % 2 ? x : x - 1; // Is x odd? good.
// Otherwise, start from the previous one.
i > 0;
i -= 2 ) {
printf("%d\n", i);
}
https://stackoverflow.com/questions/66383364/