Showing posts with label IARCS Problems. Show all posts
Showing posts with label IARCS Problems. Show all posts

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

Monday, October 17, 2011

IARCS Problem : A Board Game

/*This program is a solution to the question
 * given by IARCS at their website.
 *
 * The link to the question is:http://www.iarcs.org.in/inoi/contests/oct2004/Basic-2.php
 *
 *Note that this solution provided below
 *is in Java and not in C or C++
 */
import java.util.*;
class BoardGame
{
public static void main()
{
    System.out.println("Enter the required info : ");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int m=sc.nextInt();
    char mark[]=new char[n];
    int i;
    String s;
    for(i=0;i<n;i++)
    {
        s=sc.next();
        mark[i]=s.charAt(0);
    }
   
    int v,pos=0,k=1;
    for(i=0;i<m;i++)
    {
        v=sc.nextInt();
        if(mark[pos]=='+')
        {
            if((pos+1+v)<=n)
            {
                pos=pos+v;
            }
        }
        else if(mark[pos]=='-')
        {
            if((pos+1-v)>=1)
            {
                pos=pos-v;
            }
        }
        if(pos==0)
        {
            k++;
        }
    }
    System.out.println(k);
}
}

//Author : Mayank Rajoria

IARCS Problem : BCCS Elections (Java)

/*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/BCCSELEC
 *
 *Note that this solution provided below
 *is in Java and not in C or C++
 */
import java.util.*;
class BCCSElection
{
public static void main()
{
    System.out.println("Enter the number of people");
    Scanner sc=new Scanner(System.in);
    int c=sc.nextInt();
    int n=sc.nextInt();
    int votes[]=new int[c];
    int i,v;
    for(i=0;i<c;i++)
    {
        votes[i]=0;
    }
    for(i=0;i<n;i++)
    {
        v=sc.nextInt();
        votes[v-1]++;
    }
    int k=0,j=0,max=0;
    for(i=1;i<=3;i++)
    {
        for(j=0;j<c;j++)
        {
            if(votes[j]>=k)
            {
                k=votes[j];
                max=j;
            }
        }
        votes[max]=0;
        k=0;
    }
    System.out.println(max+1);
}
}

//Author : Mayank Rajoria