Advertisement
JosepRivaille

P57515: Rotacions cap a la dreta

Apr 6th, 2015
847
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. int count(int x) {
  7.     int xif = 0;
  8.     while (x != 0) {
  9.         ++xif;
  10.         x = x/10;
  11.     }
  12.     return xif;
  13. }
  14.  
  15. int rotacio_dreta(int x, int k) {
  16.     if (k > count(x)) k = k - count(x);
  17.     if (k == count(x)) return x;
  18.     int aux = 0;
  19.     int mult = 1;
  20.     for (int i = k; i != 0; --i) {
  21.         aux = mult * (x%10) + aux;
  22.         mult = 10 * mult;
  23.         x = x/10;
  24.     }
  25.     mult = 1;
  26.     for (int i = count(x); i != 0; --i) mult = mult *10;
  27.     aux = aux*mult + x;
  28.     return aux;
  29. }
  30.  
  31. //Pre: Llegeix un nombre
  32. //Post: Escriu la seva rotació cap a la dreta
  33. int main() {
  34.     int x, k;
  35.     while (cin >> x >> k) {
  36.         cout << rotacio_dreta(x, k) << endl;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement