fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. const int MAX_LENGHT = 1001, TEN = 10;
  5.  
  6. void add(int a[MAX_LENGHT], int b[MAX_LENGHT]) {
  7. int repetari, numar, change = 0;
  8. if (a[0] > b[0]) {
  9. repetari = a[0];
  10. } else {
  11. repetari = b[0];
  12. }
  13. if (b[0] > a[0]) {
  14. for (int i = a[0] + 1; i <= b[0]; i++) {
  15. a[i] = 0;
  16. }
  17. } else if (a[0] > b[0]) {
  18. for (int i = b[0] + 1; i <= a[0]; i++) {
  19. b[i] = 0;
  20. }
  21. }
  22. for (int i = 1; i <= repetari; ++i) {
  23. numar = a[i] + b[i] + change;
  24. if (numar >= TEN) {
  25. change = 1;
  26. numar -= TEN;
  27. } else {
  28. change = 0;
  29. }
  30. a[i] = numar;
  31. }
  32. if (change == 1) {
  33. a[repetari + 1] = 1;
  34. ++repetari;
  35. }
  36. a[0] = repetari;
  37. for (int i = 0; i <= repetari; ++i) {
  38. cout << a[i];
  39. }
  40. return;
  41. }
  42. int main() {
  43. int a[] = {5, 1, 2, 2, 5, 8}, b[] = {6, 2, 2, 2, 6, 7, 8};
  44. add(a, b);
  45. return 0;
  46. }
  47.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
6344169