Sunday, October 23, 2011

IARCS Problem : Base b Arithmetic

/*This program is a solution to the question
 * given by IARCS at their website.
 *
 * The link to the question is : http://opc.iarcs.org.in/index.php/problems/BASE
 *
 *Note that this solution provided below
 *is in Java and not in C or C++
 */
import java.util.*;
class basebIARCS
{
public static void main()
{
    System.out.println("Enter the required information");
    Scanner sc=new Scanner(System.in);
    int b=sc.nextInt();
    int d1=sc.nextInt();
    int d2=sc.nextInt();
    int n1[]=new int[d1];
    int n2[]=new int[d2];
    int i,s1=0,s2=0,m;
    for(i=0;i<d1;i++)
    {
        n1[i]=sc.nextInt();
    }
    m=0;
    for(i=d1-1;i>=0;i--,m++)
    {
        n1[i]=n1[i]*((int)(Math.pow(b,m)));
        s1=s1+n1[i];
    }
   
    for(i=0;i<d2;i++)
    {
        n2[i]=sc.nextInt();
    }
    m=0;
    for(i=(d2-1);i>=0;i--,m++)
    {
        n2[i]=n2[i]*(int)(Math.pow(b,m));
        s2=s2+n2[i];
    }
   
    int pro=s1*s2;
    int q=pro,r;
    String ans="";
    int c=0;
    while(q!=0)
    {
        r=q%b;
        ans=r+" "+ans;
        q=q/b;
        c++;
    }
    System.out.println(c);
    System.out.println(ans);
}
}
//Author : Mayank Rajoria

No comments:

Post a Comment