步进的问题。

来源:百度知道 编辑:UC知道 时间:2024/09/23 10:35:46
由一到三十个数围成一圈,从第七个数开始,每数九个数就提起。当数只剩一半的时候。被提取的数分别是多少。用C++实现并解释。谢谢!

#include<conio.h>
#include<stdio.h>
#include"malloc.h"

#define M 30
#define S 7
#define N 9

struct node // 结构体定义,把环中的每个数都存放在节点中,节点之间通过next指针相连
{
int data;
struct node *next;
};

void main()
{
int i,j,b=0,count=0;
struct node *p,*head,*q;

head=(struct node*)malloc(sizeof(struct node)); // 为头节点分配空间
head->data=S;
head->next=head; // 构造循环链表

for(i=M;i>0;i--) // 创建十个节点,赋值为1~M,同时加每个节点加入循环链表
{
p=(struct node*)malloc(sizeof(struct node));
p->next=head->next;
p->data=i;
head->next=p;
}

while(p->next!=head)
p=p->next;
p->next=head->next; // 将p指针定位到最后一个节点,并将头节点脱离出循环链表
for(i=1;i<S;i++)
p=p->next; // 将p指针定位到第S个节点的前一个节点,如果S为1,表示值为1的节点为第一个计数的节点
for(i=0;i<M;i++)
{
for(j=1;j<N;j++)