zoukankan      html  css  js  c++  java
  • 整数校验器

    作为洛谷月赛的第一题,纯暴力,但是这里面的坑实在是太多了。。。

    看到题解里面各位大佬都使用起了unsigned long long,没听过的我只能祭出打表比较法了

    题意简单来说就是判断一个整数写法是否正常。

    看了一下数据范围,发现这个只能用字符串进行读取啊。。。

    于是我们根据字符串首位可以分为以下几种情况:

    1.首位是0

    如果只有一个0,那么一定是合法的,如果除了它以外还有别的东西,那么一定是不合法的。

    除此之外只要判断是否在区间范围内即可(后面会讲如何判断)

    2.首位是‘-’

    这个应该坑了一批人,但是我比赛时没错在这。。。

    如果只有一个‘-’,那么一定是不合法的

    如果是-0,那么也是不合法的

    如果是-后面一位不是0,那么一定是合法的

    3.首位是1-9的数

    合法了!

    好的,重点来了

    那么接下来讲如何判断是否在区间内。

    题中有一个隐含条件,如果读入的东西爆longlong了,那么一定不在区间内,而如果不爆的话,我们就可以转化成正整数,那么问题就成了如何判断爆不爆

    于是就惊现了毒瘤打法:打表

    首先,将2^63-1打出来,发现是19位,那么大于19位直接判定爆了,等于的话就加一个特判,逐位比较。那么负数也是同理了。。。

    所以这道题就讲完了。。。

    最后,附上本题代码:

      1 #include<cstdio>
      2 #include<cstring>
      3 #define LL long long
      4 using namespace std;
      5 char x[1005];
      6 LL T,len,l,r;
      7 int temp[25];
      8 bool jd1()
      9 {
     10     for(int i=1; i<=18; i++)
     11     {
     12         if(x[i]-'0'>temp[i])
     13         {
     14             return 1;
     15         }
     16         else if(x[i]-'0'==temp[i])
     17         {
     18             continue;
     19         }
     20         else
     21         {
     22             return 0;
     23         }
     24     }
     25     if(x[19]-'0'>temp[19]+1)
     26     {
     27         return 1;
     28     }
     29     else
     30     {
     31         return 0;
     32     }
     33 }
     34 bool jd2()
     35 {
     36     for(int i=1; i<=19; i++)
     37     {
     38         if(x[i-1]-'0'>temp[i])
     39         {
     40             return 1;
     41         }
     42         else if(x[i-1]-'0'==temp[i])
     43         {
     44             continue;
     45         }
     46         else
     47         {
     48             return 0;
     49         }
     50     }
     51     return 0;
     52 }
     53 bool judge()
     54 {
     55     LL s=0;
     56     if(x[0]=='-')
     57     {
     58         if(len==20)
     59         {
     60             if(jd1()==1)
     61             {
     62                 return 0;
     63             }
     64         }
     65         if(len>20)
     66         {
     67             return 0;
     68         }
     69         for(int i=1; i<=len-1; i++)
     70         {
     71             s=(s<<3)+(s<<1)+x[i]-'0';
     72         }
     73         s-=s*2;
     74     }
     75     else
     76     {
     77         if(len==19)
     78         {
     79             if(jd2()==1)
     80             {
     81                 return 0;
     82             }
     83         }
     84         if(len>19)
     85         {
     86             return 0;
     87         }
     88         for(int i=0; i<=len-1; i++)
     89         {
     90             s=(s<<3)+(s<<1)+x[i]-'0';
     91         }
     92     }
     93     if(s>=l&&s<=r)
     94     {
     95         return 1;
     96     }
     97     else
     98     {
     99         return 0;
    100     }
    101 }
    102 int main()
    103 {
    104     temp[1]=9;
    105     temp[2]=2;
    106     temp[3]=2;
    107     temp[4]=3;
    108     temp[5]=3;
    109     temp[6]=7;
    110     temp[7]=2;
    111     temp[8]=0;
    112     temp[9]=3;
    113     temp[10]=6;
    114     temp[11]=8;
    115     temp[12]=5;
    116     temp[13]=4;
    117     temp[14]=7;
    118     temp[15]=7;
    119     temp[16]=5;
    120     temp[17]=8;
    121     temp[18]=0;
    122     temp[19]=7;
    123     scanf("%lld%lld%lld",&l,&r,&T);
    124     for(int i=1; i<=T; i++)
    125     {
    126         scanf("%s",x);
    127         len=strlen(x);
    128         if(x[0]=='0')
    129         {
    130             if(len==1)
    131             {
    132                 if(judge()==1)
    133                 {
    134                     printf("0
    ");
    135                 }
    136                 else
    137                 {
    138                     printf("2
    ");
    139                 }
    140             }
    141             else
    142             {
    143                 printf("1
    ");
    144             }
    145         }
    146         else if(x[0]=='-')
    147         {
    148             if(len==1)
    149             {
    150                 printf("1
    ");
    151             }
    152             else
    153             {
    154                 if(x[1]=='0')
    155                 {
    156                     printf("1
    ");
    157                 }
    158                 else
    159                 {
    160                     if(judge()==1)
    161                     {
    162                         printf("0
    ");
    163                     }
    164                     else
    165                     {
    166                         printf("2
    ");
    167                     }
    168                 }
    169             }
    170         }
    171         else
    172         {
    173             if(judge()==1)
    174             {
    175                 printf("0
    ");
    176             }
    177             else
    178             {
    179                 printf("2
    ");
    180             }
    181         }
    182     }
    183     return 0;
    184 }
  • 相关阅读:
    python super()
    git用法小结(1)--建立远程仓库
    git用法小结(1)--建立远程仓库
    Linux下多线程查看工具(pstree、ps、pstack)
    JAVA操作Oracle数据库中的事务
    MyEclipse 2013优化配置【转】
    C语言多线程编程 死锁解析
    在linux终端执行clear或top命令时出现:'xterm' unknown terminal type的错误
    Linux中获取本机网络信息的几个函数及应用
    基于内存的通信之一 “内核共享消息队列”
  • 原文地址:https://www.cnblogs.com/yufenglin/p/10463535.html
Copyright © 2011-2022 走看看