排成一圈数3退出最后一人位置问题--C语言谭浩强版练习8.5
2015-10-15 17:10
302 查看
有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号位置
/****************************************************** E8.5 Plan:可以不循环喊1到3,用总数是不是3的倍数代替 CREATE---------------------------- By: Idooi Liu Time: 2015/10/15-1500 ---------------------------------- ******************************************************/ #include <stdio.h> #include <stdlib.h> int expThree(int n, int manArray[]); int main(void) { int nMan; //How many people char *pMan; //The array int siteLast; //The last person's site int i; printf("Please input how many people:\n"); scanf("%d", &nMan); pMan=malloc(nMan*sizeof(char)); //Assignment for(i=0; i<nMan; i++) *(pMan+i)='Y'; siteLast=expThree(nMan, pMan); printf("The last one\'s site is %d\n", siteLast); free(pMan); return 0; } int expThree(int n, int manArray[]) { int i, j; int siteLast; //The last person's site int flag=0; //Count off int k; for(i=0; i<n; i++) { k=0; //For check how many people last for(j=0; j<n; j++) { if(manArray[j]=='Y') k++; siteLast=j; } if(k==1) //Only one person break; if(manArray[i]=='Y') { flag++; //The person who in the team take the next number //If the person's number is divisible by 3, take out of the team if(flag%3==0) manArray[i]='N'; } } return siteLast; }
相关文章推荐
- c++书籍阅读清单
- 自制无线共享工具C++源代码
- C++字符串切割的两种方法
- C++字符串切割的两种方法
- C++ 中关于i++和++i的解析
- C语言实现在屏幕上输出杨辉三角
- C语言assert()函数用法总结
- c++显示转换static_cast, dynamic_cast, const_cast
- C语言字符数组和字符串注意
- 操作系统FCFS,SJF进程调度(C++)
- C++笔试面试总结
- X86-64寄存器和栈帧
- C语言基础
- X86-64寄存器和栈帧
- c/c++和java达到swap不同功能
- C语言在程序中内存
- C++11简介
- C++中数字与字符串之间的转换
- 【C语言深入】[002] valotile 关键字:
- C/C++内存管理详解 堆 栈