#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node * next;
} NODE;
NODE * find(NODE *, int *);
void outputring(NODE *);
void change(int, int, NODE *);
void change(int n, int m, NODE *head)
{
int a = n, b[50] = { 0 }, i = 0, j = 0;
NODE * tmp = NULL;
NODE * p = NULL, *q = head;
do
{
b[i++] = a *= 10;
for (p = head->next, j = 0; p != NULL && j<i - 1; p = p->next)
{
if (b[j++] == a)
{
q->next = p;
return;
}
}
tmp = (NODE *)malloc(sizeof(NODE));
tmp->data = a / m;
a %= m;
q->next = tmp;
q = tmp;
q->next = NULL;
} while (a && i<50);
}
NODE * find(NODE * head, int * n)
{
NODE *L = head;
int a[55];
NODE *start, *end;
int num = 0;
L = head->next;
int i = 1;
a[i++] = L->data;
while (L->next != NULL&&L->next->data!=-1)//两个条件不可以交换,因为先判断data若是NULL报错
{
L->data = -1;
L = L->next;
a[i++] = L->data;
}
if (L->next == NULL)
{
*n = 0;
return NULL;
}
else {
start = L->next;
end = L;
num = 1;
int flag;
if (start == end)
flag = 1;
else
flag = 0;
while (start != end)
{
start = start->next;
num++;
}
*n = num;
if (flag==1)
{
i = 1;
NODE *p = head->next;
while (p != end)
{
p->data = a[i];
p = p->next;
i++;
}
start->data = a[i];
}
else
{
i = 1;
NODE *p = head->next;
while (p != end)
{
p->data = a[i];
p = p->next;
i++;
}
}
return L->next;
}
}
void outputring(NODE * pring)
{
NODE * p;
p = pring;
if (p == NULL)
printf("NULL");
else
do
{
printf("%d", p->data);
p = p->next;
} while (p != pring);
printf("\n");
return;
}
int main()
{
int n, m;
NODE * head, *pring;
scanf_s("%d%d", &n, &m);
head = (NODE *)malloc(sizeof(NODE));
head->next = NULL;
head->data = -1;
change(n, m, head);
pring = find(head, &n);
printf("ring=%d\n", n);
outputring(pring);
int ss = 1;
system("pause");
return 0;
}
求循环节
最新推荐文章于 2022-01-26 11:54:04 发布