codeforces-320A-Magic Numbers

简介: http://codeforces.com/problemset/problem/320/A 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 14

http://codeforces.com/problemset/problem/320/A

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.

Sample test(s)
Input
114114
Output
YES
Input
1111
Output
YES
Input
441231
Output
NO

思路:
1、 除了1和4不允许出现其他数字。
2、 4的前2位数只可能是14、41或者11
 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,flag=1;
 7     scanf("%d",&n);
 8     while(n)
 9     {
10         if(n%10==4)
11         {
12             n/=10;
13             if(n%10==4)
14             {
15                 n/=10;
16                 if(n%10!=1)
17                     flag=0;
18             }
19             else if(n%10==1)
20                 n/=10;
21             else
22                 flag=0;
23         }
24         else if(n%10==1)
25             n/=10;
26         else
27             flag=0;
28         if(flag==0)
29         {
30             printf("NO\n");
31             break;
32         }
33     }
34     if(n==0 && flag==1)
35         printf("YES\n");
36     return 0;
37 }
View Code

 

目录
相关文章
|
存储 算法
二分查找的一种改进-拉格朗日插值查找法
二分查找的一种改进-拉格朗日插值查找法
99 0
|
IDE 开发工具 Android开发
探索安卓与iOS开发的差异:平台选择对项目成功的影响
在移动应用开发的广阔天地中,安卓和iOS两大平台各领风骚,引领着技术进步的潮流。本文旨在深入剖析这两个平台在开发过程中的关键差异点,包括编程语言、开发工具、用户界面设计以及市场分布等方面。通过对比分析,我们不仅能更好地理解每个平台的独特优势,还能洞察这些差异如何影响项目决策和最终成果。无论你是开发者还是企业决策者,了解这些内容都将助你一臂之力,在选择适合自己项目的开发平台时做出更明智的决策。
|
C语言
使用buildroot制作根文件系统(龙芯1B使用)
使用buildroot制作根文件系统(龙芯1B使用)
771 0
使用buildroot制作根文件系统(龙芯1B使用)
|
Ubuntu 网络安全 开发工具
Cubic(Custom Ubuntu ISO Creator)创建自定义镜像
Cubic(Custom Ubuntu ISO Creator)创建自定义镜像
1963 0
Cubic(Custom Ubuntu ISO Creator)创建自定义镜像
|
消息中间件 前端开发 JavaScript
四大常用MQ的优缺点和应用场景选择
四大常用MQ的优缺点和应用场景选择
546 0
|
存储 Oracle 关系型数据库
《Oracle高性能自动化运维》一一1.2 Oracle简介
本节书摘来自华章出版社《Oracle高性能自动化运维》一 书中的第1章,第1. 2节,作者:冷菠  著 ,更多章节内容可以访问云栖社区“华章计算机”公众号查看。
1030 0
|
5天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
15天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~