Large integer multiplication: Conceptually, it is a number that cannot be stored in int, long, long long, double, etc., and the length is several hundred digits
So we use strings to store and start from the least bit, and then the big integer multiplication turns into
The problem of vertical multiplication in elementary school.
example:
Link: https://www.nowcoder.com/acm/contest/118/E
Source: Niuke.com
Topic description
Kirai likes to send "233" when chatting. "233" comes from Maopu emoji No. 233, which is a thumping and laughing emoji.
Kirai always replies "2333..." whenever she sees interesting news.
Kirai is actually very cold, he found this problem. In order not to want others to know that he was laughing immediately, he decided to multiply the two "233.." together and send it out.
Enter description:
There are multiple groups of input samples, all of which are positive integers. First, enter the number of sample groups T (T≤1500).
Next, enter the number of T groups, each group of numbers consists of two 233 strings, each 233 string length 3≤n≤50.
The data guarantees that each 233 string must have a 2 as the beginning, and the number of 3 is ≥ 2.
Output description:
The product of two 233 strings.
Example 1
enter
2 233 233 23333333333333333333333333333333333333333333333333 23333333333333333333333333333333333333333333333333
output
54289 544444444444444444444444444444444444444444444442888888888888888888888888888888888888888888888889
_
#include<cstdio> #include<iostream> #include<string.h> #include<algorithm> using namespace std; char a[100],b[100]; int c[100],d[100]; void Bigintercheng(){ memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); int lena=strlen(a); int lenb=strlen(b); for(int i=0;i<lena;i++){ c[i]=a[lena-i-1]-'0'; } for(int j=lenb-1;j>=0;j--){ for(int i=0;i<lena;i++){ d[i+lenb-1-j]+=c[i]*(b[j]-'0'); } } for(int i =0;i<lena+lenb-1;i++){ d[i+1]+=d[i]/10; d[i]%=10; } int f=0; for(int i=500;i>=0;i--){ if(!d[i]&&!f) continue; f=1; printf("%d",d[i]); } printf("\n"); } intmain () { int t; scanf("%d",&t); while(t--){ cin>>a>>b; Bigintercheng(); } }
The java code is as follows
Because java has a package for large integers, it is enough to solve it directly (java is good
import java.math.BigInteger; import java.util.Scanner; public class Main { public static void main(String[] args) { int t; Scanner in = new Scanner(System.in); t=in.nextInt(); for(int i=0;i<t;++i) { BigInteger a=in.nextBigInteger(); BigInteger b=in.nextBigInteger(); System.out.println(a.multiply(b)); } in.close(); } }
The python code is as follows (life is too short, I use python
t = int(raw_input()) while t > 0: t-=1 a, b = raw_input().split() print(int(a)*int(b))
Python Dafa is good!