1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | #include <stdio.h> #include <algorithm> #include <cstring> using namespace std; int numA[1000] = { 0 }; int numB[1000] = { 0 }; int result[1000] = { 0 }; void NumSort(int* arr, int* idx) { for (int i = 0; i <= *idx; i++) { if (arr[i] >= 10) { if (i == *idx) { *idx += 1; } arr[i + 1] += (arr[i] / 10); arr[i] %= 10; } } } int main() { int n,idxA=0,idxB=0,len; scanf("%d", &n); numA[idxA] = 1; numB[idxB] = 3; if (n < 3) { if (n == 1) { printf("1"); return 0; } if (n == 2) { printf("2"); return 0; } } for (int i = 0; i < n-2; i++) { for (int j = 0; j <= idxA; j++) { numA[j] = numA[j] * 2; } NumSort(numA, &idxA); len = max(idxA, idxB); for (int j = 0; j <= len; j++) { result[j] = numA[j] + numB[j]; } NumSort(result, &len); memcpy(numA, numB, 1000 * sizeof(int)); memcpy(numB, result, 1000 * sizeof(int)); idxA = idxB; idxB = len; } for (int i = len; i >= 0; i--) { printf("%d", result[i]); } } | cs |
'알고리즘 > 큰 수의 계산' 카테고리의 다른 글
팩토리얼 숫자 빈도수 더블릿 (0) | 2019.02.28 |
---|---|
팩토리얼 구하기 더블릿 (0) | 2019.02.28 |