1211: Analyzing element exists
[Title] Description
has generated a set of M is: (1) k is a known set of element M; (2) if y is an element of M, then, 2y + 1 and 3y + 1 It is the element M; (3) in addition to the above two cases, nothing else can become a number of element M.
Question: any given k and x, please determines whether x is an element of M. where k is an unsigned integer, x is not greater than 100,000, and if so, output YES, otherwise, output NO.
[INPUT]
input integer k and x, separated by commas.
[Output]
If so, then the output YES, otherwise output NO.
[Sample input]
0,22
[sample] output
YES
obviously recursive
#include<iostream>
using namespace std;
bool judge(int x,int y)
{
//cout<<x<<" "<<y<<endl;
if(x==y)
return true;
if(x>y)
return false;
if(x<y)
{
int c=x;
(x*=3)++;
(c*=2)++;
return (judge(x,y)||judge(c,y));//两个条件满足其一
}
}
int main()
{
char d;
int k,n;
cin>>k>>d>>n;
if(judge(k,n))
{
cout<<"YES";
}
else cout<<"NO";
}