对于任意的真分数 N/M ( 0 < N < M ),均可以求出对应的小数。如果采用链表存储各位小数,对于循环节采用循环链表表示,则所有分数均可以表示为如下链表形式。
输入: N M
输出: 整个循环节
要求:
编写一个尽可能高效的查找循环节起始点的函数: NODE * find( NODE * head, int * n ) 。函数的返回值为循环节的起点(即图中的指针p),n为循环节的长度。
说明:提交程序时请同时提交将分数转换为小数的函数 change( int n, int m, NODE * head ) (前面题目中已经编写)。
预设代码
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include
#include
typedef struct node
{ int data;
struct node * next;
} NODE;
NODE * find( NODE * , int * );
void outputring( NODE * );
void change( int , int , NODE * );
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("%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 );
return 0;
}
/* Here is waiting for you.
void change( int n, int m, NODE * head )
{
}
NODE * find( NODE * head, int * n )
{
}
*/
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
测试用例
in:
1 3
out:
ring=1↵
3↵
#include
#define NEWSIZE 4
int t = 0;
int prior = 0;
typedef struct SeqList
{
int *base;
size_t capa;
size_t len;
}SeqList;
void InitSeqList(SeqList *list,int SIZE)
{
list->capa = SIZE;
list->base = (int *)malloc(sizeof(int)*list->capa);
list->len = 0;
}
int NewSpace(SeqList *list)
{
int *newspace = (int*)malloc(sizeof(int)*(list->capa + NEWSIZE));
if(newspace == NULL)
return 0;
memcpy(newspace, list->base, sizeof(int)*list->capa);
free(list->base);
list->base = newspace;
list->capa += NEWSIZE;
return 1;
}
int FindElem(SeqList *list, int a)
{
int i,flag = 0;
if(list->len == 0)
return -1;
else
{
for(i=0; ilen; i++)
{
if(list->base[i] == a)
{
flag = 1;
break;
}
}
if(flag == 0)
return -1;
else
return i;
}
}
int EndInsert(SeqList *list, int a)
{
if(NULL == list->base)
return 0;
if(list->len >= list->capa && !NewSpace(list))
return 0;
list->base[list->len++] = a;
return 1;
}
void change(int n, int m, NODE *head)
{
NODE *p,*newNode,*q;
SeqList list;
InitSeqList(&list,n);
EndInsert(&list,n);
int yu, chu;
p = head;
while(1)
{
chu = n*10 / m;
yu = n*10 % m;
int pos = FindElem(&list,yu);
if(pos == -1)
{
EndInsert(&list,yu);
newNode = (NODE *)malloc(sizeof(NODE));
newNode->data = chu;
newNode->next = p->next;
p->next = newNode;
p = p->next;
n = yu;
prior++;
}
else
{
t = pos;
if(chu != 0)
{
newNode = (NODE *)malloc(sizeof(NODE));
newNode->data = chu;
newNode->next = p->next;
p->next = newNode;
p = p->next;
prior++;
}
q = head->next;
while(pos--)
q = q->next;
p->next = q;
break;
}
}
}
NODE * find(NODE *head, int *n)
{
NODE *p;
p = head;
*n = prior-t;
for(int i = 0;i < t+1;i++)
p = p->next;
return p;
}