-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy path8.3.cpp
47 lines (43 loc) · 942 Bytes
/
8.3.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
/*
* 题目名称:2的幂次方
* 题目来源:上海交通大学复试上机题
* 题目链接:http://dwz.win/JPE
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <stack>
using namespace std;
string Function(int n) {
stack<int> power;
int exponent = 0;
while (n != 0) {
if (n % 2 == 1) {
power.push(exponent);
}
n /= 2;
exponent++;
}
string answer = "";
while (!power.empty()) {
if (power.top() == 0) {
answer += "2(0)";
} else if (power.top() == 1) {
answer += "2";
} else {
answer += "2(" + Function(power.top()) + ")";
}
power.pop();
if (!power.empty()) {
answer += "+";
}
}
return answer;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
cout << Function(n) << endl;
}
return 0;
}