求助题解排版
  • 板块灌水区
  • 楼主Ctjer
  • 当前回复6
  • 已保存回复6
  • 发布时间2021/6/19 23:43
  • 上次更新2023/11/4 21:42:21
查看原帖
求助题解排版
464170
Ctjer楼主2021/6/19 23:43

这个题解排版哪里有问题啊……

求助


2x2x 为偶数

10x+110x+1 为奇数

我们尝试着用递归倒着做。

当前数字如果是偶数就除22

否则就减11再除1010

想一想,有没有当前末尾为35793,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;
}
2021/6/19 23:43
加载中...