计算长整型的总和
Calculation the sum of long long int
(计算长长数之和)
每当我输入一个长长的数字时,程序总是返回 -1 或 -2,有人知道为什么吗?
#include <iostream>
#include <math.h>
#include <string>
#include<iomanip>
using namespace std;
int main()
{
int n,i;
long long x1,x2,s;
do{
cin>>n;
}while(n<1);
long long t1[n];
long long t2[n];
string ch1,ch2;
i=0;
do{
do{
scanf("%lld %lld", &x1, &x2);
ch1=to_string(x1);
ch2=to_string(x2);
}while((ch1.length()>pow(10,5)) || (ch2.length()>pow(10,5)));
t1[i]=x1;
t2[i]=x2;
i++;
}while(i<n);
for(i=0;i<n;i++){
s=t1[i]+t2[i];
cout<<s<<endl;
}
return 0;
}
找不到解决方案,请帮忙! 提前感谢!
示例输入:
1
14444444444444444111115556
55656464684646464646647676
输出
-阿拉伯数字
//编辑:
有没有办法计算100000:)的总和十进制数字,如使用标准库的示例中的数字?
基本积分类型(如 long long
)可以表示的范围有限。如果计算导致的结果大于可以表示的结果,它将溢出。有符号整数溢出在C++中具有未定义的行为。
通过使用整数数组来表示数字的不同部分,可以表示无限范围的数字(理论上,内存限制了实践)。这种技术称为任意精度算术。
C++标准库中没有任意精度类型的实现,因此要么必须自己实现一个,也可以使用其他人实现的库。
相关文章:
- C 字符串返回字符串的整数/双精度/长整型值
- 无法在 Arduino 中uint8_t数组转换为无符号长整型数组
- JNI 日期值转换问题,在C++中获取不同的长整型值
- 将长整型值打印为带有前导零的十六进制
- 为什么在传递长整型时调用具有两个双精度类型的参数的重载函数?
- 提升不良词法强制转换:将字符串转换为无符号长整型时,无法将源类型值解释为目标
- 将最小值整数转换为无符号长整型
- 如何将小端格式的QByteArray转换为无符号长整型
- 无符号长整型和无符号 int 之间有什么区别,这 2 种类型应该如何在 c# 中封送?
- 环礁和(长整型)的区别?
- 将逗号格式化为长整型整数
- 如何将长整型传递给 itoa 函数?
- 是否存在将长整型转换为指针有效的情况
- 在 C++ 中创建一对长整型和矢量时出现编译错误
- 当我使用长整型时,我的代码不起作用,它与 int 一起工作得很好
- 将字符串中以十六进制形式存储的负长整型值转换为 C++ 中的长整型变量
- 为什么当我们把变量"u"的数据类型从长整型更改为整型时,模幂会出错?
- 错误:请求从 std::chrono::time_point 浮点数转换为非标量类型长整型
- 如何以优雅高效的方式将无符号/有符号整数/长整型转换为 C 字符串?
- 计算长整型的总和