数据结构 3.求循环小数 | 您所在的位置:网站首页 › c语言求小数循环节 › 数据结构 3.求循环小数 |
数据结构 3. 求循环小数
问题描述输入输出
样例输入(1)输出(1)输入(2)输出(2)输入(3)输出(3)输入(4)输出(4)
前置代码代码
问题描述
对于任意的真分数 N/M ( 0 < N < M ),均可以求出对应的小数。如果采用链表表示各个小数,对于循环节采用循环链表表示,则所有分数均可以表示为如下链表形式。 输入:N M 输出输出:转换后的小数(不超过 50 )。 样例 输入(1) 1 8 输出(1) 0.125 输入(2) 29 33 输出(2) 0.87878787878787878787878787878787878787878787878787 输入(3) 7 18 输出(3) 0.38888888888888888888888888888888888888888888888888 输入(4) 2 7 输出(4) 0.28571428571428571428571428571428571428571428571428 前置代码 /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */ #include #include 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 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 */ 代码 /* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */ #include #include 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 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 */ void change(int n,int m,NODE *head) { int x=0; NODE *p; p=(NODE*)malloc(sizeof(NODE)); p=head; for(int i=1;i break; } else { n=n*10; x=n/m; n=n%m; NODE *q; q=(NODE*)malloc(sizeof(NODE)); q->data=x; p->next=q; p=q; } } p->next=NULL; } |
CopyRight 2018-2019 实验室设备网 版权所有 |