【枚举】安卓图案解锁题解

链接:https://ac.nowcoder.com/acm/problem/13585
来源:牛客网

解题思路:
1、利用集合来检测是否有重复的按键
2、在遇到1,3,7,9,2,4,6,8都检测一下前面的数是否可以这样连接,枚举所有情况

#include <string>
#include <set>
using namespace std;
char ch(char a, char b)
{
    return '0' + ((a - '0') + (b - '0')) / 2;
}
int main()
{
    string str;
    while (cin >> str)
    {
        int flag = true;
        set<char> s;
        for (int i = 0; i < str.length(); i++)
            s.insert(str[i]);
        if (s.size() != str.length() || str.find('0') != str.npos || str.length() > 30)
        {
            cout << "NO" << endl;
            flag = false;
            continue;
        }
        for (int i = 1; i < str.length(); i++)
        {
            if (str[i] == '7')
            {
                if (str[i - 1] == '1' || str[i - 1] == '3' || str[i - 1] == '9')
                {
                    string s = str.substr(0, i);
                    if (s.find(ch(str[i], str[i - 1])) == s.npos)
                    {
                        cout << "NO" << endl;
                        flag = false;
                        break;
                    }
                }
            }
            if (str[i] == '9')
            {
                if (str[i - 1] == '1' || str[i - 1] == '3' || str[i - 1] == '7')
                {
                    string s = str.substr(0, i);
                    if (s.find(ch(str[i], str[i - 1])) == s.npos)
                    {
                        cout << "NO" << endl;
                        flag = false;
                        break;
                    }
                }
            }

            if ((str[i] == '2' && str[i - 1] == '8') || (str[i] == '8' && str[i - 1] == '2'))
            {
                string s = str.substr(0, i);
                if (s.find(ch(str[i], str[i - 1])) == s.npos)
                {
                    cout << "NO" << endl;
                    flag = false;
                    break;
                }
            }

            if ((str[i] == '4' && str[i - 1] == '6') || (str[i] == '6' && str[i - 1] == '4'))
            {
                string s = str.substr(0, i);
                if (s.find(ch(str[i], str[i - 1])) == s.npos)
                {
                    cout << "NO" << endl;
                    flag = false;
                    break;
                }
            }

            if (str[i] == '1')
            {
                if (str[i - 1] == '7' || str[i - 1] == '3' || str[i - 1] == '9')
                {
                    string s = str.substr(0, i);
                    if (s.find(ch(str[i], str[i - 1])) == s.npos)
                    {
                        cout << "NO" << endl;
                        flag = false;
                        break;
                    }
                }
            }

            if (str[i] == '3')
            {
                if (str[i - 1] == '1' || str[i - 1] == '7' || str[i - 1] == '9')
                {
                    string s = str.substr(0, i);
                    if (s.find(ch(str[i], str[i - 1])) == s.npos)
                    {
                        cout << "NO" << endl;
                        flag = false;
                        break;
                    }
                }
            }
        }
        if (flag == true)
            cout << "YES" << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流云枫木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值