复制 #include <stdio.h> void josephus(int n, int p) { int people[n]; for (int i = 0; i < n; i++) { people[i] = i + 1; } int count = 0; int index = 0; int countOut = 0; while (countOut < n) { if (people[index] != 0) { count++; if (count == p) { printf("%d ", people[index]); people[index] = 0; countOut++; count = 0; } } index = (index + 1) % n; } printf("\n"); } int main() { int n, p; scanf("%d %d", &n, &p); josephus(n, p); return 0; }请把这段代码修改为输出时行尾无空格
时间: 2024-02-14 11:10:35 浏览: 55
修改后的代码如下:
```c
#include <stdio.h>
void josephus(int n, int p) {
int people[n];
for (int i = 0; i < n; i++) {
people[i] = i + 1;
}
int count = 0;
int index = 0;
int countOut = 0;
while (countOut < n) {
if (people[index] != 0) {
count++;
if (count == p) {
printf("%d", people[index]);
people[index] = 0;
countOut++;
count = 0;
if (countOut != n) {
printf(" ");
}
}
}
index = (index + 1) % n;
}
printf("\n");
}
int main() {
int n, p;
scanf("%d %d", &n, &p);
josephus(n, p);
return 0;
}
```
主要的修改在 `josephus` 函数中的输出部分,当输出一个数时,先判断是不是最后一个数,如果不是,再输出一个空格。这样就能保证行尾无空格。
阅读全文