如题,用大数乘法很简单,但在别处看到一个用100000进制来计算的代码。如下:
1 #include <vector> 2 #include <stdio.h> 3 int main() 4 { 5 std::vector<int> bigNumber(400); 6 bigNumber[0] = 1; 7 for (int i = 0; i < 4000; i++) { 8 for (int j = 0; j < 400; j++) { 9 bigNumber[j] *= 2; 10 } 11 for (int j = 0; j < 399; j++) { 12 bigNumber[j + 1] += bigNumber[j] / 100000; // 100000进制 13 bigNumber[j] %= 100000; 14 } 15 } 16 bool willOutput = false; 17 int totalNum = 0; 18 for (int j = 399; j >= 0; j--) { 19 if (bigNumber[j] != 0 || willOutput) { 20 totalNum += 5; 21 printf(willOutput ? "%05d" : "%d", bigNumber[j]); 22 willOutput = true; 23 } 24 } 25 printf("\n%d\n", totalNum); 26 return 0; 27 }
时间: 2024-10-10 06:54:43