-
Notifications
You must be signed in to change notification settings - Fork 0
/
1015.cpp
103 lines (93 loc) · 1.34 KB
/
1015.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
93
94
95
96
97
98
99
100
101
102
103
/* 10^5(decimal) = 11000011010100000(binary)(¹²17λ)*/
#include <iostream>
#include <math.h>
#include <stack>
using namespace std;
bool isPrime(int target)
{
double temp = sqrt(target*1.0);
if (0 == target || 1 == target)
{
return false;
}
for (int i = 2; i <= temp; ++i)
{
if (0 == target%i)
{
return false;
}
}
return true;
}
typedef struct node
{
int flag; /* 0±íʾÊÇreversible primes*/
struct node *next;
}node;
int main()
{
int integer;
int radix;
stack<int> digit;
int value;
int temp;
node *list = new node;
node *pre = list;
node *cur;
cin >> integer;
while (integer >= 0)
{
cur = new node;
cur->next = NULL;
pre->next = cur;
pre = cur;
cin >> radix;
if (!isPrime(integer))
{
cur->flag = -1;
cin >> integer;
continue;
}
while (radix <= integer)
{
digit.push(integer%radix);
integer /= radix;
}
digit.push(integer);
value = 0;
temp = 1;
while (!digit.empty())
{
value += digit.top()*temp;
temp *= radix;
digit.pop();
}
if (!isPrime(value))
{
cur->flag = -1;
}
else
{
cur->flag = 0;
}
cin >> integer;
}
cur = list->next;
while (NULL != cur)
{
if (0 == cur->flag)
{
cout << "Yes" << endl;
}
else
{
cout << "No" << endl;
}
pre = cur;
cur = cur->next;
delete pre;
}
delete list;
system("pause");
return 0;
}