如何在c中实现程序来计算2 ^ 999?
答案 0 :(得分:4)
您需要使用一个大整数库,它可以处理任意大小的数组。 GMP很受欢迎:http://gmplib.org/。
如果你愿意牺牲精确度,可以使用double
,只需使用pow()
(2 ^ 999 = ~5.4 * 10 ^即可表示高达约1.8 * 10 ^ 308的值) 300)。
答案 1 :(得分:4)
#include <math.h>
double a = pow(2, 999);
答案 2 :(得分:1)
printf("%.0f\n", 0x1p999);
哎呀,太短了所以添加一些随机文本..
答案 3 :(得分:1)
bc
内置了一个bignum库,并且位于每个兼容unix的系统上。
#include <stdio.h>
main() {
FILE *p;
p=popen("bc","w");
fprintf(p, "2^999\n");
fflush(p);
fclose(p);
exit(0);
}
答案 4 :(得分:1)
#include <stdio.h>
#include <string.h>
void mul2(char *n){
int c = 0;
while(*n){
int v;
v = c + (*n - '0') * 2;
c = v / 10;
*n++ = v % 10 + '0';
}
if(c) *n++ = c + '0';
*n = '\0';
}
void print(char *n){
strrev(n);
printf("%s\n", n);
}
int main (void){
char num[302] = "1";
int i;
for(i=0;i<999;i++)
mul2(num);
print(num);
return 0;
}