C语言循环给数组赋值方法及约瑟夫环程序异常问题咨询
Hey Brandon, let's break down your problems and fix them step by step!
It sounds like your code only runs the elimination logic once (modifying n[0]) then exits early—this usually stems from incorrect loop conditions or flawed counting/indexing logic. Let's walk through a working implementation of the Josephus problem for 10 people (eliminating every 3rd person) so you can compare and spot where your code went wrong.
Here's a tested example:
#include <stdio.h> #define TOTAL_PEOPLE 10 #define ELIMINATE_EVERY 3 int main() { // 1 = alive, 0 = eliminated int survivors[TOTAL_PEOPLE]; int alive_count = TOTAL_PEOPLE; int current_index = 0; int count = 0; // Initialize all people as alive for (int i = 0; i < TOTAL_PEOPLE; i++) { survivors[i] = 1; } // Loop until only one person remains while (alive_count > 1) { // Only count if the current person is alive if (survivors[current_index] == 1) { count++; // Eliminate when we hit the 3rd person if (count == ELIMINATE_EVERY) { survivors[current_index] = 0; alive_count--; count = 0; // Reset count for next round printf("Eliminated person #%d\n", current_index + 1); // Use 1-based numbering } } // Move to next person, wrap around with modulo current_index = (current_index + 1) % TOTAL_PEOPLE; } // Find and print the last survivor for (int i = 0; i < TOTAL_PEOPLE; i++) { if (survivors[i] == 1) { printf("Last remaining person is #%d\n", i + 1); break; } } return 0; }
Common mistakes that cause early exits:
- Using an
ifstatement instead of awhileloop to repeat elimination rounds - Forgetting to update the alive person count, so your loop condition never stays true
- Failing to wrap the index with modulo (
%), so you stop after the first element instead of looping back to the start - Not resetting the count after eliminating someone, leading to incorrect counting in the next round
There are a few flexible ways to assign values to array elements in loops, depending on your needs:
a. Basic Loop Assignment (Works for All Data Types)
This is the most versatile method—you can set any value, even conditional ones:
int numbers[5]; // Set all elements to 7 for (int i = 0; i < 5; i++) { numbers[i] = 7; } // Or set alternating values for (int i = 0; i < 5; i++) { numbers[i] = (i % 2 == 0) ? 10 : 20; }
b. Using memset (For Byte-Level Assignments)
If you're working with character arrays or need to set all elements to 0, memset (from <string.h>) is efficient. Note: It assigns values per byte, so it's not suitable for non-zero integers (an int is 4 bytes on most systems, so setting each byte to 1 would result in 0x01010101 instead of 1):
#include <string.h> char letters[10]; memset(letters, 'x', sizeof(letters)); // Sets all chars to 'x' int zeros[8]; memset(zeros, 0, sizeof(zeros)); // Correctly sets all ints to 0
c. Targeting Specific Indices
If you only need to modify certain positions in an array, you can target them directly:
int scores[10] = {0}; // Initialize all to 0 first // Set even indices to 100 for (int i = 0; i < 10; i += 2) { scores[i] = 100; }
内容的提问来源于stack exchange,提问作者Brandon

