[英]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:你的问题由两部分组成:
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
元素都是0
到9
之间的数字,表示一个数字,那么您可以使用以下 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
来存储0
到9
之间的单个数字有点浪费。 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.