Codeforces Round #189 (Div. 2) A. Magic Numbers【正难则反/给出一个数字串判断是否只由1,14和144组成】
2024-09-05 01:59:14
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 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but1444, 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.
Examples
input
114114
output
YES
input
1111
output
YES
input
441231
output
NO
【代码】:
#include <bits/stdc++.h>
using namespace std;
string a;
int i,k;
bool f=true;
int main() {
cin >> a;
for (i=0;i<a.length();i++)
{
if(a[0]!='1') f=0; //首位非1开头 错
if(a[i]!='1'&&a[i]!='4') f=0;//有非1,4的字符出现 错 if(a[i]=='1') //开头为1 计数器k清零
k=0;
else if(a[i]=='4') //出现4个数超过2 错
{
k++;
if(k>2) f=0;
}
}
if (f) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
最新文章
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(13)-系统日志和异常的处理③
- 【转】Apache 配置虚拟主机三种方式
- play(1) 第一个简单的应用
- codeforces 675C C. Money Transfers(贪心)
- jquery直接获取html页面元素
- ASP.NET JSON的序列化和反序列化 之 Newtonsoft.Json
- ubuntu/linux mint 创建proc文件的三种方法(四)
- 面试之路(28)-反转链表(reverse ListNode)
- p9半幺群
- 058、flannel概述(2019-03-27 周三)
- Linux使用ntpdate和ntpd进行时间同步
- 20165205 2017-2018-2 《Java程序设计》实验四 Android程序设计
- [转]8款实用的jQuery/CSS3最新插件应用
- How to disable a plugin when Jenkins is down?
- 【BZOJ】1634: [Usaco2007 Jan]Protecting the Flowers 护花(贪心)
- SQL UNION操作符使用
- java 第七章 面向对象高级特性
- 《Java编程思想》阅读笔记二
- C++11并发编程:async,future,packaged_task,promise
- [转]LVS+Keepalived负载均衡配置