【算法与生活】电子门锁

106 阅读1分钟

电子门采用6位数的密码,每次新输入一个数字,只校验最新输入的6位,如果这6位和正确密码匹配,则门打开。如何实现实时判断门有没有打开?取前5位作基准,如果基准正确,用最新输入的一位作校验,最后一位也匹配上的话,门就成功打开。

#include<bits/stdc++.h>
using namespace std;
using ll=long long ;

#define fio ios::sync_with_stdio(0);cin.tie(0);
#define fin freopen("D:/in.txt","r",stdin);
#define fout freopen("D:/out.txt","w",stdout);

const ll maxn=1e6+5,C=1e4;
ll a[maxn];

int main()
{
    cout<<"Please Input 6-digit password:";
    ll x;cin>>x;
    cout<<"Start Receiving the User's Input...";

    ll l=x/10,r=x%10;
    //printf("l=%lld r=%lld\n",l,r);
    ll cur_l=0,cur_r=0;
    ll d,i=0;
    while(cin>>d){
        i++;
        if(i<=5) cur_l=cur_l*10+d;
        else {
            cur_r=d;
            //printf("cur_l=%lld cur_r=%lld\n",cur_l,cur_r);
            if(cur_l==l && cur_r==r){
                cout<<"Door Opened"<<"\n";
                break;
            }else {
                cur_l=cur_l%C*10+cur_r;
            }
        }

    }


    return 0;
}

【样例输入输出】 输入: 123456

1

2

3

4

5

7

9

1

2

3

4

5

6

此时输出:Door Opened