codeforces 361 A - Mike and Cellphone

本文针对Codeforces竞赛中689A题进行了解析,该题涉及通过指纹记忆判断是否可能拨错电话号码的问题。通过模拟手指在数字键盘上的移动,算法检查是否存在另一种可能的数字组合,导致拨错电话。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


题目链接:http://codeforces.com/contest/689/problem/A

Description

While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way:

Together with his old phone, he lost all his contacts and now he can only remember the way his fingers moved when he put some number in. One can formally consider finger movements as a sequence of vectors connecting centers of keys pressed consecutively to put in a number. For example, the finger movements for number "586" are the same as finger movements for number "253":

Mike has already put in a number by his "finger memory" and started calling it, so he is now worrying, can he be sure that he is calling the correct number? In other words, is there any other number, that has the same finger movements?

Input

The first line of the input contains the only integer n (1 ≤ n ≤ 9) — the number of digits in the phone number that Mike put in.

The second line contains the string consisting of n digits (characters from '0' to '9') representing the number that Mike put in.

Output

If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print "YES" (without quotes) in the only line.

Otherwise print "NO" (without quotes) in the first line.

Sample Input

Input
3 586
Output
NO
Input
2 09
Output
NO
Input
9 123456789
Output
YES
Input
3 911
Output
YES


题意:自己模拟了很久一直错,参考别人的循环列出可能按错键位的可能,如果有几个位置满足那个状态,那么他就有可能播错电话。

#include<iostream>
#include<cstdio>
using namespace std;
char a[12];
int n;
int xx()
{
    int a1,b,c,d;
    a1=1;
    b=1;
    c=1;
    d=1;
    for(int i=0; i<n; i++)
    {

        if(a[i]=='1'||a[i]=='4'||a[i]=='7'||a[i]=='0') a1=0;
        if(a[i]=='3'||a[i]=='6'||a[i]=='9'||a[i]=='0') b=0;
        if(a[i]=='1'||a[i]=='2'||a[i]=='3') c=0;
        if(a[i]=='7'||a[i]=='9'||a[i]=='0') d=0;
    }
    if(a1||b||c||d) return 0;
    else return 1;
}
int main()
{
    while(cin>>n)
    {
        cin>>a;
        if(xx()) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
}
View Code

 



转载于:https://www.cnblogs.com/hfc-xx/p/5664783.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值