会员
周边
众包
新闻
博问
闪存
赞助商
Chat2DB
所有博客
当前博客
我的博客
我的园子
账号设置
会员中心
简洁模式
...
退出登录
注册
登录
no pain no gain
hard working
博客园
::
首页
::
博问
::
闪存
::
新随笔
::
联系
::
订阅
::
管理
::
公告
用C#实现约瑟夫问题
using
System;
namespace
heut
{
class
Class1
{
public
static
void
Main()
{
int
[]a
=
new
int
[
8
];
int
sum
=
8
;
//
共有8个人;
int
k
=
3
;
//
每次数到3就退出;
int
count
=
0
;
//
记录退出的人数;
int
i
=
0
,j
=
0
;
for
(
int
m
=
0
;m
<
sum;m
++
)
a[m]
=
1
;
//
数组元素全部初始化为1;
while
(count
<
sum
-
1
)
{
if
(a[i]
!=
0
)
j
++
;
if
(j
==
k)
{
a[i]
=
0
;
count
++
;
j
=
0
;
//
重新开始,找下一个值!
}
i
++
;
if
(i
==
sum)
i
=
0
;
//
实现环(即围成一圈);
}
for
(
int
m
=
0
;m
<
sum;m
++
)
if
(a[m]
!=
0
)
Console.Write (m
+
1
+
"
\n
"
);
}
}
}
posted on
2006-05-14 16:46
龚明秋
阅读(
884
) 评论(
0
)
收藏
举报
刷新页面
返回顶部