当前位置:网站首页>【指针】求解最后留下的人
【指针】求解最后留下的人
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
边栏推荐
猜你喜欢

Résumé des points de connaissance et des réponses aux exercices après la classe du chapitre 7 de Jia junping dans la huitième édition des statistiques

Hackmyvm target series (2) -warrior
![Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]](/img/0b/ddbee0b8a34627e13bff5598bbaed8.jpg)
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]

Hackmyvm target series (4) -vulny

链队实现(C语言)

《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案

搭建域环境(win)

Binary search tree concept

Hcip -- MPLS experiment

How to understand the difference between technical thinking and business thinking in Bi?
随机推荐
记一次api接口SQL注入实战
Binary search tree concept
"Gold, silver and four" job hopping needs to be cautious. Can an article solve the interview?
Intel oneapi - opening a new era of heterogeneity
Which is more advantageous in short-term or long-term spot gold investment?
Mathematical modeling idea of 2022 central China Cup
SQL注入
内网渗透之内网信息收集(二)
xray與burp聯動 挖掘
msf生成payload大全
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
How to earn the first pot of gold in CSDN (we are all creators)
Data mining - a discussion on sample imbalance in classification problems
Record an edu, SQL injection practice
网络层—简单的arp断网
Chain team implementation (C language)
C language file operation
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
Intranet information collection of Intranet penetration (3)
Sqqyw (indifferent dot icon system) vulnerability recurrence and 74cms vulnerability recurrence