题意:给你一个数字n,有两种操作:减1或乘2,问最多经过几次操作能变成m;
随后发篇随笔普及下memset函数的初始化问题。自己也是涨了好多姿势。
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define INF 0x7fffffff;
using namespace std;
int DP[], vis[];
int dp(int n, int m)
{
if(n <= ) return INF;
if(n >= m) {DP[n] = n-m; return DP[n];}
if(!vis[n])
{
vis[n] = ;
//cout << n << "+++" << endl;
DP[n] = min(dp(n-, m), dp(n*, m))+;
}
return DP[n];
}
int main()
{
int n, m;
while(~scanf("%d%d", &n, &m)){
memset(DP, 0x3f3f3f3f, sizeof(DP));
memset(vis, , sizeof(vis));
//cout << DP[0] << endl;
printf("%d\n", dp(n, m));
}
return ;
}