-
Notifications
You must be signed in to change notification settings - Fork 0
/
等式变换.txt
95 lines (81 loc) · 1.72 KB
/
等式变换.txt
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
/*
输入一个正整数X,在下面的等式左边的数字之间添加+号或者-号,使得等式成立。
1 2 3 4 5 6 7 8 9 = X
比如:
12-34+5-67+89 = 5
1+23+4-5+6-7-8-9 = 5
请编写程序,统计满足输入整数的所有整数个数。
输入: 正整数,等式右边的数字
输出: 使该等式成立的个数
样例输入:5
样例输出:21
*/
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
string get_3x(int number)//得到三进制逆序
{
string symbols = "";
int remain = 0;
while(number)
{
remain = number % 3;
number = number / 3;
symbols += remain + '0';
}
int zero_count = 8 - symbols.size();
for(int i =0; i < zero_count; i++)
symbols += '0';
return symbols;
}
// 0 +
// 1 -
// 2 nul
void equation_transfer(int number)
{
int equal_count = 0;
const int table_size = 6561; //3^8可能出现次数
for(int i = 0; i < table_size; i++)
{
string symbol = get_3x(i);
int x = 0;//和
int y = 1;//暂存结果
for(int j = 7; j >= 0; j--)
{
if(symbol[j] == '0')//+
{
x+=y;
y = 9-j;
}
else if(symbol[j] == '1')//-
{
x+=y;
y = -(9-j);
}
else//null
{
if(y < 0)
y = y * 10 - (9-j);
else
y = y * 10 + (9-j);
}
}
if(x+y == number)//最后进行一次相加 避免末尾连续出现2
{
equal_count++;
cout << symbol << endl;
}
}
cout << equal_count << endl;
}
int main()
{
int number;
while(cin>>number)
{
equation_transfer(number);
}
return 0;
}