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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | #include <iostream> using namespace std; class Bigint { public : typedef unsigned long long ull; private : ull * content[1000]; int index; public : Bigint() { } Bigint(ull _content) : index(0) { content[0] = new ull(_content); } void expand(ull target) { index++; content[index] = new ull(target); } Bigint& operator= (const Bigint& target) { for (int i = 0; i <= index; i++) { delete content[i]; } index = target.index; for (int i = 0; i <= index; i++) { content[i] = new ull(*target.content[i]); } return *this; } Bigint operator* (const int target) { Bigint save(*this); ull s =0; for (int i = 0; i <= index; i++) { *content[i] = *content[i] * target; *content[i] = *content[i] + s; s = (*content[i]) >> 16; *content[i] = *content[i] & 0x0000FFFF; } if (s) { save.expand(s); } return save; } Bigint operator+ (const Bigint& target) { Bigint save(*this); ull s = 0; for (int i = 0; i <= index; i++) { *content[i] = *content[i] + s; if (i <= target.index) { *content[i] = *content[i] + *target.content[i]; } s = (*content[i]) >> 16; *content[i] = *content[i] & 0x0000FFFF; } if (index < target.index) { for (int i = index + 1; i <= target.index; i++) { save.expand(s + *target.content[i]); s = (*content[i]) >> 16; *content[i] = *content[i] & 0x0000FFFF; } } if (s) { save.expand(s); } return save; } bool operator< (const Bigint& target) { if (index < target.index) { return 1; } else if (index == target.index) { if (*content[index] < *target.content[target.index]) { return 1; } else { return 0; } } else { return 0; } } void show() { for (int i = index; i >= 0; i--) { cout << *content[i]; } cout << endl; } ~Bigint() { for (int i = 0; i <= index; i++) { delete content[i]; } } }; int main() { Bigint *solid[4];//0:a, 1:b, 2:전에먹은량, 3:지금 먹은량 int a, b, c; cin >> a >>b; solid[0] = new Bigint(a); solid[1] = new Bigint(b); solid[2] = new Bigint(0); cin >> a >> b >> c; *solid[0] = *solid[0] * a; *solid[1] = *solid[1] * b; solid[0]->show(); delete solid[0]; delete solid[1]; delete solid[2]; return 0; } | cs |
그냥 뭐 여러 이론있던데 제가 생각하기 편한대로 구현해보고 있습니다.
다짯다 생각하고 체크해봤는데 대입 연산자 오버로딩쪽에서 뭔가 문제가 발생하는군여 ㅈㅈ;
int 단위가 더 경제적이지 않을까유?
저도 그렇게 생각해서 처음에 int단위로만 하려고 했었는데
그래도 금방 금방 구현하는구나~ 멋지다~
인트를 벗어나는 연산인지 아닌지 계산하기 전에 판단하는법을 몰라서 그랬다고 할까요..
항상 곱하기가 필요하다는 법은 없으니까 곱할때만 바꾸는게 나을것 같은디 말이쥬.
항상 인트를 다 채울 필요는 없쥬.
그거 비교계산할때만 long long으로 바꿔서 계산하고 다시 int로 바꾸는 방법도 있겠네요
넵넵 그러네영
빅인티저 구현한사람 보니까 저같인 1000개 한정이 아니라 무한한정인거 같던데 그렇게 되면 매번 재할당 해줘야될꺼 같아서 이문제는 1000개로 충분하지 싶어서 1000개만 잡았어요
어쨋든 지금 문제는 뻑이난다는것 ... ㅜㅜ
vector 를 쓰시는게 현명할듯 아뢰오...
만약 두개의 빅 인트를 쓰시면, 4000 바이트 이상의 간격을 유지한단 소리니깐 캐시효율이 L2 급 이상에서나 먹게 되는
재할당을 막는대신 가장 불리한 크기에서 출발하는거니깐유. 별로 수를 표현하는 자료형으로 적합하지 않을듯.
아 백터.. 왜 백터를 쓸생각을 못했을까요 ㅋㅋ
근데 포인터만 만들어 놔도 4000바이트 그런식으로 먹게되는건가요
32비트 운영체재에서는 주소도 4바이트 이던가 음 ..
저건 포인터 4000개를 만든거니깐유. 당연히. 64비트에선 8000바이트쥬.
@_@
자릿수를 표현하고 싶었다면 index 보단 degree 가 적당할줄 아뢰오...