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.
Input
The first line of input contains an integer n, (1 ≤ n ≤ 109). This number doesn’t contain leading zeros.
Output
Print “YES” if n is a magic number or print “NO” if it’s not.
Input
114114
Output
YES
Input
1111
Output
YES
Input
441231
Output
NO
由“1”、“14”、“144”组成的数字输出YES,否则输出NO。
我们只要将其循环,将给定数字除10,100,1000看余数是否满足其中一个条件即可。这是一个很巧妙很简单的做法了。
#include"stdio.h"
#include"string.h"
#include"math.h"
#include"stdlib.h"
#include"iostream"
#include"algorithm"
#include"cstring"
using namespace std;
int main()
{
int a,b[100],c[100],i=0,flag=1;
scanf("%d",&a);
while(a)
{
if(a%10!=1&&a%100!=14&&a%1000!=144)
{
flag=0;
break;
}
a=a/10;
}
if(flag==1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}