这个题解排版哪里有问题啊……
求助
2x 为偶数
10x+1 为奇数
我们尝试着用递归倒着做。
当前数字如果是偶数就除2。
否则就减1再除10。
想一想,有没有当前末尾为3,5,7,9的情况呢?(除开始输入的a)
答案是否定的,逆推其中的任意一步都是正推过来的。
不可能有不相符的情况。
中间的步骤怎么处理呢?
由于是倒退,我们也得倒着输出得到的数。
可以手写一个数组代替栈。
代码:
#include<bits/stdc++.h>
using namespace std;
int a,b;
int r[35];//想象路径最长情况,即每一步都除2,2的30次方超过10^9,开30就够了。
int c=0;
bool fun(int a,int b){
if(b<a) return false;//边界
if(b==a) return true;
if(b%2==0){
r[c++]=b/2;
return fun(a,b/2);
}
else{
r[c++]=(b-1)/10;
return fun(a,(b-1)/10);
}
}
int main()
{
scanf("%d%d",&a,&b);
if(fun(a,b)){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
return 0;
}
for(int i=c-1;i>=0;i--){//倒序输出
cout << r[i] << " ";
}
cout << b << endl;//递归过程中无法计入,最后特别输出
return 0;
}
$2x$ 为偶数
$10x+1$ 为奇数
我们尝试着用递归倒着做。
当前数字如果是偶数就除$2$。
否则就减$1$再除$10$。
想一想,有没有当前末尾为$3,5,7,9$的情况呢?(除开始输入的a)
答案是否定的,逆推其中的任意一步都是正推过来的。
不可能有不相符的情况。
中间的步骤怎么处理呢?
由于是倒退,我们也得倒着输出得到的数。
可以手写一个数组代替栈。
代码:
```c
#include<bits/stdc++.h>
using namespace std;
int a,b;
int r[35];//想象路径最长情况,即每一步都除2,2的30次方超过10^9,开30就够了。
int c=0;
bool fun(int a,int b){
if(b<a) return false;//边界
if(b==a) return true;
if(b%2==0){
r[c++]=b/2;
return fun(a,b/2);
}
else{
r[c++]=(b-1)/10;
return fun(a,(b-1)/10);
}
}
int main()
{
scanf("%d%d",&a,&b);
if(fun(a,b)){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
return 0;
}
for(int i=c-1;i>=0;i--){//倒序输出
cout << r[i] << " ";
}
cout << b << endl;//递归过程中无法计入,最后特别输出
return 0;
}