Advertisement
goldfenrir

BASES

Aug 29th, 2014
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. //G6
  2. #include <cstdlib>
  3. #include <cstdio>
  4. int valid(int num, int base);
  5. long base10(long num, int base);
  6. long conv(long num,int base);
  7. int main(int argc, char** argv) {
  8.     long num,acu;
  9.     int cont,base;
  10.     while (1){
  11.         cont=scanf("(%d)%ld",&base,&num);
  12.         if (cont==2){
  13.             if (valid(num,base)){
  14.                 acu+=base10(num,base);
  15.                 printf("(%d)%ld\t",base,num);
  16.                
  17.             }                      
  18.            scanf("%d",&num);
  19.         }else if (cont==-1) {break;
  20.         }else{
  21.             acu=conv(acu,base);
  22.             printf("(%d)%ld\n",base,acu);
  23.             acu=0;
  24.         }        
  25.     }
  26.     return 0;
  27. }
  28. int valid(int num, int base){
  29.     int b=1;
  30.     while ((num%10)<base && num!=0){
  31.         num=num/10;        
  32.     }
  33.     if (num!=0) b=0;
  34.     return b;  
  35. }
  36. long base10(long num, int base){
  37.     long aux=0;    int mul=1,cif;
  38.     do {        
  39.         cif=num%base;
  40.         aux+=cif*mul;
  41.         mul*=base;
  42.     }while (num=num/10);
  43.     return aux;
  44. }
  45. long conv(long num,int base){
  46.     long aux=0,res;
  47.     int mul=1;
  48.     do {
  49.         res=num%base;
  50.         aux+=mul*res;
  51.         mul*=10;
  52.        
  53.     }while (num=num/base);
  54.     return aux;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement