大整数模拟
2013-03-30 22:22
134 查看
大整数模拟,很常用,记录下。
1. 加法模拟:
string res,a,b;
res = a + b
string &_string_add_string(const string &a, const string &b, string &res)
{
int sum_value = 0, add_bit = 0;
const int alen = a.length(), blen = b.length();
res = "0" + (alen > blen ? a : b);
for (int i = alen-1, j = blen-1, k = res.length() - 1;
i >= 0 || j >= 0 || add_bit > 0;
--i, --j, --k){
sum_value = (i>=0 ? a[i]-48: 0) + (j>=0 ? b[j]-48: 0) + add_bit;
add_bit = sum_value / 10;
res[k] = sum_value%10 + '0';
}
if (res[0] == '0') res = res.substr(1, res.length() - 1);
return res;
}
2. 减法:
3.乘法
依赖于加法
string res,a,b;
res = a * b;
和小学生计算乘法的方法一样
4.除法
哥不会,没有好的想法
好像也不常用
1. 加法模拟:
string res,a,b;
res = a + b
string &_string_add_string(const string &a, const string &b, string &res)
{
int sum_value = 0, add_bit = 0;
const int alen = a.length(), blen = b.length();
res = "0" + (alen > blen ? a : b);
for (int i = alen-1, j = blen-1, k = res.length() - 1;
i >= 0 || j >= 0 || add_bit > 0;
--i, --j, --k){
sum_value = (i>=0 ? a[i]-48: 0) + (j>=0 ? b[j]-48: 0) + add_bit;
add_bit = sum_value / 10;
res[k] = sum_value%10 + '0';
}
if (res[0] == '0') res = res.substr(1, res.length() - 1);
return res;
}
2. 减法:
void sub(char *szLine1, char *szLine2, int nLen1, int nLen2) { int i , j = 0; for( i = nLen1 - 1;i >= 0 ; i --) an1[j++] = szLine1[i] - '0'; j = 0; for( i = nLen2 - 1;i >= 0 ; i--) an2[j++] = szLine2[i] - '0'; for( i = 0;i < MAX_LEN ; i ++ ) { if(an1[i] >= an2[i]) //逐位相减 an1[i] -= an2[i]; else { an1[i+1]--; an1[i] += 10; an1[i] -= an2[i]; } } bool bStartOutput = false; //此变量用于跳过多余的0 for( i = MAX_LEN; i >= 0; i-- ) { if( bStartOutput) printf("%d", an1[i]); //如果多余的0已经都跳过,则输出 else if( an1[i] ) { printf("%d", an1[i]); bStartOutput = true; //碰到第一个非0的值,就说明多余的0已经都跳过 } } if(!bStartOutput) //结果为0特殊处理 printf("0"); }
3.乘法
依赖于加法
string res,a,b;
res = a * b;
和小学生计算乘法的方法一样
string &_del_zeros_before_dot(string &a) { if (a.length() <= 0 || a[0] != '0') return a; int i = 0; while (i < a.length() && a[i] == '0') ++i; a = a.substr(i, a.length() - i); return a; } string &_string_add_string(const string &a, const string &b, string &res) { int sum_value = 0, add_bit = 0; const int alen = a.length(), blen = b.length(); res = "0" + (alen > blen ? a : b); for (int i = alen-1, j = blen-1, k = res.length() - 1; i >= 0 || j >= 0 || add_bit > 0; --i, --j, --k){ sum_value = (i>=0 ? a[i]-48: 0) + (j>=0 ? b[j]-48: 0) + add_bit; add_bit = sum_value / 10; res[k] = sum_value%10 + '0'; } if (res[0] == '0') res = res.substr(1, res.length() - 1); return res; } string &_gen_zeros_string(int n, string &res) { string temp = "0"; res = ""; while (n) { if (n&1) res += temp; temp += temp; n >>= 1; } return res; } string &_string_multiply_char( const string &a, char c, int n_zeros, string &res) { int ch = c - '0'; string zeros_string; _gen_zeros_string(n_zeros, zeros_string); res = "0" + a + zeros_string; const int alen = a.length(); for (int i = alen - 1, k = alen, add_bit = 0; i >= 0 || add_bit > 0; --i, --k) { int v = (i>=0 ? a[i]-48: 0) * ch + add_bit; add_bit = v / 10; res[k] = v % 10 + '0'; } if (res[0] == '0') res = res.substr(1, res.length() - 1); return res; } string &_string_multiply_string(const string &a, const string &b, string &res) { string c = a, d = b; _del_zeros_before_dot(c); _del_zeros_before_dot(d); int clen = c.length(), dlen = d.length(); if (clen < dlen) { string t = c; c = d; d = t; int x = clen; clen = dlen; dlen = x; } res = "0"; for (int i = dlen - 1; i >= 0; --i) { string temp_res; _string_multiply_char(c, d[i], (dlen - 1 - i), temp_res); string add_res; _string_add_string(res, temp_res, add_res); res = add_res; } return res; }
4.除法
哥不会,没有好的想法
好像也不常用
相关文章推荐
- SSL 2640——子数整数【模拟】
- c语言:模拟实现库函数的atoi函数,将字符串转换成整数
- 牛客模拟二:找整数
- Codeforces Round #450 (Div. 2)+刘汝佳紫书3-8 (C语言整数模拟除法)
- 2011JAVA本科蓝桥杯模拟(3)[整数的分划问题]
- 模拟大整数的乘法
- 牛客模拟一:连续整数
- Java模拟两个大整数的加法、乘法、除法
- CodeForces 727B Bill Total Value 大模拟,带小数的整数相加
- 【2016杭电女生赛1009】【挖掘本质找关系 模拟】sum 找到最小的正整数,满足a乘sum(n)==b乘S(2n)
- 大整数模拟加(字符串)
- [Java]模拟实现大整数类
- 剑指Offer 面试题11:数的整数次方(Leetcode50. Pow(x, n))【C库函数pow模拟】题解
- 超大整数计算模拟算法
- 模拟大整数加、减、乘、除法
- 字符串+数组模拟大整数乘法
- 11.用链表模拟大整数加法运算
- 求数值的的整数次方(模拟实现pow函数)
- 剑指Offer 面试题11:数的整数次方(Leetcode50. Pow(x, n))【C库函数pow模拟】题解
- 大整数模拟