Live2d Test Env

HihoCoder1649 : 漏写的数字([Offer收割]编程练习赛38)(模拟题)

描述

小A今年刚上幼儿园,正在学习写100以内的数字。幼儿园的老师留了一项作业,要求小A从某个100以内的数X开始一直写到另一个100以内的数Y(Y - X > 1)。  

不过粗心的小A在作业中漏写了一个整数(好在小A漏写的不是X,并且至少写下了2个整数)。给定小A写下的数字串,你能求小A漏写的数字是多少吗?

输入

一个只包含数字的字符串。注意小A至少写下了两个数。

输出

小A漏写的数字。

样例输入

9111213

样例输出

10

 只要讨论第一个数字是个一位数还是个两位数的开头即可。

 

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char c[10010];int ans,L,a[10010],cnt;
bool trya()
{
    int i=1;cnt=0;
    a[++cnt]=c[1]-'0';
    while(i<=L){
        if(a[cnt]<9){
            if(i+1>L) continue;
             if(c[i+1]-'0'==a[cnt]+1) a[++cnt]=c[i+1]-'0',i++;
            else if(c[i+1]-'0'==a[cnt]+2) ans=a[++cnt]=a[cnt-1]+1,i++;
            else return false;
        } 
        else if(a[cnt]>=9){
            if(i+2>L) break; 
            if((c[i+1]-'0')*10+c[i+2]-'0'==a[cnt]+1) a[++cnt]=(c[i+1]-'0')*10+c[i+2]-'0',i+=2;
            else if((c[i+1]-'0')*10+c[i+2]-'0'==a[cnt]+2) ans=a[++cnt]=a[cnt-1]+1;
            else return false;
        }
    }
    return true;
}
bool tryb()
{
    int i=2;cnt=0;
    a[++cnt]=(c[1]-'0')*10+c[2]-'0';
    while(i<=L){
        if(i+2>L) break; 
        if((c[i+1]-'0')*10+c[i+2]-'0'==a[cnt]+1) a[++cnt]=(c[i+1]-'0')*10+c[i+2]-'0',i+=2;
        else if((c[i+1]-'0')*10+c[i+2]-'0'==a[cnt]+2)ans=a[++cnt]=a[cnt-1]+1;
        else return false;
    }
    return true;
}
int main()
{
    scanf("%s",c+1);
    L=strlen(c+1);
    if(!trya())  tryb();
    printf("%d\n",ans);
    return 0;
}

 

posted @ 2017-12-27 17:16  nimphy  阅读(234)  评论(0编辑  收藏  举报