每个砝码只能用一次!
思想:利用进制转换写,先把所给的数转换成3进制,再转换成字符串,
逆转(这样从左到右就是位数从低到高,
数值是1就不管,是2就往上进一位,并将此位记为-1,-1表示待会放在
左边砝码;是3就往上进一位,并将此位记为0)
#include <iostream>
#include <string>
#include <algorithm> //reverse(a.begin(), a.end())
#include <cmath> //pow()
using namespace std;
string change(int n) //将数 n 转换成 3 进制字符串 ,且把三进制数翻转过来了。
{
string s;
while(n)
{
s += n%3+'0' ; //直接在 s 后面添加ÿ