Uva 10970 大块巧克力(记忆式递归or规律)
2012-11-22 16:19:25

[cpp]
//记忆式递归
#include <iostream>
#include <cstring>
using namespace std;
int d[305][305];
int f(int i,int j)

if(d[i][j]!=-1) return d[i][j];
if(i==1) return d[i][j]=j-1;
if(j==1) return d[i][j]=i-1;
if(i%2==0) return d[i][j]=2*f(i/2,j)+1;
else if(j%2==0) return d[i][j]=2*f(i,j/2)+1;
else return d[i][j]=f(i,j-1)+f(i,1)+1;

int main(int argc, char *argv[])

int m,n;
while(cin>>m>>n)
{   memset(d,-1,sizeof(d));
cout<<f(m,n)<<endl;
}
return 0;

//记忆式递归
#include <iostream>
#include <cstring>
using namespace std;
int d[305][305];
int f(int i,int j)
{
if(d[i][j]!=-1) return d[i][j];
if(i==1) return d[i][j]=j-1;
if(j==1) return d[i][j]=i-1;
if(i%2==0) return d[i][j]=2*f(i/2,j)+1;
else if(j%2==0) return d[i][j]=2*f(i,j/2)+1;
else return d[i][j]=f(i,j-1)+f(i,1)+1;
}
int main(int argc, char *argv[])
{
int m,n;
while(cin>>m>>n)
{   memset(d,-1,sizeof(d));
cout<<f(m,n)<<endl;
}
return 0;
}
[cpp]
//找出规律
#include <iostream>
using namespace std;
int main(int argc, char *argv[])

int m,n;
while(cin>>m>>n)
{
cout<<m*n-1<<endl;
}
return 0;

//找出规律
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int m,n;
while(cin>>m>>n)
{
cout<<m*n-1<<endl;
}
return 0;
}