讨论 / 题解^_^
Mrchen666 2017-12-02 02:12:25
点我顶贴 收藏 删除
#include <iostream>

#include <string>

#include <math.h>

using namespace std;

int n;

string text;

void gan(int n)

{

if (n != 0)

{

if (n % 2 == 1)

{

if (n - 1 >= 2)

gan(n - 1);

text += "+2(0)";

}

else

{

text += '2';

if (n > 2)

{

text += '(';

int w = log(n) / log(2);

gan(w);

text += ')';

if (n != w)

{

text += '+';

gan(n - pow(2, w));

}

}

}

}

return;

}

int main()

{

cin >> n;

gan(n);

string::size_type a, b, c;

do

{

a = text.find("++", 0);

if (a != string::npos)

text.replace(a, 2, "+");

b = text.find("+)", 0);

if (b != string::npos)

text.replace(b, 2, ")");

} while (a != string::npos || b != string::npos);

c = text.rfind("+");

if (c == (text.size() - 1))

text.erase(c, 1);

cout << text;

return 0;

}

#1 Mrchen666@2017-12-02 02:13:06
回复 删除
2的幂次方

查看更多回复
提交回复