C++ 我应该使用哪种数据类型在c++;?

C++ 我应该使用哪种数据类型在c++;?,c++,c,C++,C,我试图解决一个代码,它要求我输入和输出的整数值高达18位。不幸的是,我无法以任何数据类型存储该值。我已经试过了 long int unsigned long long long long double, 这些似乎都不起作用。您能给我建议一些可能有助于我输出值的方法吗。18位数字给出的最大可能值为99999999999999&大约;9.9 × 1017. 这将适合一个无符号的64位整数(最大值264,约为1.8446744×1019)。尝试使用uint64\u t类型以确保您获得此

我试图解决一个代码,它要求我输入和输出的整数值高达18位。不幸的是,我无法以任何数据类型存储该值。我已经试过了

  long int
  unsigned long long 
  long long double,

这些似乎都不起作用。您能给我建议一些可能有助于我输出值的方法吗。

18位数字给出的最大可能值为99999999999999&大约;9.9 × 1017. 这将适合一个无符号的64位整数(最大值264,约为1.8446744×1019)。尝试使用
uint64\u t
类型以确保您获得此信息


希望这有帮助

您可以使用字符串。在C++中检查这个大整数库。

代码:

#include <cstdio>
#include <string>
#include <algorithm>
#include <iostream>

using namespace std;

struct Bigint {
    // representations and structures
    string a; // to store the digits
    int sign; // sign = -1 for negative numbers, sign = 1 otherwise

    // constructors
    Bigint() {} // default constructor
    Bigint( string b ) { (*this) = b; } // constructor for string

    // some helpful methods
    int size() { // returns number of digits
        return a.size();
    }

    Bigint inverseSign() { // changes the sign
        sign *= -1;
        return (*this);
    }

    Bigint normalize( int newSign ) { // removes leading 0, fixes sign
        for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- )
            a.erase(a.begin() + i);
        sign = ( a.size() == 1 && a[0] == '0' ) ? 1 : newSign;
        return (*this);

    }
    // assignment operator
    void operator = ( string b ) { // assigns a string to Bigint
        a = b[0] == '-' ? b.substr(1) : b;
        reverse( a.begin(), a.end() );
        this->normalize( b[0] == '-' ? -1 : 1 );
    }

    // conditional operators
    bool operator < ( const Bigint &b ) const { // less than operator
        if( sign != b.sign ) return sign < b.sign;
        if( a.size() != b.a.size() )
            return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size();
        for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] )
            return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i];
        return false;
    }
    bool operator == ( const Bigint &b ) const { // operator for equality
        return a == b.a && sign == b.sign;
    }

    // mathematical operators
    Bigint operator + ( Bigint b ) { // addition operator overloading
        if( sign != b.sign ) return (*this) - b.inverseSign();
        Bigint c;
        for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ) {
            carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0);
            c.a += (carry % 10 + 48);
            carry /= 10;
        }
        return c.normalize(sign);
    }

    Bigint operator - ( Bigint b ) { // subtraction operator overloading
        if( sign != b.sign ) return (*this) + b.inverseSign();
        int s = sign; sign = b.sign = 1;
        if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s);
        Bigint c;
        for( int i = 0, borrow = 0; i < a.size(); i++ ) {
            borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
            c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
            borrow = borrow >= 0 ? 0 : 1;
        }
        return c.normalize(s);
    }

    Bigint operator * ( Bigint b ) { // multiplication operator overloading
        Bigint c("0");
        for( int i = 0, k = a[i] - 48; i < a.size(); i++, k = a[i] - 48 ) {
            while(k--) c = c + b; // ith digit is k, so, we add k times
            b.a.insert(b.a.begin(), '0'); // multiplied by 10
        }
        return c.normalize(sign * b.sign);

    }

    Bigint operator / ( Bigint b ) { // division operator overloading
        if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
        Bigint c("0"), d;
        for( int j = 0; j < a.size(); j++ ) d.a += "0";
        int dSign = sign * b.sign; b.sign = 1;
        for( int i = a.size() - 1; i >= 0; i-- ) {
            c.a.insert( c.a.begin(), '0');
            c = c + a.substr( i, 1 );
            while( !( c < b ) ) c = c - b, d.a[i]++;
        }
        return d.normalize(dSign);
    }

    Bigint operator % ( Bigint b ) { // modulo operator overloading
        if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
        Bigint c("0");
        b.sign = 1;
        for( int i = a.size() - 1; i >= 0; i-- ) {
            c.a.insert( c.a.begin(), '0');
            c = c + a.substr( i, 1 );
            while( !( c < b ) ) c = c - b;

        }
        return c.normalize(sign);
    }

    // output method
    void print() {
        if( sign == -1 ) putchar('-');
        for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
    }
};


int main() {

    Bigint a, b, c; // declared some Bigint variables
    /////////////////////////

    // taking Bigint input //

    /////////////////////////
    string input; // string to take input
    cin >> input; // take the Big integer as string

    a = input; // assign the string to Bigint a

    cin >> input; // take the Big integer as string
    b = input; // assign the string to Bigint b


    //////////////////////////////////

    // Using mathematical operators //

    //////////////////////////////////



    c = a + b; // adding a and b
    c.print(); // printing the Bigint
    puts(""); // newline


    c = a - b; // subtracting b from a
    c.print(); // printing the Bigint
    puts(""); // newline


    c = a * b; // multiplying a and b
    c.print(); // printing the Bigint
    puts(""); // newline


    c = a / b; // dividing a by b
    c.print(); // printing the Bigint
    puts(""); // newline

    c = a % b; // a modulo b
    c.print(); // printing the Bigint
    puts(""); // newline

    /////////////////////////////////

    // Using conditional operators //

    /////////////////////////////////

    if( a == b ) puts("equal"); // checking equality

    else puts("not equal");
    if( a < b ) puts("a is smaller than b"); // checking less than operator



    return 0;

}
#包括
#包括
#包括
#包括
使用名称空间std;
结构Bigint{
//表达和结构
字符串a;//用于存储数字
int-sign;//对于负数,sign=-1;否则,sign=1
//建设者
Bigint(){}//默认构造函数
Bigint(字符串b){(*this)=b;}//字符串的构造函数
//一些有用的方法
int size(){//返回位数
返回a.size();
}
Bigint inverseSign(){//更改符号
符号*=-1;
返回(*本条);
}
Bigint normalize(int newSign){//删除前导0,修复符号
对于(int i=a.size()-1;i>0&&a[i]='0';i--)
a、 擦除(a.begin()+i);
符号=(a.size()==1&&a[0]==0')?1:newSign;
返回(*本条);
}
//赋值运算符
void运算符=(字符串b){//为Bigint指定一个字符串
a=b[0]='-'?b.substr(1):b;
相反(a.开始(),a.结束());
这->规范化(b[0]='-'?-1:1);
}
//条件运算符
布尔运算符<(常数Bigint&b)常数{//小于运算符
如果(sign!=b.sign)返回signb.a.size();
对于(int i=a.size()-1;i>=0;i--)如果(a[i]!=b.a[i])
返回符号==1?a[i]b.a[i];
返回false;
}
布尔运算符==(常量Bigint&b)常量{//相等运算符
返回a==b.a&&sign==b.sign;
}
//数学运算符
Bigint运算符+(Bigint b){//加法运算符重载
if(sign!=b.sign)返回(*this)-b.inverseSign();
Bigint c;
对于(int i=0,进位=0;i=0;i--){
c、 a.插入(c.a.begin(),'0');
c=c+a.substr(i,1);
而(!(c=0;i--)putchar(a[i]);
}
};
int main(){
Bigint a,b,c;//声明了一些Bigint变量
/////////////////////////
//接受Bigint输入//
/////////////////////////
字符串输入;//获取输入的字符串
cin>>输入;//取大整数作为字符串
a=input;//将字符串分配给Bigint a
cin>>输入;//取大整数作为字符串
b=input;//将字符串分配给Bigint b
//////////////////////////////////
//使用数学运算符//
//////////////////////////////////
c=a+b;//添加a和b
c、 print();//打印Bigint
puts(“”;//换行符
c=a-b;//从a中减去b
c、 print();//打印Bigint
puts(“”;//换行符
c=a*b;//将a和b相乘
c、 print();//打印Bigint
puts(“”;//换行符
c=a/b;//a除以b
c、 print();//打印Bigint
puts(“”;//换行符
c=a%b;//a模b
c、 print();//打印Bigint
puts(“”;//换行符
/////////////////////////////////
//使用条件运算符//
/////////////////////////////////
如果(a==b)放置(“相等”);//检查相等
else看跌期权(“不相等”);
if(a
使用
自动
。如果您使用的是当前的编译器版本,它们(gcc、clang、vc++、Intel)都实现了这一点。这些工具足够智能,现在可以为您进行优化。另外,您是为32位还是64位构建的?

无符号long-long
应该足够了。如果您尝试了
无符号long-long
但无法存储18位数字,那么您一定是在其他地方溢出了。发布您的代码,也许我们可以找到问题。也许这些是十六进制数字?您的意思是18位而不是*一个18位的整数
*?如果是前者,您可以将数字存储为18个字符的字符串。为什么需要它是一个
int
?您是否需要
int`运算(即:加法、减法等)。一个字符串可以按字典顺序进行比较,并将产生与整数相同的顺序。顺便说一下,它也将适用于有符号的
int64\t
。10位(2^10=1024)略多于3位(10^3=1000);我得到一个错误,说“整型常量对于长类型来说太大”@amian尝试写入
uint64\u t a=23456789123434ull相反,我必须从标准输入中获取一个18位长的输入。我该如何储存它@user2802841@templatetypedef-2^64约为1.8×10^19。你差了10倍。