跳到正文

题解

题解:P12681 【MX-J15-T1】叉叉学习数据类型

#SUSPENDED#约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟
获赞
3
题目
P12681

版本

  1. v1当前

简单题。

首先,编写一个 __int128 类型的快读代码:

C++
__int128 read(){
    __int128 x=0,M=1,f=0,ff=1;
    M<<=120;
    char c=getchar();
    while(c<48||c>57){if(c=='-')ff=-1;c=getchar();}
    while(c>=48&&c<=57){x=(x*10+c-48);if(x>M)f=1,x=x%M;c=getchar();}
    if(f)puts("string"),exit(0);
    return (x+f*M)*ff;
}

这里为防止溢出,把模数设置为 21202^{120}。可以发现,如果取了模,那么 nn 的绝对值一定大于等于 21202^{120},long long 和 unsigned long long 一定是存不下的,只能用 string 存。如果没有取模,则判断 nn 是否在各个数据类型的范围之内。

主函数代码:

C++
int main(){
    __int128 x=read();
    if(x<=INT_MAX&&x>=INT_MIN)cout<<"int"<<endl;
    if(x<=UINT_MAX&&x>=0)cout<<"unsigned int"<<endl;
    if(x<=LLONG_MAX&&x>=LLONG_MIN)cout<<"long long"<<endl;
    if(x<=ULLONG_MAX&&x>=0)cout<<"unsigned long long"<<endl;
    cout<<"string";
}
原文