对于任意的真分数 N/M ( 0 < N < M ),均可以求出对应的小数。如果采用链表表示各个小数,对于循环节采用循环链表表示,则所有分数均可以表示为如下链表形式。
循环节
输入: N M k
输出: 转换后的小数(不超过 k )
要求: 仅编写将分数转换为小数的函数 change( int n, int m, NODE * head ) 。
预设代码
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{ int data;
struct node * next;
} NODE;
void output( NODE *, int );
void change( int, int, NODE * );
void output( NODE * head, int kk )
{ int k=0;
printf("0.");
while ( head->next != NULL && k<kk )
{ printf("%d", head->next->data );
head = head->next;
k ++;
}
printf("\n");
}
int main()
{ int n, m,k;
NODE * head;
scanf("%d%d%d", &n, &m, &k);
head = (NODE *)malloc( sizeof(NODE) );
head->next = NULL;
head->data = -1;
change( n, m, head );
output( head,k );
return 0;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */
测试用例
in:
29 33 10
out:
0.8787878787↵
#define MAX 10000
void change(int n, int m, struct node *head)
{
struct node *p, *p1, *p2, *p3, *p4;
int chu, shang, yu, i, j;
chu = n;
p = (NODE *)malloc(sizeof(struct node));
p2 = (NODE *)malloc(sizeof(struct node));
shang = chu * 10 / m;
yu = chu * 10 % m;
p->data = shang;
head->next = p;
p1 = p;
if (yu == 0)
{
p->next = NULL;
return;
}
if (yu == chu)
{
p->next = p;
return;
}
p2->data = chu;
p3 = p4 = p2;
p2->next = NULL;
chu = yu;
for (j = 1; j <= MAX; j++)
{
int count = 0;
struct node *c;
c = p3;
while (c != NULL)
{
if (c->data == chu)
{
count++;
break;
}
else
{
c = c->next;
count++;
}
}
if (c != NULL && c->data == chu)
{
struct node *zhuan;
int count2;
zhuan = head;
for (count2 = 1; count2 <= count; count2++)
zhuan = zhuan->next;
p->next = zhuan;
return;
}
p = (NODE *)malloc(sizeof(struct node));
p2 = (NODE *)malloc(sizeof(struct node));
shang = chu * 10 / m;
yu = chu * 10 % m;
p->data = shang;
p1->next = p;
p1 = p;
if (yu == 0)
{
p1->next = NULL;
break;
}
p2->data = chu;
p4->next = p2;
p4 = p2;
p2->next = NULL;
chu = yu;
}
}