A. Magic Numberstime limit per test
2 seconds
Memory limit per test
256 megabytes
Input
Standard input
Output
Standard output
A magic number is a number formed by concatenation of number1, 14 and 144.
We can use each of these numbers any number of times. Therefore 14144,141 414 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 integerN, (1 limit ≤ limitNLimit ≤ limit 109 ).
This number doesn' t contain leading zeros.
Output
Print "YES" ifNIs a magic number or print "NO"
If it's not.
Sample test (s) input
114114
Output
YES
Input
1111
Output
YES
Input
441231
Output
NO
Question. The following code is used:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#define inf 1000000000using namespace std;int main(){ //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); char s[100]; cin>>s; int i,j; int l=strlen(s); for(i=0;i<l;i++){ if(s[i]!='1'&&s[i]!='4') break; } if(i<l||s[0]!='1'){ cout<<"NO"<<endl; return 0; } for(i=0;i<l;i++){ if(s[i]!='1') continue; for(j=i+1;j<l;j++){ if(s[j]=='1') break; } if(j-i>3){ cout<<"NO"<<endl; return 0; } } cout<<"YES"<<endl; return 0;}