-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path链表存储除法后小数.cpp
60 lines (55 loc) · 1.11 KB
/
链表存储除法后小数.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{ int data;
struct node * next;
} NODE;
void output( NODE * );
void change( int, int, NODE * );
void output( NODE * head )
{ int k=0;
printf("0.");
while ( head->next != NULL && k<50 )
{ printf("%d", head->next->data );
head = head->next;
k ++;
}
printf("\n");
}
void change(int n,int m,NODE * head)
{
NODE * p=head,* temp=NULL,* q=NULL;
int upper=n,i=0,j,ans[52];
while(upper&&i<50)
{
ans[i++]=(upper*=10);
q=head->next;
for(j=0;j<i-1&&q!=NULL;)
{//循环节发现,指回去就行
if(ans[j++]==upper)
{
p->next=q;
return;
}
q=q->next;
}
temp=(NODE *)malloc(sizeof(NODE));
temp->data=upper/m;
upper%=m;
p->next=temp;
p=temp;
p->next=NULL;
}
}
int main()
{ int n, m;
NODE * head;
scanf("%d%d", &n, &m);
head = (NODE *)malloc( sizeof(NODE) );
head->next = NULL;
head->data = -1;
change( n, m, head );
output( head );
return 0;
}
/* PRESET CODE END - NEVER TOUCH CODE ABOVE */