UVa 10106 - Product
大数加法,代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 const int maxn = 1000; 8 9 struct bign 10 { 11 int d[maxn]; 12 int len; 13 14 bign() 15 { 16 memset(d, 0, sizeof(d)); 17 len = 1; 18 } 19 20 bign(int num) 21 { 22 *this = num; 23 } 24 25 bign(char * num) 26 { 27 *this = num; 28 } 29 30 bign operator = (const char * num) 31 { 32 len = strlen(num); 33 for(int i = 0; i < len; i++) 34 d[i] = num[len-1-i] - '0'; 35 return *this; 36 } 37 38 bign operator = (int num) 39 { 40 char s[maxn]; 41 sprintf(s, "%d", num); 42 *this = s; 43 return *this; 44 } 45 46 bign operator + (const bign& b) const 47 { 48 bign c; 49 c.len = 0; 50 int g = 0; 51 for(int i = 0; i < max(len, b.len) || g; i++) 52 { 53 int x = g; 54 if(i < len) x += d[i]; 55 if(i < b.len) x += b.d[i]; 56 c.d[c.len++] = x % 10; 57 g = x / 10; 58 } 59 return c; 60 } 61 62 bign operator += (const bign& b) 63 { 64 *this = *this + b; 65 return *this; 66 } 67 68 void clean() 69 { 70 while(len > 1 && !d[len-1]) 71 len--; 72 } 73 74 bign operator - (const bign& b) 75 { 76 bign c; 77 c.len = 0; 78 int g = 0; 79 for(int i = 0; i < len; i++) 80 { 81 int x = d[i] - g; 82 if(i < b.len) x -= b.d[i]; 83 if(x >= 0) g = 0; 84 else 85 { 86 g = 1; 87 x += 10; 88 } 89 c.d[c.len++] = x; 90 } 91 c.clean(); 92 return c; 93 } 94 95 bign operator * (const bign& b) 96 { 97 bign c; 98 c.len = len + b.len; 99 for(int i = 0; i < len; i++) 100 for(int j = 0; j < b.len; j++) 101 c.d[i+j] += d[i] * b.d[j]; 102 for(int i = 0; i < c.len-1; i++) 103 { 104 c.d[i+1] += c.d[i] / 10; 105 c.d[i] %= 10; 106 } 107 c.clean(); 108 return c; 109 } 110 111 bool operator < (const bign& b) const 112 { 113 if(len != b.len) return len < b.len; 114 for(int i = len-1; i >= 0; i--) 115 if(d[i] != b.d[i]) return d[i] < b.d[i]; 116 return false; 117 } 118 119 bool operator > (const bign& b) const 120 { 121 return b < *this; 122 } 123 124 bool operator <= (const bign& b) const 125 { 126 return !(b < *this); 127 } 128 129 bool operator >= (const bign& b) const 130 { 131 return !(*this < b); 132 } 133 134 bool operator != (const bign& b) const 135 { 136 return b < *this || *this < b; 137 } 138 139 bool operator == (const bign& b) const 140 { 141 return !(b < *this) && !(b > *this); 142 } 143 144 string str() const 145 { 146 string res = ""; 147 for(int i = 0; i < len; i++) 148 res = (char)(d[i]+'0') + res; 149 if(res == "") res = "0"; 150 return res; 151 } 152 }; 153 154 istream& operator >> (istream &in, bign& x) 155 { 156 string s; 157 in >> s; 158 x = s.c_str(); 159 return in; 160 } 161 162 ostream& operator << (ostream & out, const bign& x) 163 { 164 out << x.str(); 165 return out; 166 } 167 168 int main() 169 { 170 #ifdef LOCAL 171 freopen("in", "r", stdin); 172 #endif 173 bign a, b; 174 while(cin>>a>>b) 175 { 176 bign c = a * b; 177 cout << c << endl; 178 } 179 return 0; 180 }
果然,有个模板就是好啊