当前位置: 首页 > 工具软件 > Magic > 使用案例 >

Magic Numbers

令狐翰
2023-12-01

原题网址:http://codeforces.com/problemset/problem/320/A

A. Magic Numbers
time 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 numbers 114 and 144. We can use each of these numbers any number of times. Therefore 14144141414 and 1411 are magic numbers but 1444514 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.

Sample test(s)
input
114114
output
YES
input
1111
output
YES
input
441231
output
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;
}


 类似资料:

相关阅读

相关文章

相关问答