Problem description:
Convert the score to a decimal number. I believe many people will. then, here we give a fraction of N/d, n as the numerator, D as the denominator (n, D are all integers), and try programming to find the decimal form of N/d, of course, if this decimal point is an infinite repeating decimal point, enclose the part of the loop in parentheses, and then omit the part of the loop without writing. For example:
1/3 = 0. (3)
22/5 = 4.4
1/7 = 0. (142857)
2/2 = 1.0
3/8 = 0.375
45/56 = 0.803 (571428)
The input is two positive integers, N, D (1 <= N, d <= 100000), and the output is the corresponding decimal number (for the alignment format, please output a line of up to 76 characters ).
Sample input:
1 3
22 5
1 7
Corresponding output:
0. (3)
4.4
0. (142857)
============== Code ===================================
/**
* Copyright (c) aprin at Xiamen University
* 2005-04-23
*/
# Include <stdio. h>
# Include <string. h>
# Define len_shang sizeof (struct node_shang)
# Define len_yushu sizeof (struct node_yushu)
Struct node_shang {/* business node */
Char data;
Struct node_shang * next;
} * Shang_head = 0, * shang_tail = 0;
Struct node_yushu {/* remainder node */
Long data;
Struct node_yushu * next;
} * Yushu_head = 0, * yushu_tail = 0;
Int shang_empty (void) {/* judge whether the quotient string is empty */
Return shang_head = 0;
}
Int yushu_empty (void) {/* determine whether the remainder string is empty */
Return yushu_head = 0;
}
Struct node_shang * new_shang_node (char ch) {/* the node of the new dealer */
Struct node_shang * PTR = (struct node_shang *) malloc (len_shang );
PTR-> DATA = CH;
PTR-> next = 0;
Return PTR;
}
Struct node_yushu * new_yushu_node (long a) {/* Create a remainder node */
Struct node_yushu * PTR = (struct node_yushu *) malloc (len_yushu );
PTR-> DATA =;
PTR-> next = 0;
Return PTR;
}
Void insert_shang (char ch) {/* Insert operator string node */
Struct node_shang * newptr = new_shang_node (CH );
If (shang_empty ())
Shang_head = shang_tail = newptr;
Else {
Shang_tail-> next = newptr;
Shang_tail = newptr;
}
}
Void insert_yushu (long a) {/* Insert the remainder node */
Struct node_yushu * newptr = new_yushu_node ();
If (yushu_empty ())
Yushu_head = yushu_tail = newptr;
Else {
Yushu_tail-> next = newptr;
Yushu_tail = newptr;
}
}
Char * longinttostr (long a, char * Str) {/* convert a long integer to a string */
Char temp;
Int I, J;
I = 0;
If (A = 0) {/* special processing when a = 0 */
STR [0] = '0 ';
I ++;
} Else {
While (! = 0 ){
STR [I] = (a % 10) + '0 ';
A = A/10;
I ++;
}
}
STR [I] = '/0 ';
For (j = 0; j <= (I-1)/2; j ++) {/* invert */
Temp = STR [J];
STR [J] = STR [i-1-j];
STR [i-1-j] = temp;
}
Return STR;/* return length */
}
Long found_xunhuan (void) {/* checks whether cyclic knots exist through equal remainder. If the return position pointer Ind (relative decimal point offset) appears,-1 */
Struct node_yushu * I;
Long ind;
For (I = yushu_head, IND = 0; I-> next! = 0; I = I-> next, IND ++)
If (yushu_tail-> DATA = I-> data)
Return ind;
If (I-> next = 0)
Return-1;
}
Void Div (long d, long n) {/* D is the divisor, and N is the divisor */
Long Yushu, shang_zhenshu, temp, I, len_temp;
Char STR [7];
Struct node_shang * j, * new_node;
/* Calculate the integer part */
Shang_zhenshu = D/N;
D = D % N;
Insert_yushu (d);/* Save the remainder to the remainder linked list */
Longinttostr (shang_zhenshu, STR );
I = 0;
While (STR [I]! = '/0') {/* store the product to the linked list */
Insert_shang (STR [I]);
I ++;
}
Insert_shang ('.');
If (D = 0) {/* Division */
Insert_shang ('0 ');
Return;
}
While (D! = 0) & (temp = found_xunhuan () =-1) {/* stop when the node is exhausted or found */
D = D * 10;/* carry */
Insert_shang (D/n) + '0 ');
Insert_yushu (d % N );
D = D % N;
}
/* Division completed */
If (temp! =-1) {/* discovering cyclic nodes */
J = shang_head;
While (J-> data! = '.')/* Locate the decimal point */
J = J-> next;
For (I = 0; I <temp; I ++)
J = J-> next;/* locate the first digit of the cyclic Section */
New_node = new_shang_node ('(');
New_node-> next = J-> next;
J-> next = new_node;
New_node = new_shang_node (')');
Shang_tail-> next = new_node;
Shang_tail = new_node;
}
}
Void output (void ){
Struct node_shang * I;
Long temp;
I = shang_head;
Temp = 0;
While (I-> next! = 0 ){
Putchar (I-> data );
I = I-> next;
Temp ++;
If (TEMP % 76) = 0)/* output 76 characters per line */
Putchar ('/N ');
}
Putchar (shang_tail-> data );
Putchar ('/N ');
}
Int main (void ){
Long d, n;
Scanf ("% LD % lD", & D, & N );
While (d <1) | (D> 100000) | (n <1) | (n> 100000 )){
Printf ("input is wrong! Please inpute again! (1 <= d, n <= 100000)/n ");
Scanf ("% LD % lD", & D, & N );
}
Div (d, n );
Output ();
Return 0;
}