约瑟夫问题-数组实现
2015-12-31 17:22
211 查看
约瑟夫问题_百度百科 http://baike.baidu.com/link?url=EuXtXV4hPi1AtRdzY0vMPG1gQg1kt3uCS5g4Bfy_RzWXAnDwwjJkhxWdRfk2LPnkqubRBTb52CXEaUa5jbIRaK
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
int main(void)
{
int total,out,start,i;
int *Array;
printf("please input the number of people:");
scanf("%d",&total);
printf("which one to be out:");
scanf("%d",&out);
Array=(int*)malloc(total*sizeof(int));
for(i=0; i<total; i++){
Array[i]=i+1;
//printf("%5d",Array[i]);
}
//printf("\n");
for(start=0; 1!=total; total--)
{
for(i=1; i<out; i++)
start=(start+1)%total;
//printf("%5d",Array[start]);
for(i=start; i<total-1; i++)
Array[i]=Array[i+1];
}
printf("\nthe last one is %d",Array[0]);
system("pause");
return 0;
}
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
int main(void)
{
int total,out,start,i;
int *Array;
printf("please input the number of people:");
scanf("%d",&total);
printf("which one to be out:");
scanf("%d",&out);
Array=(int*)malloc(total*sizeof(int));
for(i=0; i<total; i++){
Array[i]=i+1;
//printf("%5d",Array[i]);
}
//printf("\n");
for(start=0; 1!=total; total--)
{
for(i=1; i<out; i++)
start=(start+1)%total;
//printf("%5d",Array[start]);
for(i=start; i<total-1; i++)
Array[i]=Array[i+1];
}
printf("\nthe last one is %d",Array[0]);
system("pause");
return 0;
}
相关文章推荐
- phonegap(cordova) 自定义插件代码篇(二)----android 自动更新
- android_自动检测更新
- Global Azure SQL Server Database 导出和导入配置介绍
- Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJE
- 命令行下构建xcode工程(build和archive)
- Activity launchMode四分之二 ----singleTop
- diskpart删除保护分区的方法
- mysql concat乱码
- HEAP[xxx.exe]:Invalid Address specified to RtlValidateHeap 错误的解决方法总结
- 数组指针与指针数组的区别
- 挥别过去-展望未来
- ios 当前年月获取近半年的年月
- jquery学习初始篇
- Paiting Series 1: Part 1
- Codeforces Round #337 (Div. 2)(3/5)
- js判断游览器是移动端还是PC端
- 【多肉】播种育苗 友情推荐:教你如何买到真种子
- dubbo开发(1)--- zookeeper搭建和dubbo管理端安装
- android Banner 广告轮播
- Dynamics CRM2016 业务流程之Task Flow(二)