zoukankan      html  css  js  c++  java
  • CodeForces Petya and Java strcmp的使用

    B - Petya and Java
    Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u


    Description
    Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger.

    But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?"

    Petya knows only 5 integer types:

    1) byte occupies 1 byte and allows you to store numbers from  - 128 to 127

    2) short occupies 2 bytes and allows you to store numbers from  - 32768 to 32767

    3) int occupies 4 bytes and allows you to store numbers from  - 2147483648 to 2147483647

    4) long occupies 8 bytes and allows you to store numbers from  - 9223372036854775808 to 9223372036854775807

    5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower.

    For all the types given above the boundary values are included in the value range.

    From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him.

    Input
    The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number ncan't be represented as an empty string.

    Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).

    Output
    Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above.

    Sample Input
    Input
    127
    Output
    byte
    Input
    130
    Output
    short
    Input
    123456789101112131415161718192021222324
    Output
    BigInteger

     


    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=122089#problem/B

    主要是strcmp的使用

    当s1<s2时,返回为负数;
    当s1=s2时,返回值= 0;
    当s1>s2时,返回正数。
    即:两个字符串自左向右逐个字符相比(按ASCII值大小相比较),直到出现不同的字符或遇''为止。如:"A"<"B" "a">"A" "computer">"compare"
    特别注意:strcmp(const char *s1,const char * s2)这里面只能比较字符串,即可用于比较两个字符串常量,或比较数组和字符串常量,不能比较数字等其他形式的参数。

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <string.h>
    using namespace std;
    
    char a[105];
    
    int main()
    {
        while(~scanf("%s",a))
        {
            int len=strlen(a);
            if(len<3||(len==3&&strcmp(a,"127")<=0))
                printf("byte
    ");
            else if(len<5||(len==5&&strcmp(a,"32767")<=0))
                printf("short
    ");
            else if(len<10||(len==10&&strcmp(a,"2147483647")<=0))
                printf("int
    ");
            else if(len<19||(len==19&&strcmp(a,"9223372036854775807")<=0))
                printf("long
    ");
            else
                printf("BigInteger
    ");
        }
    }
    

      


  • 相关阅读:
    什么是看板方法?
    瓶颈法则
    累积流图——你还没有用过吗?
    为什么我们关注看板方法?
    蒟蒻报道
    博客更换通知
    浅谈树套树(线段树套平衡树)&学习笔记
    浅谈FFT(快速博立叶变换)&学习笔记
    题解 洛谷P1903/BZOJ2120【[国家集训队]数颜色 / 维护队列】
    题解 洛谷P4550/BZOJ1426 【收集邮票】
  • 原文地址:https://www.cnblogs.com/Hyouka/p/5702106.html
Copyright © 2011-2022 走看看