当前位置:网站首页>作业7.21 约瑟夫环问题与进制转换
作业7.21 约瑟夫环问题与进制转换
2022-07-25 09:20:00 【不知名大学生M】
1. 使用循环链表实现约瑟夫环

实现函数
//约瑟夫环
void circle(loopLink *L,int m)
{
loopLink *p=L; //p指向头结点
loopLink *q=L;
while(q->next!=L)
{
q=q->next; //q指向头结点L的前一个结点
}
int count=0; //计数
while(p!=q) //结点大于1个
{
count++;
if(count==m)
{
printf("%d ",p->data);
count=0;
q->next=p->next;
free(p);
p=q->next;
}
else
{
p=p->next;
q=q->next;
}
}
printf("%d \n",p->data);
free(p);
p=NULL;
q=NULL;
}
运行结果
人数为8,数到4出圈,所得到序列为4 8 5 2 1 3 7 6
人数为10,数到3出圈,所得到的序列为3 6 9 2 7 1 8 5 10 4

主函数代码
#include "looplink.h"
int main(int argc, const char *argv[])
{
int n;
int m;
printf("输入人数n:");
scanf("%d",&n);
printf("输入数m:");
scanf("%d",&m);
loopLink *L=list_create();
for(int i=1;i<=n;i++)
{
list_insert_tail(L,i);
}
loopLink *S=delete_head(L);
printf("新序列为:");
circle(S,m);
return 0;
}
2. 使用栈的特点完成进制转换

函数代码
//进制转换
void conversion(int n,int m)
{
seqStack *S=creat();
while(n/m!=0)
{
int a=n%m;
push(S,a);
n/=m;
}
push(S,n);
int b=0;
printf("进制转换结果:");
while(!empty(S))
{
pop(S,&b);
if(b>-1&&b<10)
{
printf("%d",b);
}
else if(b>=10&&b<=15)
{
char c=b+55;
printf("%c",c);
}
else
{
printf("进制转换失败");
}
}
printf("\n");
}
运行结果
输入十进制数987,可以使用二进制,八进制,十六进制,可以使用十六进制以内的任意进制
二进制结果1111011011
八进制结果1733
十六进制结果3DB
六进制结果4323

主函数代码
#include "seqstack.h"
int main(int argc, const char *argv[])
{
int a;
int b;
printf("输入十进制数:");
scanf("%d",&a);
printf("输入进制:");
scanf("%d",&b);
conversion(a,b);
return 0;
}
源代码链接
循环链表 https://gitee.com/Wei_JiaMin/looplink.git
顺序栈 https://gitee.com/Wei_JiaMin/seqstack.git
边栏推荐
- OverTheWire-Natas
- 微信小程序中的列点击隐藏,再点击显示
- ~4.1 剑指 Offer 05. 替换空格
- 【代码源】每日一题 非递减01序列
- office文件对应的Content-Type类型
- C#语言和SQL Server数据库技术
- [WSN communication] optimize HWSN energy-saving clustering protocol based on MATLAB biogeography [including Matlab source code, 1989]
- C language and SQL Server database technology
- ActiveMQ -- dead letter queue
- 【代码源】每日一题 算的我头都大啦
猜你喜欢

Nacos启动报错Unable to start web server

数据控制语言(DCL)

【代码源】每日一题 算的我头都大啦

*7-2 CCF 2015-09-2 日期计算
![[De1CTF 2019]SSRF Me](/img/12/44c37cc713b49172a10579c9628c94.png)
[De1CTF 2019]SSRF Me

Prim 最小生成树(图解)

『每日一问』简单聊聊JMM/说说对JMM的了解
![[WSN communication] optimize HWSN energy-saving clustering protocol based on MATLAB biogeography [including Matlab source code, 1989]](/img/47/c804acc3f8de1f0cd5938c3d889e46.png)
[WSN communication] optimize HWSN energy-saving clustering protocol based on MATLAB biogeography [including Matlab source code, 1989]

Week小结

~4.1 剑指 Offer 05. 替换空格
随机推荐
[GKCTF 2021]easynode
~4.1 剑指 Offer 05. 替换空格
Go foundation 3
MySQL排序
MySQL appends a string to the string of a field in the table [easy to understand]
Thick willow dustpan, thin willow bucket, who hates reptile man? Asynchronous synergism, half a second to strip away a novel
excl批量导入数据,后台公共解析方法
Numpy- array属性、改变形状函数、基本运算
~1 ccf 2022-06-2 寻宝!大冒险!
@3-1 CCF 2020-09-1 称检测点查询
梦想启航(第一篇博客)
C#语言和SQL Server数据库技术
Difference between redis and mongodb (useful for interview)
【代码源】每日一题 算的我头都大啦
sqli-labs安装 环境:ubuntu18 php7
redis的五种数据结构原理分析
Go foundation 1
BigDecimal 对数据进行四舍五入
Understand why we should rewrite the equals method and hashcode method at the same time + example analysis
What are stand-alone, cluster and distributed?