深信服最后一道笔试题看起来简单,做起来难

5道大题都好难,没刷过题的我都没做出来,看到最后一道题简单做了1个小时,还是没做出来,第二天又想了一上午才做出来。
大概是一个正整数用2的幕次方表示次数,比如:
输入5
5=1+1+1+1+1
5=2+1+1+1
5=2+2+1
5=4+1
输出就为4

是我太菜了吧,最后结果:
// sxf5.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
using namespace std;

int nummu(int k)
{
int  km = 1;
if (k == 0)
{
return 1;
}
else
{
for (size_t i = 1; i <= k; i++)
{
km = km * 2;
}
}
return km;
}

int cal1(int a, int mu)
{
int re = 0;
int k = 0;
int lk = 0;

int mu1 = nummu(mu);
for (size_t i = 1; a >= i*mu1; i++)
{
re = i*mu1;
if (a - re == 0)
{
k++;
break;
}
else
{
if (mu == 0)
{
k++;
break;
}
else
{
int muj = mu - 1;
for (size_t j = 0; j<=muj||(a-re )< nummu(j); j++)
{
k+= cal1(a - re, j);
}
}
}
}
return k;
}

int cal(int a)
{
int k = 0;
for (size_t i = 0; a >= nummu(i); i++)
{
k += cal1(a, i);
}
return k;
}

int _tmain(int argc, _TCHAR* argv[])
{
int ina;
while (true)
{
cin >> ina;
int k = cal(ina);
cout << k << endl;
}
return 0;
}

这还是调试多次的结果。
#深信服#
全部评论
研发还是测试岗?
点赞 回复
分享
发布于 2017-10-14 17:45
深信服对c很执着😎
点赞 回复
分享
发布于 2017-10-14 18:32
小红书
校招火热招聘中
官网直投
动态规划,就5行
点赞 回复
分享
发布于 2017-10-14 18:48
动态规划的,但是只通过了90
点赞 回复
分享
发布于 2017-10-14 20:53
楼主拿到offer了吗
点赞 回复
分享
发布于 2017-11-22 12:07

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务