-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatingDecimals.cpp
More file actions
60 lines (54 loc) · 1.68 KB
/
RepeatingDecimals.cpp
File metadata and controls
60 lines (54 loc) · 1.68 KB
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
60
#include<stdio.h>
#include<stdlib.h>
#include<map>
using namespace std;
int* repeatingDigit;
int main(){
int numerator,denominator;
int remainder = 0;
bool first = 1;
while( scanf("%d %d",&numerator,&denominator) == 2 ){
map <int,int> remainderMap;
map <int,int>::iterator it;
int idx = 0;
repeatingDigit = (int*) malloc(100001*sizeof(int));
printf("%d/%d = %d.",numerator,denominator,numerator/denominator);
if(numerator % denominator == 0){
printf("(0)\n");
printf("%c%c%c1 = number of digits in repeating cycle\n\n",' ',' ',' ');//naive input formatting
}else{
remainder = numerator % denominator;
do{
remainder *= 10;
it = remainderMap.find(remainder);
if( it != remainderMap.end()){
break;
}
remainderMap[remainder] = idx;
//tadi salah di sini... ada yang dimasukkin dua kali di map
if(idx<50)repeatingDigit[idx]= remainder/ denominator;
idx ++;//tadi juga salah nempatin bagian ini, jadinya posisi digit nya salah..
remainder = remainder % denominator;
}while(remainder != 0 );
if(remainder == 0 ){
for(int i = 0 ; i < idx ; i ++){
printf("%d",repeatingDigit[i]);
}
printf("(0)\n%c%c%c1 = number of digits in repeating cycle\n\n",' ',' ',' ');
}else{
for(int i = 0; i < (it->second) ;i++)printf("%d",repeatingDigit[i]);
printf("(");
int limit = idx;
if(idx > 50) limit = 50;
for(int i = (it->second) ; i < limit ;i++){
printf("%d",repeatingDigit[i]);
}
if(idx>49)printf("...");
printf(")\n");
printf("%c%c%c%d = number of digits in repeating cycle\n\n",' ',' ',' ',idx-(it->second));
}
}
free(repeatingDigit);
}
return 0;
}