码迷,mamicode.com
首页 > 其他好文 > 详细

求循环小数

时间:2015-11-09 20:45:26      阅读:550      评论:0      收藏:0      [点我收藏+]

标签:

对于任意的真分数 N/M ( 0 < N < M ),均可以求出对应的小数。如果采用链表表示各个小数,对于循环节采用循环链表表示,则所有分数均可以表示为如下链表形式。

技术分享

输入: N M

输出: 转换后的小数(不超过 50 )

要求: 仅编写将分数转换为小数的函数 change( int n, int m, NODE * head ) 。 

测试输入技术分享期待的输出技术分享时间限制技术分享内存限制技术分享额外进程技术分享
测试用例 1 以文本方式显示
  1. 1 8↵
以文本方式显示
  1. 0.125↵
1秒 64M 0
测试用例 2 以文本方式显示
  1. 29 33↵
以文本方式显示
  1. 0.87878787878787878787878787878787878787878787878787↵
1秒 64M 0
测试用例 3 以文本方式显示
  1. 7 18↵
以文本方式显示
  1. 0.38888888888888888888888888888888888888888888888888↵
1秒 64M 0
测试用例 4 以文本方式显示
  1. 2 7↵
以文本方式显示
  1. 0.28571428571428571428571428571428571428571428571428↵
1秒 64M 0

预设代码

前置代码

 1 /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
 2 
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 
 6 typedef struct node
 7 {   int         data;
 8     struct node * next;
 9 } NODE;
10 
11 void output( NODE * );
12 void change( int, int, NODE * );
13 
14 void output( NODE * head )
15 {   int k=0;
16 
17     printf("0.");
18     while ( head->next != NULL && k<50 )
19     {   printf("%d", head->next->data );
20         head = head->next;
21         k ++;
22     }
23     printf("\n");
24 }
25 
26 int main()
27 {   int n, m;
28     NODE * head;
29 
30     scanf("%d%d", &n, &m);
31     head = (NODE *)malloc( sizeof(NODE) );
32     head->next = NULL;
33     head->data = -1;
34     change( n, m, head );
35     output( head );
36     return 0;
37 }
38 
39 /* PRESET CODE END - NEVER TOUCH CODE ABOVE */

 

 

编写代码如下:

  1 /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
  2 
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 
  6 typedef struct node
  7 {
  8     int         data;
  9     struct node * next;
 10 } NODE;
 11 
 12 void output(NODE *);
 13 void change(int, int, NODE *);
 14 
 15 void output(NODE * head)
 16 {
 17     int k = 0;
 18 
 19     printf("0.");
 20     while (head->next != NULL && k<50)
 21     {
 22         printf("%d", head->next->data);
 23         head = head->next;
 24         k++;
 25     }
 26     printf("\n");
 27 }
 28 
 29 void change(int n, int m, struct node *head)
 30 {
 31     struct node *p, *left, *p1, *head_yu, *yu1;
 32     int chu;
 33     int shang;
 34     int i, k;
 35     int yu;
 36     chu = n;
 37     p = (NODE *)malloc(sizeof(struct node));
 38     left = (NODE *)malloc(sizeof(struct node));
 39     shang = chu * 10 / m;
 40     yu = chu * 10 % m;
 41     p->data = shang;
 42     head->next = p;
 43     p1 = p;
 44     if (yu == 0){
 45         p->next = NULL;
 46         return;
 47     }
 48     if (yu == chu){
 49         p->next = p;
 50         return;
 51     }
 52     left->data = chu;
 53     head_yu = yu1 = left;
 54     left->next = NULL;
 55     chu = yu;
 56     for (k = 1; k <= 49; k++){
 57         int count = 0;
 58         struct node *c;
 59         c = head_yu;
 60         while (c != NULL){
 61             if (c->data == chu){
 62                 count++;
 63                 break;
 64             }
 65             else{
 66                 c = c->next;
 67                 count++;
 68             }
 69         }
 70         if (c!=NULL&&c->data == chu){
 71             struct node *zhuan;
 72             int count2;
 73             zhuan = head;
 74             for (count2 = 1; count2 <= count; count2++)
 75                 zhuan = zhuan->next;
 76             p->next = zhuan;
 77             return;
 78         }
 79         p = (NODE *)malloc(sizeof(struct node));
 80         left = (NODE *)malloc(sizeof(struct node));
 81         shang = chu * 10 / m;
 82         yu = chu * 10 % m;
 83         p->data = shang;
 84         p1->next = p;
 85         p1 = p;
 86         if (yu == 0){
 87             p1->next = NULL;
 88             break;
 89         }
 90         left->data = chu;
 91         yu1->next = left;
 92         yu1 = left;
 93         left->next = NULL;
 94         chu = yu;
 95     }
 96 }
 97 
 98 int main()
 99 {
100     int n, m;
101     NODE * head;
102 
103     scanf("%d%d", &n, &m);
104     head = (NODE *)malloc(sizeof(NODE));
105     head->next = NULL;
106     head->data = -1;
107     change(n, m, head);
108     output(head);
109 
110     return 0;
111 }
112 
113 /* PRESET CODE END - NEVER TOUCH CODE ABOVE */

 

求循环小数

标签:

原文地址:http://www.cnblogs.com/yixianyong/p/4950786.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!