有n个人围成一圈,从第1个人开始,1,2,…,m报数,报至m出局,余下的人继续从1,2,…,m报数,重复之前的流程,要求:求出被淘汰编号的序列,及最后剩下的一人是原来的第几号?
例如:
10
3
3 6 9 2 7 1 8 5 10
4
#include<stdio.h>
#include<malloc.h>
typedef struct Qnode {
int date;
struct Qnode *next;
} Qn, *QNO;
typedef struct {
QNO front;
QNO rear;
} Link;
void init(Link &Q) {
Q.front = Q.rear = new Qn;
Q.front->next = NULL;
}//初始化队列
void inset(Link &Q, int e) {
QNO p;
p = new Qn;
p->date = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p;
}//向队列输入元素
void print(Link &Q) {
if (Q.rear == Q.front) {
printf("队列为空");
} else {
QNO p;
for (p = Q.front->next; p != Q.rear->next; p = p->next) {
printf("%d ", p->date);
}
}
}//队列的遍历
void sta(Link &Q, int *e) {
if (Q.rear == Q.front) {
printf("队列为空");
} else {
QNO p;
p = Q.front->next;
*e = Q.front->next->date;
Q.front->next = Q.front->next->next;
delete p;
}
}//出队
int main() {
Link P;
init(P);
Link P2;
init(P2);
int a, b, c;
scanf("%d", &a);
for (int i = 1; i <= a; i++) {
inset(P, i);
}
scanf("%d", &b);
int i = 1;
while (P.rear != P.front) {
print(P);
printf("是P\n");
print(P2);
printf("是P2\n");
sta(P, &c);
printf("\n");
print(P);
printf("\n");
if (i == b) {
inset(P2, c);
i = 1;
if(P.rear == P.front->next){
break;
}
}
else{
inset(P, c);
i++;
}
}
print(P);
print(P2);
}
用栈将10进制转换为8进制
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node
{
int data;
struct Node *pNext;
}NODE ,*PNODE;
typedef struct Stack
{
PNODE pTop;
PNODE pBottom;
}STACK,*PSTACK;
void init(PSTACK pS)
{
pS->pTop=(PNODE)malloc(sizeof(NODE));
if(pS->pTop==NULL)
{
printf("动态内存分配失败\n");
exit(-1);
}
else
{
pS->pBottom=pS->pTop;
pS->pTop->pNext=nullptr;
}
}
void push(PSTACK pS,int val){
PNODE pNew=(PNODE)malloc(sizeof(NODE));
pNew->data=val;
pNew->pNext=pS->pTop;
pS->pTop=pNew;
}
void print(PSTACK pS)
{
PNODE ch=(PNODE)malloc(sizeof(NODE));
ch->pNext=pS->pTop;
while(1){
printf("%d",ch->pNext->data);
ch->pNext=ch->pNext->pNext;
if(ch->pNext==pS->pBottom){
break;
}
}
}
bool pop(PSTACK pS,int *zh)
{
if(pS->pTop!=pS->pBottom){
PNODE ch=(PNODE)malloc(sizeof(NODE));
ch->pNext=pS->pTop;
*zh=pS->pTop->data;
pS->pTop=pS->pTop->pNext;
ch->pNext=NULL;
return true;
}
else{
return false;
}
}
void kong(PSTACK pS)
{
PNODE ch=(PNODE)malloc(sizeof(NODE));
PNODE sh=(PNODE)malloc(sizeof(NODE));
sh->pNext=pS->pBottom->pNext;
pS->pTop=pS->pBottom;
while(ch->pNext!=nullptr||sh->pNext!=nullptr){
ch->pNext=sh->pNext;
sh->pNext=ch->pNext->pNext;
ch->pNext=nullptr;
}
}
int main(){
STACK S;
init(&S);
int a,b;
scanf("%d",&a);
while(a!=0){
b=a%8;
push(&S,b);
a=a/8;
}
print(&S);
}