954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Comparison of numbers (different number systems)

Hello, guys.
I made a program to compare numbers,
Example input:

1 3 10 845157 2 1101101 16 5AE12

It transfers numbers from different number system to decimal, then it compares them, but it is not so fast as i need, maybe just a little advice needed, I really dont know, I've tried all i thought...

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>

using namespace std;

int main(void)
{
    int poc,u,j,x,max,maxi;    
    char pov[35001][31];          // store for numbers from input 
    
    scanf("%d",&poc);  // Number of tasks
    while (poc--)
    {
      scanf("%d",&u);  // Amount of numbers coming from input
      int k=0,des[u],sus[u]; // sus - Number system identificator; des - store for decimal numbers
      while (u--)
      {
        scanf("%d",&sus[k]);    // Number System identificar (2-32)
        scanf("%s",&pov[k]);    // Number input
        
          int sum=0,j=0;
          j=strlen(pov[k]);  // Length of string
          
        if (sus[k]==10)       // if number is decimal just copy, if not transfer to decimal
        {           
           des[k]=atoi(&pov[k][0]);
        }
        else
        {   
          for (int i=0; i<j; i++)         // transfer to decimal number system
           {
             if ((pov[k][j-1-i])>64)      // if bigger than 64 -> A-Z (A = 10, B=11...)
               x=(int(pov[k][j-1-i])-55);
             else                         // if less than 64 it should be a number...
               x=(pov[k][j-1-i]-'0'); 
             sum+=x*pow(sus[k],i);
            }
          des[k]=sum;   // storing the decimal value for comparison
         }
        k++;
      }
      
      for (int i=0; i<k; i++)   // Output - the biggest first
      {
          max=0;
          for (j=0; j<k; j++)
          {
              if (des[j]>max)
              {
                 max=des[j];
                 maxi=j;
              }
          }
          des[maxi]=0;
          printf("%d %s\n",sus[maxi],pov[maxi]);
      }
      printf("#\n");  // Every task ends with #
    }
    
    return 0;
}
Matkoo
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: