对大数字求和
Summing Large Numbers
我在欧拉项目网站上遇到了一些问题,遇到了一个问题。问题问:"计算出以下一百个 50 位数字之和的前十位数字。我猜有一些数学方法可以解决这个问题,但我只是想知道这么大的数字是如何求和的?我将数字存储为字符串并将每个数字转换为长整型数字,但数字太大,总和不起作用。
有没有办法将非常大的数字保存为变量(不是字符串)?我不希望代码出现问题,因为我想自己解决这个问题。
我只是想知道这么大的数字是如何相加的?
您可以使用数组:
long LargeNumber[5] = { < first_10_digits>, < next_10_digits>....< last_10_digits> };
现在您可以计算 2 个大数的总和:
long tempSum = 0;
int carry = 0;
long sum[5] = {0,0,0,0,0};
for(int i = 4; i >= 0; i--)
{
tempSum = largeNum1[i] + largeNum2[i] + carry; //sum of 10 digits
if( i == 0)
sum[i] = tempSum; //No carry in case of most significant digit
else
sum[i] = tempSum % 1000000000; //Extra digits to be 'carried over'
carry = tempSum/1000000000;
}
for( int i = 0; i < 5; i++ )
cout<<setw(10)<<setfill('0')<<sum[i]<<"n"; //Pad with '0' on the left if needed
有没有办法将非常大的数字保存为变量(这不是 字符串)?
这没有原语,您可以使用任何数据结构(数组/队列/链接列表)并适当地处理它
我猜有一些数学方法可以解决这个问题
答案是肯定的!但
我不希望代码出现问题,因为我想自己解决这个问题。
您可以将数字存储在数组中。为了节省空间并提高操作性能,请将数字的数字以 10^9 为基数存储。所以一个数字182983198432847829347802092190将在数组中表示为以下内容
arr[0]=2092190 arr[1]=78293478 arr[2]=19843284 arr[3]=182983
为了清楚起见,该数字表示为 arr[i]*(10^9i) 的总和现在从 i=0 开始,开始按照你小时候学习的方式添加数字。
我在java中做过,在这里我采用数字N1和N2,并且我创建了一个大小为1000的数组。让我们举个例子如何解决这个问题,N1=12,N2=1234。对于 N1=12,temp=N1%10=2,现在从右到左添加带有数字 N2 的这个数字,并将结果存储到从 i=0 开始的数组中,同样适用于 N1 的其余数字。数组将存储结果,但顺序相反。看看这个链接。 请查看此链接 http://ideone.com/V5knEd
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception {
Scanner scan=new Scanner(System.in);
int A=scan.nextInt();
int B=scan.nextInt();
int [] array=new int[1000];
Arrays.fill(array,0);
int size=add(A,B,array);
for(int i=size-1;i>=0;i--){
System.out.print(array[i]);
}
}
public static int add(int A, int B, int [] array){
int carry=0;
int i=0;
while(A>0 || B>0){
int sum=A%10+B%10+carry+array[i];
array[i]=sum%10;
carry=sum/10;
A=A/10;
B=B/10;
i++;
}
while(carry>0){
array[i]=array[i]+carry%10;
carry=carry/10;
i++;
}
return i;
}
}
#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
struct grid{
int num[50];
};
int main()
{
struct grid obj[100];
char ch;
ifstream myfile ("numbers.txt");
if (myfile.is_open())
{
for(int r=0; r<100; r++)
{
for(int c=0; c<50; c++)
{
myfile >> ch;
obj[r].num[c] = ch - '0';
}
}
myfile.close();
int result[50],temp_sum = 0;
for (int c = 49; c>=0; c--)
{
for (int r=0; r<100; r++)
{
temp_sum += obj[r].num[c];
}
result[c] = temp_sum%10;
temp_sum = temp_sum/10;
}
string temp;
ostringstream convert;
convert << temp_sum;
temp = convert.str();
cout << temp_sum;
for(unsigned int count = 0; count < (10 - temp.length()); count++)
{
cout << result[count];
}
cout << endl;
}
return 0;
}
您的时间和内存大小的最佳方法:D
#include <iostream >
#include <climits >
using namespace std;
int main()
{
unsigned long long z;
cin >>z;
z=z*(z+1)/2;
C out << z;
return 0;
}
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 如何使用循环对所有数字求和?
- 循环中的线程 Runnin 用于对 c++ 中的数字求和
- 找到所有与自己求和的数字X的快速方法,去掉一个数字得到N
- 使用特征类对向量中的某些数字求和
- C++对以前打印的列表中的数字求和
- 一个简单的程序,对数字进行计数和求和.我怎样才能让它工作
- C++简介:将字符串从文件行中的数字中分离出来,并对这些数字求和
- 求和数字递归C++
- 对大数字求和
- C++,要求用户输入一组数字求和(未知的输入数量),但是数字不会加起来
- 求和巨大的数字
- 如何在c++中对数组的一行中的数字求和
- 编写一个函数从文件中读取数字并求和
- 将特定数字求和以获得100的不同方法
- 数字不断求和,但不会停止求和