josephus Problem 中级(使用数组模拟链表,提升效率)
2014-10-26 17:37
357 查看
问题描述:
在《josephus Problem 初级(使用数组)》中,我们提出了一种最简单直接的解决方案。
但是,仔细审视代码之后,发现此种方案的效率并不高,具体体现在,当有人出局时,遍历数组仍需要对其进行判断,
这无疑做了无用功,降低了代码效率,在人数多时尤其明显。
解决方案:
当有人出局时,考虑将当前出局的人的前一个人(未出局)的下一个人置为当前出局的下一个人(未出局)。这样,便确保了每次对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 初级(使用数组)》中,我们提出了一种最简单直接的解决方案。
但是,仔细审视代码之后,发现此种方案的效率并不高,具体体现在,当有人出局时,遍历数组仍需要对其进行判断,
这无疑做了无用功,降低了代码效率,在人数多时尤其明显。
解决方案:
当有人出局时,考虑将当前出局的人的前一个人(未出局)的下一个人置为当前出局的下一个人(未出局)。这样,便确保了每次对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 中级(使用数组模拟链表,提升效率)
- josephus Problem 初级(使用数组)
- 使用数组模拟链表
- 利用数组模拟的链表解决Josephus问题
- 约瑟夫环-链表和数组表示实现及分析(Josephus Problem)
- UVA11988(破损键盘) 使用数组模拟链表插入
- C/C++:使用数组模拟链表
- 学生类封装 , (链表)插入排序成绩 无动态数组 ,动态开辟空间 以及静态函数,数据成员的使用
- 使用对象-关系映射(ORM)系统中间件提升软件开发效率及质量
- 使用对象-关系映射(ORM)系统中间件提升软件开发效率及质量
- 硬币抛掷模拟(使用数组)
- 提升Delphi编程效率必须使用的快捷键(Delphi2007版本)
- 使用数组的方法,解决Josephus问题
- 使用ORM中间件提升软件开发效率及质量
- Linux中使用history命令提升效率
- 使用锯齿数组提高效率
- 数组、List、ArrayList的使用以及for、foreach效率比较
- 使用对象-关系映射(ORM)系统中间件提升软件开发效率及质量
- MTK 使用数组模拟动态内存分配
- 使用对象-关系映射(ORM)系统中间件提升软件开发效率及质量