• 求循环小数


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

    输入: N M

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

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

    测试输入关于“测试输入”的帮助期待的输出关于“期待的输出”的帮助时间限制关于“时间限制”的帮助内存限制关于“内存限制”的帮助额外进程关于“{$a} 个额外进程”的帮助
    测试用例 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("
    ");
    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("
    ");
     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 */
  • 相关阅读:
    new Handler()和new Handler(Looper.getMainLooper())的区别
    Okhttp3日志采集功能
    【ACM打卡】ZOJ 1001 1048
    20180808 阿里巴巴测试开发工程师一面
    20180601 -1
    20180601
    20180601 指针
    20180601 虚函数
    20180601 C++-1
    20180601 C++
  • 原文地址:https://www.cnblogs.com/yixianyong/p/4950786.html
Copyright © 2020-2023  润新知