forked from zhuli19901106/poj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
POJ1426(AC).cpp
92 lines (76 loc) · 1.14 KB
/
POJ1426(AC).cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
using namespace std;
char res[205][205];
bool suc;
void dfs(int a[], int sp, int rem, const int &n)
{
int bit;
if(suc){
return;
}
if(!suc && rem == 0){
suc = true;
int i, j;
j = 0;
for(i = 0; i < sp; ++i){
res[n][j++] = a[i] + '0';
}
res[n][j] = 0;
return;
}else if(!suc && sp < 100){
bit = 0;
a[sp] = bit;
dfs(a, sp + 1, (rem * 10) % n, n);
bit = 1;
a[sp] = bit;
dfs(a, sp + 1, (rem * 10 + 1) % n, n);
}
}
void trim_zero(char str[], int n)
{
int r2, r5;
int r10;
int i;
r2 = 0;
while(n > 0 && n % 2 == 0){
n /= 2;
++r2;
}
r5 = 0;
while(n > 0 && n % 5 == 0){
n /= 5;
++r5;
}
r10 = (r2 > r5 ? r2 : r5);
i = strlen(str) - 1;
while(str[i] == '0'){
--i;
}
i = i + r10 + 1;
str[i] = 0;
}
int main()
{
int n;
int a[100];
for(n = 1; n <= 200; ++n){
memset(a, 0, 100 * sizeof(int));
suc = false;
a[0] = 1;
dfs(a, 1, 1, n);
trim_zero(res[n], n);
}
while(true){
if(scanf("%d", &n) != 1){
break;
}else if(n == 0){
break;
}
printf("%s\n", res[n]);
}
return 0;
}