import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); BigInteger bi1 = new BigInteger(scanner.next()); BigInteger bi2 = new BigInteger(scanner.next()); System.out.println(bi1.multiply(bi2)); } }
def mul(num1,num2): temp = [i*num1 for i in num2] res = '' i = 1 number = temp[0] while i <len(temp): res += str(number%10) number //= 10 number += temp[i] i += 1 res += str(number)[::-1] return res[::-1] if __name__=='__main__': num1 = int(input()) str2 = [int(i) for i in input()][::-1] print(mul(num1,str2))
#include <stdio.h> #include <iostream> #include <string> using namespace std; //字符串转整型 int str_to_int(string a) { int int_num; int_num=a[0]-'0'; return int_num; } //整型转string string int_to_str(int a) { string str_num; str_num=a+48; return str_num; } //大整数相乘 string multi(string num1,string num2) { string result; int res=0,res_carry=0,size=0; int num1_size=num1.size(); int num2_size=num2.size(); //相乘之后最长为size size=num1_size+num2_size; for(int j=num2_size-1,count=1;j>=0;j--,count++) { if(count>=2) result.insert((count-1)*size+0,count-1,'0'); for(int i=num1_size-1;i>=0;i--) { int med_num1,med_num2; string result_num; //转化每位的数字为整型 med_num1=str_to_int(&num1[i]); med_num2=str_to_int(&num2[j]); //计算相乘结果,包括进位 res=med_num1*med_num2+res_carry; if(res>9) { res_carry=res/10; //进位数 res = res%10; //保留低位 result_num = int_to_str(res); result.append(result_num); } else { res_carry=0; //进位数为0 result_num = int_to_str(res); result.append(result_num); } } if(res_carry!=0) { string final_carry=int_to_str(res_carry); result.append(final_carry); res_carry=0;//清空进位,进行下一位的乘法 } //每次运算之后size大小对齐 while(result.size()<(count*size)) result.append("0"); } //进行加法运算 string str_sum,final_result; int sum=0,sum_carry=0; int med_sum=0; for(int p=0;p<size;p++) { for(int count1=0;count1<num2_size;count1++) { med_sum =str_to_int(&result[p+(count1*size)]); sum = sum + med_sum; } sum = sum+sum_carry; if(sum>9) { sum_carry=sum/10; //进位数 sum = sum%10; //保留低位 str_sum = int_to_str(sum); final_result.append(str_sum); sum=0; } else { sum_carry=0; //进位数为0 str_sum = int_to_str(sum); final_result.append(str_sum); sum=0; } } if(sum_carry!=0) { string final_sum_carry=int_to_str(sum_carry); final_result.replace(size,1,final_sum_carry); //final_result.append(final_sum_carry); } return final_result; } int main() { string num1,num2,result; cin>>num1>>num2; result = multi(num1,num2); while(result[result.size()-1]=='0') result.erase(result.size()-1); for(int i=result.size()-1;i>=0;i--) cout<<result[i]; }
//使用大数加法和大数乘法,模拟真实的乘法计算 #include<iostream> (720)#include<string> using namespace std; //大数加法 string BigIntAdd(string s1,string s2){ int i = s1.size() - 1; int j = s2.size() - 1; char temp; int carry = 0; string str = ""; while(i>=0 && j>=0){ int num = s1[i] - '0' + s2[j] - '0' + carry; carry = num/10; num %= 10; temp = num + '0'; str = temp + str; i--; j--; } while(i>=0){ int num = s1[i] - '0' + carry; carry = num/10; num %= 10; temp = num + '0'; str = temp + str; i--; } while(j>=0){ int num = s2[j] - '0' + carry; carry = num/10; num %= 10; temp = num + '0'; str = temp + str; j--; } if(carry == 1) str = '1' + str; return str; } //大数乘法 string BigIntMul(string s1,int d){ int i = s1.size() - 1; int carry = 0; char temp; int num = 0; string str = ""; while(i >= 0){ num = (s1[i] - '0') * d + carry; carry = num/10; num %= 10; temp = num + '0'; str = temp + str; i--; } if(carry > 0){ char c = carry + '0'; str = c + str; } return str; } int main(){ string s1 = ""; string s2 = ""; cin >> s1; cin >> s2; string str = ""; string longstr = ""; string shortstr = ""; string zero = ""; if(s1.size()>s2.size()){ longstr = s1; shortstr = s2; } else{ longstr = s2; shortstr = s1; } for(int i = shortstr.size() - 1;i >= 0;i--){ string s = BigIntMul(longstr,shortstr[i]-'0'); s += zero; zero += '0'; str = BigIntAdd(str,s); } //将前端的0删除 int j = 0; for(;j < str.size();j++){ if(str[j] != '0') break; } for(;j < str.size();j++){ cout << str[j]; } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ string a, b; cin>>a>>b; int n=a.length(), m=b.length(); int s[n+m]; memset(s, 0, sizeof(s)); for(int i=n-1;i>=0;i--){ int c = 0; for(int j=m-1;j>=0;j--){ int k = i+j+1; int x = (a[i]-'0') * (b[j]-'0'); s[k] += x + c; c = s[k] / 10; s[k] %= 10; } s[i] = c; } bool p = true; for(int i=0;i<n+m;i++){ if(p){ if(s[i]==0) continue; else p = false; } cout<<s[i]; } cout<<endl; return 0; }
两个长数字字符串相乘,输出结果字符串。 1.首先直接转化为整数相乘来解决是无法绕过类型内存限制的,所以这个思路行不通。 2.模拟乘法过程,如123*27,依次求出123*7的结果和123*2的结果,将两者相加(pluss函数)。 3.对于123*7和123*2的计算由于考虑到第一点仍然不能直接转化为整型来计算,而简单而安全的 做法是如对于123*7,每一位3,2,1与7相乘是可以直接整数相乘的,那么将123*7分解为这一系列 操作即可,注意进位问题(mul函数)。 #include<iostream> #include<string> using namespace std; string mul(string s,char c) { string res; int carry=0; int tmp; for(int i=s.size()-1;i>=0;i--) { tmp=(s[i]-48)*(c-48)+carry; carry=tmp/10; res.insert(res.begin(),char(tmp%10+48)); } if(carry>0) res.insert(res.begin(),char(carry+48)); return res; } string pluss(string a,string b,int k) { if(a.size()==0) return b; else{ while(k>0) { b+='0'; k--; } int i=a.size()-1; int j=b.size()-1; int carry=0; int tmp; while(i>=0) { tmp=(b[j]-48)+(a[i]-48)+carry; carry=tmp/10; b[j]=char(tmp%10+48); i--; j--; } while(j>=0) { tmp=(b[j]-48)+carry; carry=tmp/10; b[j]=char(tmp%10+48); j--; } if(carry>0) { b.insert(b.begin(),char(carry+48)); } return b; } } string multiply(string s1,string s2) { string res; string tmp; for(int i=s2.size()-1;i>=0;i--) { tmp=mul(s1,s2[i]); res=pluss(res,tmp,s2.size()-i-1); } return res; } int main() { string s1; string s2; cin>>s1; cin>>s2; while(s1[0]=='0') { s1.erase(s1.begin()); } while(s2[0]=='0') { s2.erase(s2.begin()); } string res=multiply(s1,s2); cout<<res; }
#include <bits/stdc++.h> using namespace std; /*模拟笔算乘法*/ int main(){ string strA,strB,ans; getline(cin,strA); getline(cin,strB); ans.resize(strA.size()+strB.size()); for(int i=0;i<ans.size();i++) ans[i]='0'; for(int i=strB.size()-1,q=0 ;i>=0 ;i--,q++) { int x = strB[i]-'0'; //取出当前位数,与另外的字符串相乘。 for(int j=strA.size()-1,p=0,n=0 ;j>=0 ;j--,p++) { int y = strA[j]-'0'; int sum=x*y; //计算乘法 int pos = ans.size()-1-p-q; //存放位置 int num=ans[pos]-'0'+sum%10+n; //进位累计到当前位置 n=num/10; //保留累加进位 n+=sum/10; //保留相乘进位 ans[pos]=num%10+'0'; //把算好的数字放进去 if(j==0) //当到最高位时存放进位 ans[ans.size()-2-p-q]=n+'0'; } } int k=0; bool out=ans[0]!='0'; //最高位是否为0 while(k<ans.size()){ if(out) cout<<ans[k]; else if(ans[k+1]!='0') out=true; k++; } return 0; }
用系统自带的大数类的取巧做法就不说了,提供一种我们小学学的乘法相乘的做法,AC代码如下
import java.util.Scanner; import static java.lang.System.in; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(in); String str1 = sc.nextLine(), str2 = sc.nextLine(); //去掉前面的0,比如030这种数据 int isZero = 0; while (str1.charAt(isZero++) == '0') ; str1 = str1.substring(isZero-1); isZero = 0; while (str2.charAt(isZero++) == '0') ; str2 = str2.substring(isZero-1); String timesRes = "", plusRes = ""; for (int i = str2.length() - 1, j = 0; i >= 0; i--, j++) { timesRes = strTimesChar(str1, str2.charAt(i), j); plusRes = strAddStr(plusRes, timesRes); } System.out.println(plusRes); } public static String strTimesChar(String str, char c, int tenN) { int flag = 0; int temp = 0; String res = ""; int ic = c - '0'; for (int i = str.length() - 1; i >= 0; i--) { temp = (str.charAt(i) - '0') * ic + flag; res = (temp % 10) + res; flag = temp / 10; } if (flag > 0) { res = flag + res; } for (int i = 0; i < tenN; i++) { res += '0'; } return res.toString(); } public static String strAddStr(String str1, String str2) { int len1 = str1.length(); int len2 = str2.length(); if (len1 > len2) { for (int i = 0; i < len1 - len2; i++) { str2 = '0' + str2; } } else { for (int i = 0; i < len2 - len1; i++) { str1 = '0' + str1; } } int flag = 0; String res = ""; int temp = 0; for (int i = str1.length() - 1; i >= 0; i--) { temp = str1.charAt(i) + str2.charAt(i) - 2 * '0' + flag; res = (temp % 10) + res; flag = temp / 10; } if (flag > 0) { res = flag + res; } return res; } }
#include<iostream> #include<string> using namespace std; int main() { string a, b; cin >> a >> b; int size = a.size() + b.size(); string c(size,0); for (int i = a.size() - 1; i >= 0; i--) { int in_num = 0; for (int j = b.size() - 1; j >= 0; j--) { int tmp = (a[i] - '0')*(b[j] - '0'); c[i + j + 1] += tmp + in_num; in_num = c[i + j + 1] / 10; c[i + j + 1] %= 10; } c[i] = in_num; } for (int i = 0; i < size; i++) c[i] += '0'; while(c[0]=='0') c.erase(0,1); cout<<c<<endl; return 0; }
C++ 版本
var multiply = function(num1, num2) { if(num1 == "0" || num2 == '0'){ return '0' ; } var res = new Array(num1.length + num2.length) ; for(var i = 0; i < res.length; i ++){ res[i] = 0 ; } var a = num1.split('').reverse() ; var b = num2.split('').reverse() ; for(var i = 0; i < a.length; i ++){ for(var j = 0; j < b.length; j ++){ res[i+j] += a[i]*b[j] ; } } var carry = 0 ; var str = [] ; for(var i = 0; i < res.length; i ++){ res[i] += carry ; carry = parseInt(res[i] / 10 ); res[i] = res[i] % 10 ; str[i] = res[i] ; } str = str.reverse().join("") ; var index = 0 ; while(str[index] === "0"){ index ++ ; } return str.substring(index) ; }; console.log(multiply(readline(),readline())) ;
#include <stdio.h> #include <string.h> void GetMulti(char s1[], char s2[]) { int num1[1000], num2[1000]; int res[1000] = {0}; for (int i = 0; i < strlen(s1); ++i) { num1[strlen(s1) - i - 1] = s1[i] - '0'; } for (int i = 0; i < strlen(s2); ++i) { num2[strlen(s2) - 1 - i] = s2[i] - '0'; } for (int i = 0; i < strlen(s1); ++i) { for (int j = 0; j < strlen(s2); ++j) { res[i + j] += num1[i] * num2[j]; } } int tmp = 0; for (int i = 0; i <= strlen(s1) + strlen(s2) - 1; ++i) { res[i] += tmp; tmp = res[i] / 10; res[i] = res[i] % 10; } tmp = strlen(s1) + strlen(s2) - 1; while (res[tmp] == 0) { --tmp; } for (int i = tmp; i >= 0; --i) { printf("%d", res[i]); } printf("\n"); return; } int main() { char str1[1000], str2[1000]; gets(str1); gets(str2); GetMulti(str1, str2); return 0; }