josephus Problem 中级(使用数组模拟链表,提升效率)
问题描写叙述:
在《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 中级(使用数组模拟链表,提升效率)的更多相关文章
- UVA11988-Broken Keyboard(数组模拟链表)
Problem UVA11988-Broken Keyboard Accept: 5642 Submit: 34937 Time Limit: 1000 mSec Problem Descripti ...
- 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 ...
- 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 ...
- PAT 甲级 1052 Linked List Sorting (25 分)(数组模拟链表,没注意到不一定所有节点都在链表里)
1052 Linked List Sorting (25 分) A linked list consists of a series of structures, which are not ne ...
- UVa12657 - Boxes in a Line(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...
- 天梯赛 L2-022. (数组模拟链表) 重排链表
题目链接 题目描述 给定一个单链表 L1→L2→...→Ln-1→Ln,请编写程序将链表重新排列为 Ln→L1→Ln-1→L2→....例如:给定L为1→2→3→4→5→6,则输出应该为6→1→5→2 ...
- CSUOJ 1329 一行盒子(数组模拟链表)
题目:id=1329">http://acm.csu.edu.cn/OnlineJudge/problem.php? id=1329 题意: watermark/2/text/aHR0 ...
- UVa 11988 Broken Keyboard(数组模拟链表)
题目链接: https://cn.vjudge.net/problem/UVA-11988 /* 问题 将一段文本经过一定的规则处理后输出,规则就是[表示home键,表示光标跳到行首,]表示end键, ...
- UVa 11988 (数组模拟链表) Broken Keyboard (a.k.a. Beiju Text)
题意: 模拟一个文本编辑器,可以输入字母数字下划线,如果遇到'['则认为是Home键,如果是']'则认作End键. 问最终屏幕上显示的结果是什么字符串. 分析: 如果在数组用大量的移动字符必然很耗时. ...
随机推荐
- UISwitch(开关控件)、UISegmentedControl(分段控件)
一.UISwitch 1.初始化 UISwitch *s1 = [[UISwitch alloc]initWithFrame:CGRectMake(50, 170, 100, 200)]; 2.设 ...
- 多重背包问题:悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(HDU 2191)(二进制优化)
悼念512汶川大地震遇难同胞——珍惜现在,感恩生活 HDU 2191 一道裸的多重背包问题: #include<iostream> #include<algorithm> #i ...
- JQuery学习(层级)ancestor & descendant
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- mongoDB研究笔记:复制集数据同步机制
http://www.cnblogs.com/guoyuanwei/p/3279572.html 概述了复制集,整体上对复制集有了个概念,但是复制集最重要的功能之一数据同步是如何实现的?带着这个问题 ...
- [MSSQL2012]CUME_DIST函数
CUME_DIST函数以某列作为基准,计算其它行相对于基准行数据的比例.差距比例,比较容易理解 先看下测试数据 DECLARE @TestData TABLE( ID INT IDENTITY ...
- vim安装YouCompleteMe 插件
要安装YouCompleteMe ,vim须支持python.看是否支持,可以在vim中:version 查看, 如果python前有+号,就是支持,减号就是不支持. 如果不支持,需要以编译安装方式重 ...
- hibernate date类型插入数据库时精度只到日期没有时间
由hibernate 的逆向工具从数据库表生成的*.hbm.xml ,对于数据库的date类型生成如下: <property name = "crttime" ...
- 【译】UNIVERSAL IMAGE LOADER.PART 2---ImageLoaderConfiguration详解
ImageLoader类中包含了所有操作.他是一个单例,为了获取它的一个单一实例,你需要调用getInstance()方法.在使用ImageLoader来显示图片之前,你需要初始化它的配置-Image ...
- Spirng quartz 整合
以下是资料来源: quartz maven confighttp://quartz-scheduler.org/downloads Spring 定时器(Timer,Quartz)http://sup ...
- centos安装firefox flash插件
centos下的firefox flash插件默认不是最新版的,安装过程如下: 将安装地址添加到repolist中 sudo yum -y install http://linuxdownload.a ...