fork(3) download
  1. #include <iostream>
  2. using namespace std;
  3. string multiply (string s1, string s2)
  4. {
  5. char str[10];
  6. string ans="";
  7. int m=s1.length();
  8. int n=s2.length();
  9.  
  10. if (!s1.compare("0") || !s2.compare("0"))
  11. return "0";
  12.  
  13. int *res = new int[m + n];
  14.  
  15. for (int i = m - 1; i >= 0; i--)
  16. {
  17. for (int j = n - 1; j >= 0; j--)
  18. {
  19. res[m + n - i - j - 2] += (s1[i] - '0') * (s2[j] - '0');
  20. res[m + n - i - j - 1] += res[m + n - i - j - 2] / 10;
  21. res[m + n - i - j - 2] %= 10;
  22. }
  23. }
  24.  
  25.  
  26. for (int i = m + n - 1; i >= 0; i--)
  27. {
  28. if (res[i] != 0)
  29. {
  30. for (int j = i; j >= 0; j--)
  31. {
  32. sprintf(str,"%d", res[j]);
  33. ans+=str;
  34. }
  35. return ans;
  36. }
  37.  
  38. }
  39. }
  40.  
  41. int main() {
  42.  
  43. cout << multiply("0", "0"); // Doesn't work - prints nothing.
  44. cout.put(cout.widen('\n'));
  45. cout.flush();
  46. // cout << multiply("0", "0") << endl; //works!! This prints "0" correctly
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
0