简体   繁体   English

我如何计算一个非常大的数字,如 c 中的 (1000 digits),并使用数组打印出来

[英]How can I compute a very big digit number like (1000 digits ) in c , and print it out using array

how to print a number in C with 1000 digits same as 3 1000 ?如何在 C 中打印一个 1000 位数字与 3 1000相同的数字? using arr[1000] , every position in the arr gets a digit.使用arr[1000]arr中的每个 position 都会得到一个数字。

void main() {
    unsigned int  Base, Power;
    int Digit[1000] = { 0 }, i;
    Base = 3;
    Power = 1000;
}

Here is a very simple example to get you started.这是一个非常简单的示例,可以帮助您入门。 This is for addition (not multiplication or exponentiation), and it's for 3-digit numbers, not thousands.这是用于加法(不是乘法或求幂),它用于 3 位数,而不是千位。 But it demonstrates the basic idea of holding each digit in one element of an array.但它演示了将每个数字保存在数组的一个元素中的基本思想。 When you add (or subtract, or multiply) numbers like these in a computer program, you can use exactly the same techniques you learned for doing arithmetic in school.当你在计算机程序中加(或减,或乘)这样的数字时,你可以使用你在学校做算术时所学的完全相同的技巧。

#include <stdio.h>

int main()
{
    int a[10], b[10], c[10];

    a[2] = 4; a[1] = 5; a[0] = 6;   /* 456 */
    b[2] = 7; b[1] = 8; b[0] = 9;   /* 789 */

    int partialsum = a[0] + b[0];
    c[0] = partialsum % 10;
    int carry = partialsum / 10;

    partialsum = a[1] + b[1] + carry;
    c[1] = partialsum % 10;
    carry = partialsum / 10;

    partialsum = a[2] + b[2] + carry;
    c[2] = partialsum % 10;
    carry = partialsum / 10;

    c[3] = carry;

    printf("%d%d%d%d\n", c[3], c[2], c[1], c[0]);
}

The biggest limitation in this program is that it's hardwired to work with 3-digit numbers and a 4-digit sum.该程序的最大限制是它只能使用 3 位数字和 4 位总和。 The first improvement you might like to try to make would be to keep count (perhaps in additional variables) of the actual number of digits in each number.您可能想要尝试进行的第一个改进是保持对每个数字中实际数字位数的计数(可能在其他变量中)。

See also this question and its answer, and its linked duplicates.另请参阅此问题及其答案及其链接的副本。

Assume Digit as an array (it is an array) (instead of a number), and then print those single digits using a loop.假设Digit是一个数组(它是一个数组)(而不是一个数字),然后使用循环打印这些单个数字。

As Digit is stored on stack memory, you can use sizeof() operator.由于Digit存储在堆栈 memory 中,您可以使用sizeof()运算符。

int length = sizeof(Digit) / sizeof(Digit[0]);
for(int i = 0; i < length; i++)
{
    printf("%d", Digit[i]);
}

Also, avoid void main() { } , use int main() { } .另外,避免void main() { } ,使用int main() { }

C does not have a standard multi-precision integer package. You can implement a brute force approach this way: C 没有标准的多精度 integer package。您可以通过这种方式实现蛮力方法:

#include <stdio.h>

int multiply(char *num, int max, int p, int n) {
    int i, carry = 0;
    for (i = max; i > p;) {
        carry += (num[--i] - '0') * n;
        num[i] = '0' + carry % 10;
        carry /= 10;
    }
    while (carry) {
        num[--i] = '0' + carry % 10;
        carry /= 10;
    }
    return i;
}

#define N        3
#define POWER    1000
#define NUMSIZE  1000

int main() {
    char num[NUMSIZE];
    int p = NUMSIZE;
    num[--p] = '\0';
    num[--p] = '1';

    for (int i = 0; i < POWER; i++) {
        p = multiply(num, NUMSIZE - 1, p, N);
    }
    printf("%d^%d = %s\n", N, POWER, num + p);
    return 0;
}

Output: Output:

3^1000 = 1322070819480806636890455259752144365965422032752148167664920368226828597346704899540778313850608061963909777696872582355950954582100618911865342725257953674027620225198320803878014774228964841274390400117588618041128947815623094438061566173054086674490506178125480344405547054397038895817465368254916136220830268563778582290228416398307887896918556404084898937609373242171846359938695516765018940588109060426089671438864102814350385648747165832010614366132173102768902855220001

3 1000 only has 478 digits, for 1000 digits, you need 3 2094 or 3 2095 . 3 1000只有 478 位,1000 位需要 3 2094或 3 2095

Your question consists of two parts:你的问题由两部分组成:

  1. How to compute the result and write it to an array?如何计算结果并将其写入数组?
  2. How to print the result array?如何打印结果数组?

The first half of the question has already been answered in the answers provided by other people.问题的前半部分已经在其他人提供的答案中得到了回答。 This answer provides a solution on how to print the result array, assuming that the array's elements are of type int (as they are in the question). This answer提供了一个关于如何打印结果数组的解决方案,假设数组的元素是int类型(因为它们在问题中)。

If every int element in the array is a number between 0 and 9 which represents a digit, then you can use the following function for printing the array:如果数组中的每个int元素都是09之间的数字,表示一个数字,那么您可以使用以下 function 打印数组:

void print_digits( const int array[], int array_size )
{
    int i = 0;

    //skip leading zeros, but don't skip last zero
    for ( ; i < array_size - 1; i++ )
    {
        assert( 0 <= array[i] && array[i] <= 9 );
        if ( array[i] != 0 )
            break;
    }

    //print all remaining digits
    for ( ; i < array_size; i++ )
    {
        assert( 0 <= array[i] && array[i] <= 9 );
        printf( "%d", array[i] );
    }

    printf( "\n" );
}

Here is a demonstration program:这是一个演示程序:

#include <stdio.h>
#include <assert.h>

void print_digits( const int array[], int array_size )
{
    int i = 0;

    //skip leading zeros, but don't skip last zero
    for ( ; i < array_size - 1; i++ )
    {
        assert( 0 <= array[i] && array[i] <= 9 );
        if ( array[i] != 0 )
            break;
    }

    //print all remaining digits
    for ( ; i < array_size; i++ )
    {
        assert( 0 <= array[i] && array[i] <= 9 );
        printf( "%d", array[i] );
    }

    printf( "\n" );
}

int main( void )
{
    int digits[100] = {0};

    //set the number to 3.4 E+60
    digits[39] = 3;
    digits[40] = 4;

    print_digits( digits, sizeof digits / sizeof *digits );
}

This program has the following output:该程序具有以下 output:

3400000000000000000000000000000000000000000000000000000000000

Note that it is a bit of a waste of memory to use a 32-bit int to store a single digit between 0 and 9 .注意 memory 用一个 32 位的int来存储09之间的单个数字有点浪费。 Therefore, it would be less wasteful to use char instead, which is guaranteed to be able to represent numbers up to 127 (which is sufficient, because we only need to be able to represent numbers up to 9 ).因此,使用char来代替浪费会更少,它保证能够表示最多127个数字(这就足够了,因为我们只需要能够表示最多9数字)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 arrays 打印 10 位大数字的反转? - how can i print reverse of a big 10 digit number by using arrays? 如何在 C 中定义非常大的二维数组(如 1000*1000) - How define very large 2D array in C (like 1000*1000) 如何将数字转换为数字数组? - How can I convert a number into an array of digits? 如何为数字的数字数组(int)分配内存,其长度等于该数字的位数? - How can I allocate memory for an array of digits (int) of a number with the length being equal to the number of digits of that number? 如何打印像网格一样的数组? - How can I print an array like a grid? 如何计算大小为 1000 x 1000 的二维数组中 2 个元素之间的步幅? C++ - How can I calculate the stride between 2 elements in a 2D array of size 1000 by 1000? C++ 用5位数字的数字填充长度为5的数组 - Fill array of length 5 with the digits of a 5 digit number 如何将数字切成较小的数字以适合我的数组 - How can I slice a number into smaller digits to fit my array 如何通过使用XOR运算符输入2个或更多数字来解决这个C ++问题 - How can I do this C++ question by using XOR operator for 2 or more digits number 我正在尝试反转 C 中的数组。 但是,代码将随机 1-2 抛出到一些非常大的数字 - I am trying to reverse an array in C. But, the code throws out random 1-2 to some very large numbers digit numbers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM