大致双搜索的开始 -- 数字还是很神奇的
大致快做对了- -
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <map>
#include <iomanip>
using namespace std;
map<long long ,long long > d;//记忆化
long long part(long long );
long long sum;
long long sum1;
int dv;
void dfs(long long n,long long s)//第二搜索记忆化查找
{
//cout<<n<<' '<<s<<endl;
dv=n; //二叉右根树最后一个有效值
if(n<=0) {cout<<n<<endl;return ;}
if(n%10==n) {d[n]=1;return ;}
if(d[n]>0)
{
//cout<<n<<' '<<d[n]<<endl;
return ;
}
long long x=part(n);// n的字典序最大部分
if(x==n) while(x>9) x/=10;
dfs(x,s);
if(x<s) x=s;
dfs(n-x,s);
d[n]+=d[x]+d[n-x];
//cout<<n<<' '<<d[n]<<' '<<x<<' '<<d[x]<<' '<<n-x<<' '<<d[n-x]<<' '<<y<<' '<<s<<endl;
}
void edfs(long long n) //第一搜索查询
{
if(n<=0) return ;
long long x=part(n);
long long y=part(n-x);
if(x==n)
{
while(x>9) x/=10;
//sum1+=x;
//cout<<setw(4)<<n-x<<' '<<x<<endl;
sum+=1;
edfs(n-x);
}
else
{
while(y>9) y/=10;
//sum1+=x;
dfs(x,y);
//cout<<dv<<' '<<y<<endl;
if(dv<y)
{
while(dv>=0) {dv-=y;}
sum++;
}
else dv=0;
if(dv<0) d[x]--;
sum+=d[x];
// cout<<setw(4)<<d[x]<<' '<<x<<' '<<y<<' '<<n<<' '<<dv<<endl;
d.clear();
edfs(n-x+dv);
}
}
long long part(long long n) //部分最大记录
{
long long s=0;
long long m=0,cc=0,ms=1;
while(n>0)
{
s=s+(n%10)*ms;
if(n%10>m)
{
m=n%10;
cc=s;
}
n=n/10;
ms*=10;
}
return cc;
}
int main()
{
long long n;
while(cin>>n)
{
sum=0;sum1=0;
d.clear();
edfs(n);
cout<<sum<<endl;
}
}
这个是已AC代码 满满的都是拼
#include <iostream> #include <cstring> #include <iomanip> #include <cstdio> #include <string> #include <algorithm> #include <queue> #include <cmath> #include <map> using namespace std; map<long long ,long long >d[100]; map<long long ,long long >p[100]; long long part(long long ); void dfs(long long n,long long s) { if(d[s][n]) return ; if(n<=0) return ; if(n%10==n) { d[s][n]=1; return ; } long long x=part(n); long long y=part(n-x); if(x==n) while(x>9) x/=10; while(y>9) y/=10; if(y<s) y=s; dfs(x,y); if(x<s) x=s; long long z=0; long long w; if(p[y][x]<y&&p[y][x]) { //cout<<z<<' '<<n<<endl; z=p[y][x]; w=n-x+z-y; dfs(n-x+z-y,s); d[s][n]+=d[y][x]+d[s][n-x+z-y]; p[y][n]=p[y][n-x+z-y]; } else { if((n-x)%10==n-x) { if(n-x>0) d[s][n-x]=1; else d[s][n-x]=0; if(n-x>0)p[s][n-x]=n-x; //cout<<s<<' '<<n-x<<endl; } else { dfs(n-x,s); } w=n-x; d[s][n]+=d[y][x]+d[s][n-x]; p[s][n]=p[s][n-x]; } p[s][n]=p[s][w]; //cout<<s<<' '<<n<<' '<<n-x<<' '<<w<<' '<<' '<<x<<' '<<d[s][n]<<' '<<p[s][n]<<' '<<y<<' '<<x<<' '<<p[y][x]<<endl; } long long part(long long n) { long long s=0; long long m=0,cc=0,ms=1; while(n>0) { s=s+(n%10)*ms; if(n%10>m) { m=n%10; cc=s; } n=n/10; ms*=10; } return cc; } int main() { long long n; while(cin>>n) { dfs(n,0); cout<<d[0][n]<<endl; long long i=0; while(i<100) { d[i].clear(); i++; }i=0; while(i<100) { p[i].clear(); i++; } } }