csansoon

Consolidation 3 P89407 F004B. Stable products

Dec 28th, 2018
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. bool same_digits(int x, int y, int b ){
  6.   vector<int> v(b,0);
  7.   int taman = v.size();
  8.   int xy= x*y;
  9.   while(xy!=0){
  10.     if (x!=0)++v[x%b];
  11.     if (y!=0)++v[y%b];
  12.     --v[xy%b];
  13.     x/=b;
  14.     y/=b;
  15.     xy/=b;
  16.   }
  17.   for (int i = 0; i<taman; ++i){
  18.     if(v[i]!=0) return false;
  19.   }
  20.   return true;
  21. }
  22.  
  23.    
  24. void write(int n, int b ){
  25.   if(n!=0){
  26.   write(n/b, b);
  27.   int aux = n%b;
  28.   if (aux<10) cout << aux;
  29.   else cout << char(aux-10+'A');
  30.   }
  31. }
  32.  
  33.  
  34.  
  35. int main(){
  36.   int x,y;
  37.   while (cin >> x >> y){
  38.     int in = 2;
  39.     bool entra = false;
  40.     cout << "solutions for " << x << " and " << y;
  41.     while(in<=16){
  42.       bool resultat = same_digits(x, y, in);
  43.       if (resultat) {
  44.     if (not entra) cout << endl;
  45.     write(x,in);
  46.     cout << " * ";
  47.     write(y,in);
  48.     cout << " = ";
  49.     int nxy = x*y;
  50.     write(nxy,in);
  51.     cout << " (base " << in << ')' << endl;
  52.         entra = true;
  53.     }
  54.     ++in;
  55.   }
  56.   if (not entra) {
  57.     cout << endl;
  58.     cout << "none of them" << endl;
  59.   }
  60.     cout << endl;
  61.   }
  62. }
  63.  
  64. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Add Comment
Please, Sign In to add comment