问题描写叙述:

在《josephus Problem 0基础(使用数组)》中。我们提出了一种最简单直接的解决方式。

可是,细致审视代码之后。发现此种方案的效率并不高,详细体如今。当有人出局时,遍历数组仍须要对其进行推断,

这无疑做了无用功。减少了代码效率。在人数多时尤其明显。

解决方式:

当有人出局时,考虑将当前出局的人的前一个人(未出局)的下一个人置为当前出局的下一个人(未出局)。

这样,便确保了每次对counter的添加都是有效的。遍历到的人都是还没有出局的。大大提升了程序的效率。这事实上运用了链表的思想。

代码:

#include <stdio.h>
/*total people number*/
#define ALL 100
/*people leave when count to left_counter*/
#define left_counter 3
/*next Array record the next people's position*/
int next[ALL]; /*init next array*/
void initNext()
{
int i = 0 ;
for (i = 0; i < ALL; i++)
{
next[i] = (i+1) % ALL;
}
} /*print next array*/
void printNext()
{
int i = 0;
for (i = 0; i < ALL; i++)
{
printf("%d ", next[i]);
}
printf("\n");
} int main(void)
{
initNext();
int left = ALL; /*init total left number*/
int counter = 0; /*init counter*/
int i = 0; /*init array index*/
int prev = All-1; /*init prev*/
while (left > 0)
{
counter++;
/*if counter == left_counter , people out, set next[prev] = next[i]
counter = 0
left--
**/
if (counter == left_counter)
{
left--;
printf("%d is out\n", i+1);
counter = 0;
next[prev] = next[i];
printNext();
} /*change prev, increase index*/
prev = i;
i = next[i]; }
printf("problem finished!\n");
return 0;
}

josephus Problem 中级(使用数组模拟链表,提升效率)的更多相关文章

  1. UVA11988-Broken Keyboard(数组模拟链表)

    Problem UVA11988-Broken Keyboard Accept: 5642  Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...

  2. B - Broken Keyboard (a.k.a. Beiju Text) 数组模拟链表

    You're typing a long text with a broken keyboard. Well it's not so badly broken. The only problem wi ...

  3. C - Boxes in a Line 数组模拟链表

    You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simul ...

  4. PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)

    1052 Linked List Sorting (25 分)   A linked list consists of a series of structures, which are not ne ...

  5. UVa12657 - Boxes in a Line(数组模拟链表)

    题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...

  6. 天梯赛 L2-022. (数组模拟链表) 重排链表

    题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...

  7. CSUOJ 1329 一行盒子(数组模拟链表)

    题目:id=1329">http://acm.csu.edu.cn/OnlineJudge/problem.php? id=1329 题意: watermark/2/text/aHR0 ...

  8. UVa 11988 Broken Keyboard(数组模拟链表)

    题目链接: https://cn.vjudge.net/problem/UVA-11988 /* 问题 将一段文本经过一定的规则处理后输出,规则就是[表示home键,表示光标跳到行首,]表示end键, ...

  9. UVa 11988 (数组模拟链表) Broken Keyboard (a.k.a. Beiju Text)

    题意: 模拟一个文本编辑器,可以输入字母数字下划线,如果遇到'['则认为是Home键,如果是']'则认作End键. 问最终屏幕上显示的结果是什么字符串. 分析: 如果在数组用大量的移动字符必然很耗时. ...

随机推荐

  1. Python成长笔记 - 基础篇 (三)python列表元组、字典、集合

    本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码   一.列表和元组的操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义 ...

  2. 第12周&第13周

    12&13:STL Standard Template Library sort, binary_search/lower_bound/upper_bound, multiset, set, ...

  3. Magicodes.WeiChat——利用纷纭打造云日志频道

    纷纭,是个免费的渠道集成工具.这里我就不多介绍了,右侧是飞机票:https://lesschat.com/ 在开发或者在运维情况下,我们经常需要查看并关注服务器端日志以确保程序是否健康运行.尤其是在微 ...

  4. IIS 7.5 + asp.net MVC4 设置路由处理URL请求

    使用asp.net MVC4开发的网站,在本地的VS012环境下运行,一切正常.但当发布到Windows 2008 R2(IIS7.5 + Framework4.5)上时,访问相关网页时,出现有下面的 ...

  5. C#设计模式(22)——访问者模式(Vistor Pattern)

    一.引言 在上一篇博文中分享了责任链模式,责任链模式主要应用在系统中的某些功能需要多个对象参与才能完成的场景.在这篇博文中,我将为大家分享我对访问者模式的理解. 二.访问者模式介绍 2.1 访问者模式 ...

  6. 实验四 时序逻辑电路的VHDL设计

    一.实验目的 熟悉QuartusⅡ的VHDL文本设计过程,学习简单时序逻辑电路的设计.仿真和测试方法. 二.实验 1. 基本命题 用VHDL文本设计触发器,触发器的类型可任选一种.给出程序设计.仿真分 ...

  7. Neo4j:Data Model Transformation:From Relation To Graph

    Here are some tips that help you with the transformation: Each entity table is represented by a labe ...

  8. jQuery的XX如何实现?——3.data与cache机制

    往期回顾: jQuery的XX如何实现?——1.框架 jQuery的XX如何实现?——2.show与链式调用 -------------------------- 源码链接:内附实例代码 jQuery ...

  9. VirtualBox安装Ubuntu教程

    1.VirtualBox虚拟机安装,及VirtualBox安装Ubuntu教程VirtualBox版本为VirtualBox-4.3.12-93733-Win.exe,Ubuntu版本为ubuntu- ...

  10. vscode中启动浏览器的tasks.json

    {    // See https://go.microsoft.com/fwlink/?LinkId=733558    // for the documentation about the tas ...