惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security @ Cisco Blogs
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Project Zero
Project Zero
C
CERT Recently Published Vulnerability Notes
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cisco Blogs
博客园 - 司徒正美
L
LINUX DO - 热门话题
D
Docker
M
MIT News - Artificial intelligence
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
Jina AI
Jina AI
Last Week in AI
Last Week in AI
Security Latest
Security Latest
The Hacker News
The Hacker News
L
Lohrmann on Cybersecurity
Y
Y Combinator Blog
A
Arctic Wolf
小众软件
小众软件
T
Threat Research - Cisco Blogs
Know Your Adversary
Know Your Adversary
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
I
InfoQ
腾讯CDC
Google DeepMind News
Google DeepMind News
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 叶小钗
I
Intezer
NISL@THU
NISL@THU
A
About on SuperTechFans
爱范儿
爱范儿
C
Cybersecurity and Infrastructure Security Agency CISA
D
Darknet – Hacking Tools, Hacker News & Cyber Security
G
Google Developers Blog
J
Java Code Geeks
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Cyberwarzone
Cyberwarzone
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
Latest news
Latest news

博客园 - 王国营的博客

asp.net Handler中的IsReusable属性及在Handler中使用Session 关于Asp.Net中避免用户连续多次点击按钮,重复提交表单的处理 一道面试题 多层循环的递归实现 不通过第三个变量实现两个整型变量的交换 将PHP文件生成静态文件源码 PHP Warning: date(): It is not safe to rely on the system's timezone settings EcShop 如何制作一张windows sp3启动安装盘iso? win7下xp mode IP在同一网段实现网络共享 sql server 2005附加数据库错误 无法打开物理文件 解决114啦源码(114la)不能生成地方房产和地方报刊问题4级页面0字节 菜鸟学PHP之Smarty入门(组图) 弹出一次“设为主页”和“加入收藏”代码 EMS SQL Manager中文显示乱码的解决方法 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序 CSS和JS链接地址中带的问号是做什么的 IIS7/IIS7.5下轻松配置PHP利器(微软PHP Manager for IIS 7) iframe异步加载技术及性能 新开网址导航网站,欢迎各位朋友捧场
约瑟夫环问题
王国营的博客 · 2013-12-02 · via 博客园 - 王国营的博客

约瑟夫环问题

约瑟夫环是一个数学的应用问题:

已知16个人(以编号0,1,2...15分别表示)围坐在一张圆桌周围。从编号为0的人开始报数(报数从1开始),数到3的倍数的那个人出列;直到圆桌剩下一个,请打印出每个人出列的编号

C#非递归发实现

 static void Main(string[] args)
        {
            //保存移走的人的个数
            int RemoveCount = 0;
            //保存每个人读出的数字
            int ReadCount = 0;
            int[] personNum = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

            while (true)
            {
                for (int i = 0; i < personNum.Length; i++)
                {
                    //表示没有移除的数据
                    if (personNum[i] != -1)
                    {
                        //数字加1
                        ReadCount++;
                        if (ReadCount % 3 == 0)
                        {
                            //设置为移除-1
                            Console.WriteLine(personNum[i]+"号移除");
                            personNum[i] = -1;
                            //移除的个数
                            RemoveCount++;
                            if (RemoveCount >= 16)
                            {
                                break;
                            }
                        }
                    }
                }
                if (RemoveCount >= 16)
                {
                    break;
                }
            }
            //最后输出一个数字
            for (int i = 0; i < personNum.Length; i++)
            {
                if (personNum[i] != -1)
                {
                    Console.WriteLine("最后一个人原来的编号是"+personNum[i]);
                    break;

                }
            }
        }

posted @ 2013-12-02 11:30  王国营的博客  阅读(287)  评论()    收藏  举报