C 语言实例 - 约瑟夫生者死者小游戏

C 语言教程 · 2019-02-21 11:56:47

30 个人在一条船上,超载,需要 15 人下船。

于是人们排成一队,排队的位置即为他们的编号。

报数,从 1 开始,数到 9 的人下船。

如此循环,直到船上仅剩 15 人为止,问都有哪些编号的人下船了呢?

实例

#include<stdio.h> int c = 0; int i = 1; int j = 0; int a[30] = { 0 }; int b[30] = { 0 }; int main() { while (i<=31) { if (i == 31) { i = 1; } else if (c == 15) { break; } else { if (b[i] != 0) { i++; continue; } else { j++; if (j != 9) { i++; continue; } else { b[i] = 1; a[i] = j; j = 0; printf("第%d号下船了\n", i); i++; c++; } } } } }

执行以上实例,输出结果为:

第9号下船了
第18号下船了
第27号下船了
第6号下船了
第16号下船了
第26号下船了
第7号下船了
第19号下船了
第30号下船了
第12号下船了
第24号下船了
第8号下船了
第22号下船了
第5号下船了
第23号下船了

点击查看所有 C 语言教程 文章: https://codercto.com/courses/l/17.html

查看所有标签

Release It!

Release It!

Michael T. Nygard / Pragmatic Bookshelf / 2007-03-30 / USD 34.95

“Feature complete” is not the same as “production ready.” Whether it’s in Java, .NET, or Ruby on Rails, getting your application ready to ship is only half the battle. Did you design your system to......一起来看看 《Release It!》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

URL 编码/解码
URL 编码/解码

URL 编码/解码