題目:兩個大數相乘。
大數相乘(2)見此。
方法1:將兩個大數分別用陣列儲存,模擬乘法運算,將其中乙個陣列的每一位分別與令乙個大數相乘,結果相加。
**見後面**部分的string multiply1(bignumber &bignumber2)。
方法2:同樣是上面的思路,不同的是儲存過程直接用乙個二維陣列來訪問每一位相乘的結果,然後處理相加的過程。
**見後面**部分的string multiply2(bignumber &bignumber2)。
#include #include #include using namespace std;
class bignumber
} return true;
}public:
explicit bignumber(const char tocopy)
explicit bignumber(const bignumber &tocopy)
string showvalue()
bool showcheck()
string multiply1(bignumber &bignumber2)
for(count = len2; count > 0; --count)
int *convertpointer = new int[len1 + len2];
memset(convertpointer, 0, (len1 + len2) * sizeof(int));
for(count = len2; count > 0; --count)
}for(count = len1 + len2; count > 0; --count)
}char *resultchar = new char[len1 + len2];
bool checkresultzero = false;
int countresultzero = 0;
for(count = 0; count < len1 + len2; ++count)
}else
}resultchar[countresultzero] = '\0';
string result = resultchar;
delete no1;
delete no2;
delete convertpointer;
delete resultchar;
return result;
} string multiply2(bignumber &bignumber2)
}int *convertpointer = new int[len1 + len2];
memset(convertpointer, 0, sizeof(int) * (len1 + len2));
int count = 0;
for(count = len1 + len2; count > 0; --count)
if(convertpointer[count - 1] > 9)
}char *resultchar = new char[len1 + len2];
bool checkresultzero = false;
int countresultzero = 0;
for(count = 0; count < len1 + len2; ++count)
}else
}resultchar[countresultzero] = '\0';
string result = resultchar;
for(countcolumn = 0; countcolumn < len1 + 1; ++countcolumn)
delete convertcolumn;
delete convertpointer;
delete resultchar;
return result;
}};int main()
大數相乘 大數相加
大數相乘 大數相乘 1 include includeint main int ans i while lb 0 int cnt j for i 0 i 0 i if e i break 去除前面多餘的0 if i 1 else 當結果是0的時候 printf 0 printf n return 0...
大數相乘演算法
啥也說,直接給 高精度的乘法,乘數的位數沒有限制,小數點後面的位數也沒有限制 輸入引數 乘數 str1,str2 輸出引數 str str1 str2 返回值型別 string string large mult string str1,string str2,string str 輸出引數 ass...
c 大數相乘
using system using system.collections.generic using system.text namespace numbers if ch1.length 0 return true return true public static string getmuti...