腾讯笔试题——用1,1,2,2,4,4,8,8...2^i,2^i拼凑成一个整数n,求问多少种拼凑方法

用1,1,2,2,4,4,8,8…2^i,2^i拼凑成一个整数n,求问多少种拼凑方法

话不多说,上代码:

#include<iostream>
#include<math.h>
#include<time.h>
#include<set>
using namespace std;
int nums[63];//num[i]表示2^i的个数,只有0,1,2三个取值
//回溯法
int IsOk(long long n,int *nums,int len)
{
    long long sum = 0;
    for (int i = 0; i < len; i++)
        sum += nums[i] * pow(2, i);
    if (sum==n)
        return 0;
    else if (sum > n)
        return -1;
    else
        return 1;
}
void solve(long long n, int index, int *nums, int len,int &count)
{
    if (index >= len)
        return;
    for (int i = 0; i <= 2; i++)
    {
        nums[index] = i;
        if (IsOk(n, nums, len) == 0)
            count++;
        else if (IsOk(n, nums, len) == 1)
            solve(n, index + 1, nums, len, count);
    }
    nums[index] = 0;//回溯法,要撤回上一步的假设
}


long long DP(long long n)//使用动态规划方法
{
    int len = log(n) / log(2) + 1;
    long long **dp = new long long*[n + 1];
    for (long long i = 0; i <= n; i++)
    {
        dp[i] = new long long[len];
    }
    for (int i = 0; i < len; i++)
        for (long long j = 0; j <= n; j++)
            dp[j][i] = 0;

    //dp[n][i]表示使用1,1,2,2,4,4,...,2^i可以组合出n的方案数
    for (int i = 0; i < len; i++)
        dp[0][i] = 1;
    if (n == 1||n==2)
        return n;
    dp[1][0] = 1;
    dp[2][0] = 1;
    for (int i = 3; i <= n; i++)
        dp[i][0] = 0;

    //dp[n][i]=
//  cout << "len=" << len << endl;
    for (int i = 1; i < len; i++)
        for (long long j = 1; j <= n; j++)
        {

            for (int m = 0; m <= 2; m++)
                if (j - pow(2, i)*m >= 0)
                {
                    dp[j][i] = dp[j][i] + dp[(long long)(j - pow(2, i)*m)][i - 1];
                    //cout <<"j="<< j << " " << "i=" << i << " " << "m=" << m<<" "<< dp[j][i]<<endl;
                }

        }
    return dp[n][len - 1];
}

int solve3(long long n)
{
    long long stop = n / 2;
    long long res = 0;
    set<int> myset;
    /*
    将硬币分为两份:1,2,4,8,16,.....和1,2,4,8,16....
    组成两个数值为a,b的两个数字,他们的和是a+b=n; 
    a在每一份中只可能有一种组合方式(二进制的思想)
    */
    for (int i = 1; i <= stop; i++)
    {
        res = i ^ (n - i);
        myset.insert(res);
    }
    //对于1,2,4,8结果再加1.
    int len = log(n) / log(2) + 1;
    if (pow(2, len-1) == n)
        return myset.size() + 1;

    return myset.size();
}


int main()
{
    for (int i = 0; i < 63; i++)
        nums[i] = 0;
    long long n;
    clock_t start, finish;
    while (true)
    {
        cin >> n;
        int len = log(n) / log(2) + 1;
        int count = 0;
        start = clock();
        solve(n, 0, nums, len, count);
        cout << count << endl;
        finish = clock();
        cout << "回溯法耗费时间为" << (double)(finish - start) / CLOCKS_PER_SEC<<"秒"<<endl;
        //
        //
        start = clock();
        long long res = DP(n);
        cout << res << endl;
        finish = clock();
        cout << "动态规划方法耗费时间为" << (double)(finish - start) / CLOCKS_PER_SEC << "秒" << endl;

        start = clock();
        res = solve3(n);
        cout << res << endl;
        finish = clock();
        cout << "第三种方法耗费时间为" << (double)(finish - start) / CLOCKS_PER_SEC << "秒" << endl;
    }

    return 0;   
}
输入a,b,A,B,求问:能否经过同时+1或者同时*2变成A,B
int method1(int a,int b,int A,int B){
    if(a>A || b>B) return -1;
    int c = a-b,
        d = A-B;
    if(c*d < 0) return -1;
    if(c==0 && d==0){
        // ok
    }else if(c!=0 && d!=0){
        if(d%c!=0) return -1;
        int e = d/c;
        if(e&(e-1)!=0) return -1;
    }else return -1;
    int res = 0;
    while(true){
        if(A==a) return res;
        if(A%2==0){
            if(A/2>=a){
                A>>=1;
                res++;
            }else{
                res += A-a;
                return res;
            }
        }else{
            A-=1;
            res++;
        }
    }
}

我的方法:

#include<iostream>
using namespace std;
bool fun(int a,int A,int b,int B,int &count)
{
    if (a == A&&b==B)
        return true;
    if (a > A||b>B)
        return false;
    int count1 = 0;
    int count2 = 0;
    bool res1 = fun(a + 1, A, b + 1, B, count1);
    bool res2 = fun(2 * a, A, 2 * b, B, count2);
    if (res1 || res2)
    {
        if (res1&&res2)
        {
            count = count1 > count2 ? count2 + 1 : count1 + 1;
            return true;
        }
        if (res1)
        {
            count = count1 + 1;
            return true;
        }
        if (res2)
        {
            count = count2 + 1;
            return true;
        }
    }
    else
    {
        count = -1;
        return false;
    }
}

int main()
{
    int a;
    int A;
    //cin >> a;
    //cin >> A;
    a = 100;
    A = 408;
    int b = 1000;
    int B = 4009;
    int count = 0;
    fun(a, A,b,B,count);
    cout << count << endl;



return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值