Showing posts with label AdHoc. Show all posts
Showing posts with label AdHoc. Show all posts

10424 Love Calculator

10424 Love Calculator

10424 Love Calculator

#include<bits/stdc++.h>
using namespace std;


int main()
{

    string a,b;
    while(getline(cin,a))
    {

        getline(cin,b);
        float rat;
        long s1=0,s2=0;
        for(int i=0;i<a.length();i++)
        {
            char t=tolower(a[i]);
            //cout<<t;

            if(t>='a'&&t<='z')
            {
                s1+=t-'a'+1;

            }



        }
        if(s1%9==0) s1=9;
        else s1%=9;

        for(int i=0;i<b.length();i++)
        {
            char t=tolower(b[i]);
            //cout<<t;

            if(t>='a'&&t<='z')
            {
                s2+=t-'a'+1;

            }



        }
        if(s2%9==0) s2=9;
        else s2%=9;


        if(s1>s2)
            rat=((float)s2/s1)*100.0;
        else
            rat=((float)s1/s2)*100;
        printf("%.2lf %c\n",rat,'%');


    }



    return 0;
}

11057 - Exact Sum

11057 - Exact Sum
11057 - Exact Sum


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        vector<int>v;
        for(int i=0;i<n;i++)
        {
            int y;
            cin>>y;
            v.push_back(y);
        }
        int m;
        cin>>m;
        int b1,b2;
        sort(v.begin(),v.end());
        for(int i=0;i<v.size()-1;i++)
        {
            for(int j=i+1;j<v.size();j++)
            {
                if(v[i]+v[j]==m)
                {
                    b1=v[i];
                    b2=v[j];
                }
            }

        }
        printf("Peter should buy books whose prices are %d and %d.\n\n", b1, b2);


    }


    return 0;
}

10954 - Add All

10954 - Add All
#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    int x;
    while(1)
    {
        scanf("%d",&n);
        if(n==0) break;
        priority_queue<int, vector<int>, greater<int> >v;
        for(int i=0;i<n;i++)
        {
            cin>>x;
            v.push(x);

        }


        int t;
        int sum=0;
        while(v.size()>=2)
        {
            t=v.top();
            v.pop();
            t+=v.top();
            v.pop();


            sum+=t;
            v.push(t);
        }
        cout<<sum<<endl;

    }

    return 0;
}

10019 - Funny Encryption Method

10019 - Funny Encryption Method
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n;
    for(int t=1;t<=n;t++)
    {
        int bc=0,hc=0,h=0;
        cin>>m;
        int b=m;
        while(m!=0)
        {
            if((m&1)==1)
                bc++;
            m>>=1;
            //cout<<(m&1)<<endl;
        }

        h+=(b/10000)*pow(16,4);
        b%=10000;
        //cout<<h<<endl;

        h+=(b/1000)*pow(16,3);
        b%=1000;
        //cout<<h<<endl;

        h+=(b/100)*pow(16,2);
        b%=100;

       // cout<<h<<endl;
        h+=(b/10)*pow(16,1);
        b%=10;
        //cout<<h<<endl;
        h+=b;
        //cout<<h<<endl;

         while(h!=0)
         {
            if((h&1)==1)
                hc++;
            h>>=1;
            //cout<<(m&1)<<endl;
         }
        cout<<bc<<" "<<hc<<endl;
    }
    return 0;
}

476 - Points in Figures: Rectangles

476 - Points in Figures: Rectangles

476 - Points in Figures: Rectangles



#include<bits/stdc++.h>
using namespace std;
struct recPoint{   
 double x1,y1,x2,y2;};
int main(){
    char c;
    recPoint r[11];
    int i=0;
    while(true) 
   {
        cin>>c;
        if(c=='*') break;
        if(c=='r')                                  cin>>r[i].x1>>r[i].y1>>r[i].x2>>r[i].y2;        
i++;

    }    
double a,b;   
 int w=1,v;   
 bool flag=false;    
while(true)    { 
       cin>>a>>b;        flag=false;        v=-1;
        if(a==9999.9&&b==9999.9) break;        for(int j=0;j<i;j++)        {
            if(r[j].x1<a&&r[j].x2>a&&r[j].y1>b&&r[j].y2<b)            {

                flag=true;
                printf("Point %d is contained in figure %d\n",w,j+1);

            }        }
        if(!flag)
            printf("Point %d is not contained in any figure\n",w);        w++;    }
return 0;}


674 - Coin Change (Uva Solution)

674 - Coin Change (Uva Solution)
674 - Coin Change

import java.util.Scanner;

class Main {
public static long [] ways=new long [10000];

public static void main(String[] args) {


Scanner sc =new Scanner(System.in);
int n;
int [] coin=new int[5];
coin[0]=1;
coin[1]=5;
coin[2]=10;
coin[3]=25;
coin[4]=50;
int i,j;
ways[0]=1;
for(i=0;i<5;i++){
for(j=coin[i];j<10000;j++){
ways[j]+=ways[j-coin[i]];

}

}
while(sc.hasNext())
{

n=sc.nextInt();




System.out.println(ways[n]);

}

}


}

541 - Error Correction ( uva solution )

541 - Error Correction ( uva solution )

541 - Error Correction



import java.util.Scanner;

public class Main{   //this is mandatory that in the class name "Main" "M" must be capital letter


public static void main(String [] args){
Scanner sc=new Scanner(System.in);
int n;

while(sc.hasNext()){
n=sc.nextInt();

if(n==0) break;
int a[][] =new int[300][300];
int r,c,e,f,in1=0,in2=0;
r=c=e=f=0;
for(int i=0;i<n;i++){

for(int j=0;j<n;j++)
a[i][j]=sc.nextInt();
}
int i,j;
for( i=0;i<n;i++){
r=0;

for( j=0;j<n;j++){
r+=a[i][j];

}
if(r%2!=0){
e++;
in1=i+1;


}


}


for( i=0;i<n;i++){
c=0;

for( j=0;j<n;j++){
c+=a[j][i];

}
if(c%2!=0){
f++;
in2=i+1;


}


}
if(f==0&&e==0)
System.out.println("OK");
else if(f==1&&e==1)
System.out.println("Change bit ("+in1+","+in2+")");
else
System.out.println("Corrupt");





}
}

}

11942 - Lumberjack Sequencing

  11942 - Lumberjack Sequencing
  11942 - Lumberjack Sequencing

#include<bits/stdc++.h>

using namespace std;

bool fun(int a[],int j)
{
    for(int i=0;i<j-1;i++)
    {
        if(a[i]!=a[i+1])
        {
            if(a[i]<a[i+1]){
                for(int k=0;k<j-1;k++)
                {
                    if(a[k]>a[k+1])
                        return false;

                }
                return true;

            }
            else
            {
                for(int k=0;k<j-1;k++)
                {
                    if(a[k]<a[k+1])
                        return false;

                }
                return true;

            }
        }

    }
    return true;
}

int main()
{
    int n,t;
    string s;
    cin>>n;
    getchar();
    int a[25];
    cout<<"Lumberjacks:"<<endl;
    while(n--)
    {
        for(int i=0;i<10;i++)
            cin>>a[i];

        if(fun(a,10))
            cout<<"Ordered"<<endl;
        else cout<<"Unordered"<<endl;
        s.clear();

    }



 return 0;
}

12279 - Emoogle Balance

12279 - Emoogle Balance
12279 - Emoogle Balance
#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,a,x,y;
    int c=1;
    while(scanf("%d",&n)&&n)
    {
        x=y=0;
        while(n--)
        {
            cin>>a;
            if(a==0)
                x++;
            else y++;

        }
        printf("Case %d: %d\n",c,y-x);
        c++;

    }


    return 0;

}

11498 - Division of Nlogonia

 11498 - Division of Nlogonia
 11498 - Division of Nlogonia

#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n,N,M,x,y;
    while(scanf("%d",&n)&&n)
    {
        cin>>N>>M;
        while(n--)
        {
            cin>>x>>y;
            x-=N;
            y-=M;
            if(x*y==0)
                printf("divisa");
            else if(x>0&&y<0)
                printf("SE");
            else if(x>0&&y>0)
                printf("NE");
            else if(x<0&&y>0)
                printf("NO");
            else
                printf("SO");
            cout<<endl;
        }
    }


    return 0;

}

1182 - Parity ( LightOj solution)

1182 - Parity ( LightOj solution)

#include<bits/stdc++.h>

using namespace std;
vector<int> mainVector;
void d2b(int n)
{
    mainVector.clear();
    int inputNumber=0;
    int divisionStore=0,modStore=0;




    inputNumber=n;

    do
    {
        modStore=inputNumber%2;
        inputNumber=inputNumber/2;
        mainVector.push_back(modStore);

    }while(inputNumber!=0);


 }
int main()
{
    int t,c;
    long long a;

    cin>>t;
    int cou;
    for(c=1;c<=t;c++)
    {
        cou=0;
        cin>>a;
        d2b(a);
        for(int i=0;i<mainVector.size();i++)
        {
            if(mainVector[i]==1)
                cou++;
        }


        printf("Case %d: ",c);
        if(cou%2==0)
            printf("even");
        else
            printf("odd");
        cout<<endl;
    }

    return 0;
}

10183 - How Many Fibs? (Uva Solution)

10183 - How Many Fibs? (Uva Solution)
import java.math.BigInteger;
import java.util.Scanner;


class Main {

    public static void main(String[] args) {
       
        Scanner sc=new Scanner(System.in);
        BigInteger [] f=new BigInteger[502];
        f[1]=BigInteger.valueOf(1);
        f[2]=BigInteger.valueOf(2);
        for(int i=3;i<=501;i++)
        {
            f[i]=f[i-1].add(f[i-2]);
           
        }
        while(sc.hasNext())
        {
            int c=0;
            BigInteger a=sc.nextBigInteger();
            BigInteger b=sc.nextBigInteger();
            if(a.compareTo(BigInteger.ZERO)==0&&b.compareTo(BigInteger.ZERO)==0)
                break;
            else if(a.compareTo(BigInteger.ZERO)==0&&b.compareTo(BigInteger.ZERO)==0)
                System.out.println("1");
            else if(a.compareTo(BigInteger.ZERO)==0&&b.compareTo(BigInteger.ONE)==0)
                System.out.println("1");
            else if(a.compareTo(BigInteger.ONE)==0&&b.compareTo(BigInteger.ONE)==0)
                System.out.println("1");
            else
            {
                for(int i=1;i<501;i++)
                {
                    if(f[i].compareTo(a)>=0&&f[i].compareTo(b)<=0)
                        c++;
                }
                System.out.println(c);
               
            }
               
        }
        sc.close();

    }

}

352 - The Seasonal War ( UVA SOLUTION )

352 - The Seasonal War ( UVA SOLUTION )
#include<bits/stdc++.h>
using namespace std;
int n,counter;
char a[101][101];
int visited[101][101];

int valid(int r,int c)
{
    if(r>=0&&r<n&&c>=0&&c<n) return true;
    return false;
}
void floodfill(int r,int c)
{

    visited[r][c]=1;
    if(valid(r+1,c)&&a[r+1][c]=='1'&&!visited[r+1][c]) floodfill(r+1,c);
    if(valid(r-1,c)&&a[r-1][c]=='1'&&!visited[r-1][c]) floodfill(r-1,c);
    if(valid(r,c+1)&&a[r][c+1]=='1'&&!visited[r][c+1]) floodfill(r,c+1);
    if(valid(r,c-1)&&a[r][c-1]=='1'&&!visited[r][c-1]) floodfill(r,c-1);
    //daiagonal
    if(valid(r-1,c-1)&&a[r-1][c-1]=='1'&&!visited[r-1][c-1]) floodfill(r-1,c-1);
    if(valid(r-1,c+1)&&a[r-1][c+1]=='1'&&!visited[r-1][c+1]) floodfill(r-1,c+1);
    if(valid(r+1,c-1)&&a[r+1][c-1]=='1'&&!visited[r+1][c-1]) floodfill(r+1,c-1);
    if(valid(r+1,c+1)&&a[r+1][c+1]=='1'&&!visited[r+1][c+1]) floodfill(r+1,c+1);
}

int main()
{
    int i,j,c=1;
    while(cin>>n)
    {
        memset(a,'0',sizeof(a));
        getchar();
        for(i=0;i<n;i++)
        {
            //getchar();
            for(j=0;j<n;j++)
            {
                cin>>a[i][j];
            }
            getchar();

        }

        memset(visited,0,sizeof(visited));
        counter=0;

        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {

                if(!visited[i][j]&&a[i][j]=='1')
                {
                    counter++;

                    floodfill(i,j);
                }

            }

        }
        printf("Image number %d contains %d war eagles.\n",c,counter);

        c++;
    }
    return 0;
}

11830 - Contract Revision

11830 - Contract Revision


import java.math.BigInteger;
import java.util.Scanner;

class Main11830 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String ch,s,temp;

int i,j;
Scanner sc=new Scanner(System.in);
BigInteger a;
while(true)
{
temp=new String("");
ch=sc.next();
s=sc.next();
if(ch.charAt(0)=='0'&&s.charAt(0)=='0') break;
for(i=0;i<s.length();i++)
{
if(s.charAt(i)!=ch.charAt(0))
temp=temp+s.charAt(i);


}


if(temp.length()==0) temp="0";
a=new BigInteger(temp);

System.out.println(a);

}

}

}