fork download
  1. /// Raihan Ruhin
  2. /// CSE, Jahangirnagar University.
  3. /// Dhaka-Bangladesh.
  4. /// id: raihanruhin (topcoder / codeforces / codechef / hackerrank), 3235 (lightoj)
  5. /// mail: raihanruhin@ (yahoo / gmail / facebook)
  6. /// blog: ruhinraihan.blogspot.com
  7.  
  8. #include<bits/stdc++.h>
  9. using namespace std;
  10.  
  11. #define SET(a) memset(a,-1,sizeof(a))
  12. #define CLR(a) memset(a,0,sizeof(a))
  13. #define PI acos(-1.0)
  14.  
  15. #define MOD 1000000007
  16. #define MX 100010
  17.  
  18. string stringMult(string num1, string num2)
  19. {
  20. int l1=num1.size(), l2=num2.size(); //length of two arrays.
  21. int mx=l1+l2;
  22. int arr[mx]; //temporary integer array for intermediate calculation.
  23. CLR(arr); //set all value with 0
  24. int n1[l1], n2[l2]; //correspondent integer array of two string.
  25. //ASCII to number
  26. for(int i=0; i<l1; i++) n1[i]=num1[i]-'0';
  27. for(int i=0; i<l2; i++) n2[i]=num2[i]-'0';
  28.  
  29. //calculation
  30. for(int i=l1-1; i>=0; i--)
  31. for(int j=l2-1; j>=0; j--)
  32. arr[i+j+1]+=n1[i]*n2[j];
  33. //carry
  34. for(int i=mx-1; i>0; i--)
  35. if(arr[i]>9)
  36. {
  37. arr[i-1]+=arr[i]/10;
  38. arr[i]=arr[i]%10;
  39. }
  40. //eliminate starting zeros.
  41. int pos=0;
  42. while(!arr[pos]) pos++;
  43. //store to a string
  44. string s="";
  45. for(int i=pos;i<mx;i++)
  46. s+=arr[i]+'0';
  47. return s;
  48. }
  49.  
  50. int main()
  51. {
  52. ios_base::sync_with_stdio(0);
  53. cin.tie(0);
  54. int tc,kk=1, n;
  55. cin>>tc;
  56.  
  57. while(tc--)
  58. {
  59. string a, b;
  60. cin>>a>>b;
  61. cout<<"Case "<<kk++<<": "<<stringMult(a, b)<<endl;
  62. }
  63. return 0;
  64. }
  65.  
  66.  
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
Standard output is empty