A magic number is a number formed by concatenation of numbers 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but 1444, 514 and 414 are not.
You're given a number. Determine if it is a magic number or not.
The first line of input contains an integer n, (1 ≤ n ≤ 109). This number doesn't contain leading zeros.
Print "YES" if n is a magic number or print "NO" if it's not.
114114
YES
1111
YES
441231
NO
代码:
#include <iostream>
using namespace std;
int main()
{
int n,i,s,x,t;
while(scanf("%d",&n) != EOF)
{
t = 0;
x = n;
s = x%10;
while(x)
{
x = x/10;
if(s == 4 || s == 1)
{
if(s == 4)
{
for(i = 0; i < 3; i++)
{
s = x%10;
if(s == 1)
{
break;
}
x = x/10;
}
if(i >= 2)
{
t = 1;
break;
}
}
}
else
{
t = 1;
break;
}
s = x%10;
}
if(t == 1)
{
printf("NO\n");
}
else
{
printf("YES\n");
}
}
return 0;
}