【发布时间】:2015-11-26 21:40:11
【问题描述】:
我正在用 c 编写一个程序来存储 2^100000,并且我正在使用数组来存储结果。 完整代码如下:
#include <stdio.h>
#include <math.h>
int main()
{
int test, n, i, j, x, resul;
int a[200], m, temp;
scanf("%d", &test);
for (i = 0; i < test; i++) {
a[0] = 3; // initializes array with only 1 digit, the digit 1.
m = 1; // initializes digit counter
scanf("%d", &n);
temp = 0; // Initializes carry variable to 0.
for (i = 1; i < n; i++) {
for (j = 0; j < m; j++) {
x = a[j] * 2 + temp; //x contains the digit by digit product
a[j] = x % 10; //Contains the digit to store in position j
temp = x / 10; //Contains the carry value that will be stored on later indexes
}
while (temp > 0) { //while loop that will store the carry value on array.
a[m] = temp % 10;
temp = temp / 10;
m++; // increments digit counter
}
}
for (i = m - 1; i >= 0; i--) //printing answer
printf("%d", a[i]);
}
return 0;
}
有人能告诉我一种更有效的方法来降低时间复杂度吗?
【问题讨论】:
-
这只是一小部分代码,将整个函数与变量定义和调用代码一起发布。
-
我已根据您的要求添加了整个代码。
-
我提供了一个详尽的答案,有帮助吗?
标签: c