当前位置:网站首页>【指针】求解最后留下的人
【指针】求解最后留下的人
2022-07-06 09:24:00 【|光|】
要求
有n个人围成一圈,顺序排号。从第一个人开始报数,凡报到3的人退出圈子,编一个函数,求解最后留下的是原来的第几号的那位。(用指针实现)
代码
/* * 该函数用来实现计算最后剩下的那一位 * n:围圈的人数 * 返回其编号 */
int last_one(int n)
{
int k=-1;
int i,j,l=0;
int a[n];
for(i=0;i<n;i++)
{
a[i] = 0;
}
for(i=0;i<n-1;i++)
{
for(j=0;j<3;j++)
{
if(a[l]==1)
{
j--;
l++;
if(l==n)
{
l=0;
}
continue;
}
if(j==2)
{
a[l]=1;
}
l++;
if(l==n)
l=0;
}
}
for(i=0;i<n;i++)
{
if(a[i]==0)
{
k = i+1;
break;
}
}
return k;
}
main函数
int main()
{
int n;
scanf("%d",&n);
printf("%d\n",last_one(n));
return 0;
}
测试
测试输入
3
输出
2
边栏推荐
- What language should I learn from zero foundation. Suggestions
- 《统计学》第八版贾俊平第四章总结及课后习题答案
- "Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?
- Web vulnerability - File Inclusion Vulnerability of file operation
- Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
- Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
- 7-8 7104 Joseph problem (PTA program design)
- How to turn wechat applet into uniapp
- xray与burp联动 挖掘
- 内网渗透之内网信息收集(一)
猜你喜欢
Network technology related topics
Tencent map circle
Captcha killer verification code identification plug-in
Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
Intranet information collection of Intranet penetration (2)
7-5 staircase upgrade (PTA program design)
Xray and burp linkage mining
Internet Management (Information Collection)
数据库多表链接的查询方式
随机推荐
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
Only 40% of the articles are original? Here comes the modification method
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
This article explains in detail how mockmvc is used in practical work
外网打点(信息收集)
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
我的第一篇博客
记一次edu,SQL注入实战
《统计学》第八版贾俊平第十一章一元线性回归知识点总结及课后习题答案
Intranet information collection of Intranet penetration (2)
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
Feature extraction and detection 14 plane object recognition
How to understand the difference between technical thinking and business thinking in Bi?
Hackmyvm target series (2) -warrior
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
Realize applet payment function with applet cloud development (including source code)
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
《统计学》第八版贾俊平第四章总结及课后习题答案
Interpretation of iterator related "itertools" module usage
XSS (cross site scripting attack) for security interview