社区
C语言
帖子详情
用指针方法处理
6233lele
2005-04-13 08:41:04
有n个人围成一圈,顺序牌号.从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位.
偶不知道怎么做了,望各位赐教
...全文
101
7
打赏
收藏
用指针方法处理
有n个人围成一圈,顺序牌号.从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位. 偶不知道怎么做了,望各位赐教
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
7 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
dongpy
2005-04-13
打赏
举报
回复
//帖一个链表实现的。
#define M 3
typedef struct queue
{
int Index;
queue *next;
}Queue,*Pqueue;
Pqueue CreateQueue(int n)
{
Pqueue pQueue,pQueue_ret,pPrev;
for(int i=1;i<=n;i++)
{
pQueue=(Pqueue)malloc(sizeof(Queue));
pQueue->Index=i;
if(i == 1) //链表头
{
pQueue_ret = pQueue ;
}
else
{
pPrev-> next = pQueue;
}
pPrev = pQueue ;
}
pPrev->next=pQueue_ret;
return pQueue_ret;
}
Pqueue DequeueOne(Pqueue pQueue,int m)
{
Pqueue pPrev;
for(int i=1;i<m;i++)
{
pPrev=pQueue;
pQueue=pQueue->next;
}
pPrev->next= pQueue->next;
printf("%d->",pQueue->Index);
free(pQueue);
return pPrev->next;
}
void DequeueAll(Pqueue pQueue,int m)
{
while (pQueue!=pQueue->next)
{
pQueue=DequeueOne(pQueue,m);
}
printf("%d\n",pQueue->Index);
}
int main()
{
int n;
Pqueue pQueue;
printf("输入人数n:");
scanf("%d",&n);
pQueue=CreateQueue(n);
printf("出列顺序:\n");
DequeueAll(pQueue,M);
return 0;
}
江海门户
2005-04-13
打赏
举报
回复
或者用递归也可以考虑
useresu
2005-04-13
打赏
举报
回复
大概想了一下,没想到被这么多人抢了 ,而且偶的答案似乎太那个了,郁闷哪
useresu
2005-04-13
打赏
举报
回复
很多种方法的,可以用链表实现,先用生成一个n长的链表,
tydef NCODE struct
{
int i;//用于记录人的编号
ncode * pcode;
}ncode
然后把3的倍数的从链表中删除,钱能那本《C++程序设计教程》中有详细的例子。
晨星
2005-04-13
打赏
举报
回复
使用循环链表最直接。
pcboyxhy
2005-04-13
打赏
举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int m, n, step=0, index=0, cir=0, t;
scanf("%d%d", &m, &n);
char *out = (char*)malloc(m);
if(!out) return -1;
memset(out, 0, m);
while(1)
{
if(step==m) break;
while(cir<n)
{
if(!out[index]) ++cir;
index=(index+1)%m;
}
cir=0; ++step; out[(index+m-1)%m]=1;
printf("\nNo.%d out.", !index?m:index);
}
free(out);
system("PAUSE");
return 0;
}
llf_hust
2005-04-13
打赏
举报
回复
#include "stdio.h"
#include "stdlib.h"
typedef struct _list
{
int val;
struct _list* next;
}List,* pList;
void main()
{
int n,i;
pList head,temp,cur,pre,flw;
scanf("%d",&n);
head =(pList) malloc(sizeof(List));
head->val = 1;
cur = head;
for(i = 2 ; i <= n ; i++)
{
temp = (pList) malloc(sizeof(List));
temp->val = i;
cur->next = temp;
cur = temp;
}
cur->next = head;
pre = cur;
cur = head;
i = 1;
while(cur->next != cur)
{
if(i%3==0)
{
pre ->next = cur->next;
free(cur);
cur = pre->next;
}
else
{
pre =cur;
cur= cur->next;
}
i++;
}
printf("%d\n",cur->val);
free(cur);
}
Rightmost Digit
快速幂的题目:
Upscayl -先进的 AI 算法放大并增强低分辨率图像
Upscayl —— 基于Linux优先理念构建的免费开源AI图像放大工具,适用于Linux、MacOS和Windows操作系统。
APQP_template填写要求.xlsx
APQP_template填写要求.xlsx
poc_result_56972824-D129-4768-8FC8-4B2DC8B0DA1E.pptx
poc_result_56972824-D129-4768-8FC8-4B2DC8B0DA1E.pptx
Generated-Music-Section-Timing-Auditor-v1.0-原创源码与文档.zip
原创开发工具源码合集,包含可直接运行的完整源码、自动化测试、离线示例、HTML/JSON/SVG 报告、运行截图、README、使用说明、功能清单、MIT License 与原创声明。适合前端、Python、AI 工具开发与工程实践学习,解压后按 README 即可运行。
C语言
70,038
社区成员
243,245
社区内容
发帖
与我相关
我的任务
C语言
C语言相关问题讨论
复制链接
扫一扫
分享
社区描述
C语言相关问题讨论
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章