High-digit exponentiation

Calculate 7 to the 34th power

【analysis】

Calculation-the nth power of a number can be realized by a simple one-fold loop, but the 34th power of 7 is a very large number, which exceeds the range of the computer. This is actually a big integer storage problem, which can be stored using arrays.

To calculate 7 to the 34th power, store 1 in the array a, multiply it by 7, and loop 34 times. The value of array a is 7 to the 34th power. Any integer (assuming that the number of digits is n) is multiplied by 7, and the number of digits of the result will not exceed n+1. From the lowest digit to the highest digit, multiply each digit by 7 in turn. If there is a carry, store the carry in the temporary variable C, and store the remainder in the current position. When multiplying the next digit by 7, you also need to add the carry c to the multiplication result at this time.

code:
 

#include<stdio.h>
void main()
{
	static int a[34];
	int i, j, k, c = 0;
	a[0] = 1;
	k = 0;
	printf("7的34次方是: ");
	for (i = 1; i <= 34; i++)
	{
		for (j = 0; j <= k; j++)
		{
			a[j] = a[j] * 7 + c;
			c = a[j] / 10;
			a[j] %= 10;
		}
		if (c)
		{
			k++;
			a[k] = c;
			c = 0;
		}
	}
	for (; k >= 0; k--)
		printf("%d", a[k]);
	printf("\n");
	getchar();
}

result:

 

Guess you like

Origin blog.csdn.net/baidu_36669549/article/details/104160411