C Example: Josephus Problem: counting eliminations, array shifts and survivor

🗓 Last updated: 2026-11-30👤 Written by: Ivy✓ Reviewed by: MOTO🏷 Category: Algorithms

Simulates n people in a circle with an array: each round eliminates the k-th person, shifts later elements forward, and wraps the index modulo the remaining count; repeats until one remains. Solves (10,3), (41,3) and (7,2), printing elimination order and survivor.

⚡ Intermediate Reference entries: kw-intfn-printfkw-static
🖥 Run OnlineGCC 10.2 · C11 standard · click ▶ to run · Copy code
🖨 Reference OutputMatches the online run in a standard C11 environment
C · GCC 10.2 · -std=c11
n=10 k=3 eliminated: 3 6 9 2 7 1 8 5 10
survivor: 4 (after 9 eliminations)

n=41 k=3 eliminated: 3 6 9 12 15 18 21 24 27 30 33 36 39 1 5 10 14 19 23 28 32 37 41 7 13 20 26 34 40 8 17 29 38 11 25 2 22 4 35 16
survivor: 31 (after 40 eliminations)

n=7 k=2 eliminated: 2 4 6 1 5 3
survivor: 7 (after 6 eliminations)
🧩 Keep Learning

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏