深信服最后一道笔试题看起来简单,做起来难
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;
}
这还是调试多次的结果。
#深信服#