簡體   English   中英

總結大數

[英]Summing Large Numbers

我在Project Euler網站上遇到了一些問題並遇到了問題。 問題是,“計算出以下一百個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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM