大数运算(乘法)
代码
#include#include #include using namespace std; string num1, num2; int sum[1000], ans[1000]; void chen(string n1, string n2){ int cnt = 0; cnt = n1.length() + n2.length() - 2; for(int i = 0; i < n1.length(); i++) for(int j = 0; j < n2.length(); j++){ sum[i + j] += (n1[i] - '0') * (n2[j] - '0'); } for(int i = cnt; i >= 0; i--) ans[cnt - i] = sum[i]; for(int i = 0; i <= cnt+1; i++){ if(ans[i] >= 10){ ans[i+1] += ans[i] / 10; ans[i] %= 10; } } int i; for(i = cnt + 1; i >= 0; i--) if(ans[i] != 0) break; for( ; i >= 0; i--) cout << ans[i]; cout << endl; } int main(){ cin >> num1 >> num2; chen(num1, num2); }
Comments | NOTHING