求循环节

#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值