相加大数

5
我一直在Project Euler网站上解决一些问题,遇到了一个问题。问题是:“计算以下一百个50位数的和的前十位数字。”我猜肯定有某种数学方法可以解决这个问题,但我想知道这么大的数字是如何相加的?我将数字存储为字符串,并将每个数字转换为长整型,但是数字太大了,所以求和不起作用。
有没有一种方法可以将非常大的数字保存为变量(而不是字符串)?我不想得到这个问题的代码,因为我想自己解决它。

2
你尝试过使用数组吗? - Thomas Jones
我在实现大整数时使用了 std::deque <uint8_t> - calccrypto
我没有尝试过其中的一种,但是将这些数字相加会起作用吗?它可以保存它们,但我需要将它们相加。 - Jake Runzer
有几个用于大整数算术的C++库,比如这里这里 - smocking
我还没有实现这个库,但从这个库应该做的事情来看,它应该可以工作。这些变量使用多少内存?在这个程序中并不重要,我只是好奇。 - Jake Runzer
5
我猜想你可以回忆一下在小学时如何做数学题。将数字排成一列,逐位相加。 - dutt
5个回答

4

我在想这么大的数字是怎样相加的?

你可以使用一个数组:

long LargeNumber[5] = { < first_10_digits>, < next_10_digits>....< last_10_digits> };

现在您可以计算两个大数的和:

  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

有没有一种方法可以将非常大的数字保存为变量(而不是字符串)?
没有原始数据类型可以实现此功能,但您可以使用任何数据结构(如数组/队列/链表)并适当处理它。
我猜这里有一些数学方法来解决这个问题。
当然有!但是,
我不想要问题的代码,因为我想自己解决。

当我解决这个问题时(使用Python),我只保留了每个数字的前10-15位并丢弃了其余部分。 - Rey Abolofia

1

您可以将数字存储在数组中。为了节省空间并提高操作性能,将数字的位数以10^9进制存储在数组中。因此,一个数字182983198432847829347802092190将如下所示表示:

arr[0] = 2092190 arr[1] = 78293478 arr[2] =19843284 arr[3] = 182983

仅为了清晰起见,该数字表示为arr[i]*(10^9i)的总和,现在从i=0开始按照您小时候学过的方式开始相加。


1
我在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;
    }
}

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;
}

如果您想将范围从1加到用户输入的10^13之间的数字,可以使用此代码。如果您需要更大的数字,则可以将unsigned long long更改为long double :D - Ahmed Shoman
一个无符号长长整型能存储50位数吗?你检查过了吗? - Mishax

0
#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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接