fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. inline string odejmij(string a, string b){
  6. //WYNIK WYJSCIOWY Z DODAWANIA
  7. string wynik = "";
  8.  
  9. //DLUGOSC STRINGOW
  10. int strAd, strBd;
  11.  
  12. //PRZENIESIENIE
  13. int p = 0;
  14.  
  15. //WYROWNANIE LICZB
  16. while(a.size() - b.size() > 0) b = "0" + b;
  17.  
  18. cout << a << ' ' << b << endl;
  19.  
  20. strAd = a.size() - 1;
  21. strBd = b.size() - 1;
  22.  
  23. while(strAd >= 0 || strBd >= 0){
  24. //buforA, buforB, wynik
  25. int bufA = 0;
  26. int bufB = 0;
  27. int w = 0;
  28.  
  29. if(strAd >= 0) bufA = a[strAd] - 48; else bufA = 0;
  30. if(strBd >= 0) bufB = b[strBd] - 48; else bufB = 0;
  31.  
  32. if((bufA - bufB - p ) < 0) { w = bufA - bufB - p + 10; p = 1; }
  33. else if((bufA - bufB - p) == 0) { w = 0; p = 0; }
  34. else {w = bufA - bufB - p; p = 0;}
  35.  
  36. wynik = "0" + wynik;
  37. wynik[0] = 48 + w;
  38.  
  39. strAd--;
  40. strBd--;
  41. }
  42.  
  43. string wynikF = "";
  44.  
  45. //USUNIECIE ZER Z POCZATKU
  46. int s = 0;
  47. for(int i = 0; i < wynik.size(); i++){
  48. if(wynik[i] == 48) s += 1;
  49. else break;
  50. }
  51.  
  52. for(int i = s; i < wynik.size(); i++){
  53. wynikF += wynik[i];
  54. }
  55.  
  56. if(wynikF.size() == 0) wynikF += 48;
  57.  
  58. return wynikF;
  59. }
  60.  
  61. inline string NWD(string, string);
  62.  
  63. inline string wiekszy(string strA, string strB){
  64. int dlA = strA.size();
  65. int dlB = strB.size();
  66.  
  67. if(dlA > dlB) { return NWD(odejmij(strA, strB), strB); }
  68. else if(dlB > dlA) { return NWD(odejmij(strB, strA), strA); }
  69. else{
  70. for(int i = 0; i < dlA; i++){
  71. if(strA[i] > strB[i]) { return NWD(odejmij(strA, strB), strB);}
  72. else if(strA[i] < strB[i]) { return NWD(odejmij(strB, strA), strA);}
  73. }
  74. }
  75. }
  76.  
  77. inline string NWD(string a, string b){
  78. if(a == b) return a;
  79. else return wiekszy(a, b);
  80. }
  81.  
  82. int main(){
  83. ios_base::sync_with_stdio(NULL);
  84. cin.tie(nullptr);
  85.  
  86. string strA, strB;
  87. cin >> strA >> strB;
  88.  
  89. cout << odejmij(strA, strB);
  90. }
Success #stdin #stdout 0s 4468KB
stdin
123
234
stdout
123 234
889