forked from PashaKlybik/prog-053506
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.1(2).c
103 lines (100 loc) · 2.01 KB
/
5.1(2).c
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
93
94
95
96
97
98
99
100
101
102
103
#include <bits.h>
int size = 0;
struct node {
struct node *l, *r;
int x;
};
struct node* New(){
size++;
struct node *v;
v = malloc(sizeof(struct node) * 1);
v -> x = malloc(sizeof(char) * 100);
v -> l = NULL;
v -> r = NULL;
return v;
}
struct node *head,*tail;
void push_l(int x) {
if(size == 0)
{
head = tail = New();
tail -> x = x;
head -> x = x;
return;
}
struct node *p = head;
head -> l = New();
head = head -> l;
head -> r = p;
head -> x = x;
}
void push_r(int x) {
if(size == 0)
{
head = tail = New();
tail -> x = x;
head -> x = x;
return;
}
struct node *p = tail;
tail -> r = New();
tail = tail -> r;
tail -> l = p;
tail -> x = x;
}
void pop_l() {
struct node *p = head;
size--;
head = head -> r;
free(p);
}
void pop_r() {
size--;
struct node *p = tail;
tail = tail -> l;
free(p);
}
struct node* get(int index) {
struct node * cur = head;
while(index--) {
cur = cur -> r;
}
return cur;
}
int check(int x) {
int cur = 0;
for(int i = 0; i < size; i++) {
cur = cur * 10 + get(i) -> x;
cur %= x;
}
return cur;
}
void divide(int x) {
int F = 1;
struct node* cur = head;
while(cur != NULL){
if(cur -> r != NULL) (cur -> r) -> x += (cur -> x % x) * 10;
cur -> x /= x;
int value = cur -> x;
cur = cur -> r;
if(F == 1 && value == 0) {
pop_l();
} else F = 0;
}
}
int main() {
char *str = malloc(sizeof(char) * 100);
gets(str);
for(int i = 0; i < strlen(str); i++) {
push_r(str[i] - '0');
}
int num = 2;
while(1) {
if(size == 1 && get(0) -> x == 1) break;
while(check(num) == 0) {
printf("%d%c", num, ' ');
divide(num);
}
num++;
}
}