Advertisement
JosepRivaille

X84338: Suma de meitats d'un número relacionades

Mar 14th, 2015
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. //Pre: Llegeix un natural
  6. //Post: Si té xifres parells, compara la part esquerra i dreta
  7. int main() {
  8.     int n, xf = 0, a  = 0, b = 0;
  9.     cin >> n;
  10.     for (int i = n; i != 0; i = i/10) ++xf;
  11.     if (xf%2 != 0 or n == 0) cout << "res" << endl;
  12.     else {
  13.         xf = (xf/2);
  14.         for (int at = xf; at != 0; --at) {
  15.             a = a + n%10;
  16.             n = n/10;
  17.         }
  18.         for (int bt = xf; bt != 0; --bt) {
  19.             b = b + n%10;
  20.             n = n/10;
  21.         }
  22.         if (a > b) cout << b << " < " << a << endl;
  23.         else if (a < b) cout <<  b << " > " << a << endl;
  24.         else cout << b << " = " << a << endl;
  25.     }
  26.  
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement