Problem E
Time Limit : 3000/2000ms (Java/Other) Memory Limit : 65535/65535K (Java/Other)
Total Submission(s) : 98 Accepted Submission(s) : 33
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
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.
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≤10^13). This number doesn't contain leading zeros.
Output
Print "YES" if n is a magic number or print "NO" if it's not.
Sample Input
114114 1111 441231
Sample Output
YES YES NO
————————————————————————————————————————————————————————————————————
(ps:又在学霸室友的指导下AC了。。%%%)
1.直接见代码吧,就是144,14,1扫一遍
代码如下:
#include <stdio.h> #include <string.h> int main(){ int i,sum; char s[100]; while(scanf("%s",s)!=EOF){ sum=0; for(i=0;i<strlen(s);) { if(s[i]=='1'&&s[i+1]=='4'&&s[i+2]=='4') {i=i+3; sum+=3;} else if(s[i]=='1'&&s[i+1]=='4') {i=i+2;sum+=2;} else if(s[i]=='1') {i=i+1;sum++;} else break; } if(sum==strlen(s)) printf("YES\n"); else printf("NO\n"); } return 0; }
大一新生acm,方法可能不好,希望大神指点,不喜勿喷。
人一我百,人十我万!!!