Pages

Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Sunday, 22 December 2013

UVA - 12626 - I ❤ Pizza

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

int min(int x,int y){
    if(x<y)
        return x;
    return y;
}

int main()
{
    int n=0;
    scanf("%d",&n);
    while(n--){
        int M=0,A=0,R=0,G=0,I=0,T=0,i=0,z=0;
        char str[600];
        scanf("%s",str);
        z=strlen(str);
        for(i=0;i<z;i++){
            if(str[i]=='M')
                M++;
            else if(str[i]=='A')
                A++;
            else if(str[i]=='R')
                R++;
            else if(str[i]=='G')
                G++;
            else if(str[i]=='I')
                I++;
            else if(str[i]=='T')
                T++;
        }
        int answer=min(min(min(min(min(M,A/3),R/2),G),I),T);
        printf("%d\n",answer);
    }
    return 0;
}


UVA - 12704 - Little Masters

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

int main()
{
    int n=0;
    scanf("%d",&n);
    while(n--){
        float x=0,y=0,r=0;
        scanf("%f %f %f",&x,&y,&r);
        double s=sqrt(x*x+y*y);
        printf("%.2f %.2f\n",r-s,r+s);
    }
    return 0;
}

UVA - 12700 - Banglawash

#include <stdio.h>

int main()
{
    int n=0,cases=1;
    scanf("%d",&n);
    while(n--){
        int matches=0,i=0;
        scanf("%d",&matches);
            char str[11];
            scanf("%s",str);
            int A=0,B=0,T=0,W=0;
            for(i=0;i<matches;i++){
                if(str[i]=='A')
                    A++;
                else if(str[i]=='B')
                    B++;
                else if(str[i]=='T')
                    T++;
                else if(str[i]=='W')
                    W++;
            }
            if((B+A==matches)&& B!=0)
                printf("Case %d: BANGLAWASH\n",cases);
            else if((W+A==matches) && W!=0)
                printf("Case %d: WHITEWASH\n",cases);
            else if(A==matches)
                printf("Case %d: ABANDONED\n",cases);
            else if(B>W)
                printf("Case %d: BANGLADESH %d - %d\n",cases,B,W);
            else if(B<W)
                 printf("Case %d: WWW %d - %d\n",cases,W,B);
            else if(B==W)
                printf("Case %d: DRAW %d %d\n",cases,B,T);
            cases++;
    }
    return 0;
}

Monday, 16 December 2013

UVA - 12708 - GCD The Largest

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

int main()
{
    int n=0;
    long x=0;
    scanf("%d",&n);
    while(n--){
        scanf("%ld",&x);
        if(x%2!=0){
            x--;
        }
        printf("%ld\n",x/2);
    }
    return 0;
}

Saturday, 13 July 2013

UVA - 12611 - Beautiful Flag

#include <stdio.h>

int main()
{
    int cases,i=1, r;
    scanf("%d",&cases);
    while(cases--){
        scanf("%lf",&r);
        printf("Case %d:\n",i);
        printf("%.0lf %.0lf\n",-2.25*r,1.5*r);
        printf("%.0lf %.0lf\n",2.75*r,1.5*r);
        printf("%.0lf %.0lf\n",2.75*r,-1.5*r);
        printf("%.0lf %.0lf\n",-2.25*r,-1.5*r);
        i++;
    }
    return 0;
}

Friday, 28 June 2013

UVA - 11364 - Parking

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

int main()
{
    int cases,n,val;
    scanf("%d",&cases);
    while(cases--){
        scanf("%d",&n);
        int max=0,min=100;
        while(n--){
            scanf("%d",&val);
            if(val>max)
                max=val;
            if(val<min)
                min=val;
        }
        printf("%d\n",2*(max-min));
    }
    return 0;
}

UVA - 12279 - Emoogle Balance

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

int main()
{
    int n,val,i=1;
    while(1){
        scanf("%d",&n);
        if(n==0){
            break;
        }
        int treat=0,party=0;
        while(n--){
            scanf("%d",&val);
            if(val==0){
                treat++;
            }else{
                party++;
            }
        }
        printf("Case %d: %d\n",i,(party-treat));
        i++;
    }
    return 0;
}

Thursday, 14 March 2013

Thursday, 17 January 2013

UVA - 10167 - Birthday Cake

/*You don't have to brute force A from -500 to 500 and B from -500 to 500.
You can brute force A from 0 to 500 and B from -500 to 500  as if (-500,-500) is the same as (500,500) .
*/
#include<stdio.h>
#include<string.h>
#define MAX 101

int x[MAX],y[MAX];

int ans(int A,int B,int n){
    int i,pos=0,neg=0;
    for(i=0;i<2*n;i++){
        int temp=A*x[i]+B*y[i];
        if(temp>0){
            pos++;
        }
        else if(temp<0){
            neg++;
        }else{
            return 0;
        }
    }
    if(pos==neg)
        return 1;
    return 0;
}

int main()
{
    int i,j,n;
    while(1){
        scanf("%d",&n);
        if(n==0)
            break;
        for(i=0;i<2*n;i++){
            scanf("%d %d",&x[i],&y[i]);
        }
        int temp=-1,tempA=-1,tempB=-1;
        for(i=0;i<501;i++){
            for(j=-500;j<501;j++){
                temp=ans(i,j,n);
                if(temp==1){
                    tempA=i;
                    tempB=j;
                    break;
                }
            }
            if(temp==1){
                break;
            }
        }
        printf("%d %d\n",tempA,tempB);
    }
    return 0;
}

Wednesday, 16 January 2013

UVA - 270 - Lining Up

#include<stdio.h>
#include<string.h>
#define MAX 701

int coolinear(int x1,int y1,int x2,int y2,int x3,int y3){
    return (y1 - y2) * (x1 - x3) == (x1 - x2) * (y1 - y3);
}

int main()
{
    int i,ic,n ,j, z, counter, points,max;
    int x[MAX], y[MAX], arr[MAX][MAX], temp[MAX] ;
    char str[150];
    gets(str);
    sscanf(str, "%d", &n);
    gets(str);
    for(ic=0;ic<n;ic++){
        if(ic>0)
            printf("\n");
        points = 0;
        while(gets(str) != NULL){
            if(str[0] == '\0')
                break;
            sscanf(str, "%d%d", &x[points], &y[points]);
            points++;
        }
        memset(arr, -1, sizeof(arr));
        max = 0;
        for(i = 0; i < points; i ++)
            for(j = i + 1; j < points; j ++)
                if(arr[i][j]){
                    counter = 0;
                    for(z = 0; z < points; z++)
                        if(coolinear(x[i],y[i],x[j],y[j],x[z],y[z]))
                            temp[counter++] = z;
                    if(counter > max)
                        max = counter;
                    int l,m;
                    for(l = 0; l < counter; l ++)
                        for(m = l + 1; m < counter; m ++){
                            arr[temp[l]][temp[m]] = 0;
                            arr[temp[m]][temp[l]] = 0;
                        }
                }
        printf("%d\n", max);
    }
    return 0;
}

Saturday, 12 January 2013

UVA - 10489 - Boxes of Chocolates

#include<stdio.h>

int main()
{

   long n,friends,boxes,k,product,sum,temp;
   scanf("%ld",&n);
   while(n--){
      scanf("%ld %ld",&friends,&boxes);
      sum=0;
      while(boxes--){
         product=1;
         scanf("%ld",&k);
         while(k--){
            scanf("%ld",&temp);
            product=(product*temp)%friends;
         }
         sum=(sum+product)%friends;
      }
      printf("%ld\n",sum);
   }
   return 0;
}

Friday, 11 January 2013

UVA - 481 - What Goes Up

#include <vector>
#include <cstdio>

using namespace std;

void LIS(vector<int> &input, vector<int> &lis){
    vector<int> temp(input.size());
    lis.push_back(0);
    int s, e;
    for (size_t i = 1; i < input.size(); i++){
        if (input[lis.back()] < input[i]){
            temp[i] = lis.back();
            lis.push_back(i);
            continue;
        }
        s = 0;
        e = lis.size()-1;
        while (s < e){
            int m = (s + e) / 2;
            if (input[lis[m]] < input[i])
                s=m+1;
            else
                e=m;
        }
        if (input[i] < input[lis[s]]){
            if (s > 0)
                temp[i] = lis[s-1];
            lis[s] = i;
        }
    }
    for (s = lis.size(), e = lis.back(); s--; e = temp[e])
        temp[s] = e;
}


int main()
{
    int x;
    vector<int> seq;
    while(scanf("%d",&x)==1){
        if(x==22)
            break;
        seq.push_back(x);
    }
    vector<int> lis;
    LIS(seq, lis);
    printf("%d\n-\n", lis.size());
    for (unsigned int i = 0; i < lis.size(); i++)
        printf("%d\n", seq[lis[i]]);
    return 0;
}

Friday, 28 December 2012

UVA - 438 - The Circumference of the Circle

#include <stdio.h>
#include <math.h>
#define pi 3.141592653589793

double circ(double x,double y,double z){
        double s=(x+y+z)/2;
        return (x*y*z)*pi/(2.0*sqrt(s*(s-x)*(s-y)*(s-z)));
}

double ecuD(double x1,double y1,double x2,double y2){
    return sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
}

int main() {
    double x1,y1,x2,y2,x3,y3,x,y,z;
    while (scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)==6){
        x=ecuD(x1,y1,x2,y2);
        y=ecuD(x1,y1,x3,y3);
        z=ecuD(x2,y2,x3,y3);
        printf("%.2lf\n",circ(x,y,z));
    }
    return 0;
}

UVA - 1339 - Ancient Cipher (C solution)

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>

using namespace std;

int main() {
    char x[101], y[101];
    while(gets(x)){
        gets(y);
        int len=strlen(x);
        int arr1[26],arr2[26];
        for(int i=0;i<26;i++){
            arr1[i]=arr2[i]=0;
        }
        for(int i=0;i<len;i++){
            arr1[x[i]-65]++;
            arr2[y[i]-65]++;
        }
        sort(arr1, arr1 + sizeof (arr1) / sizeof (arr1[0]));
        sort(arr2, arr2 + sizeof (arr2) / sizeof (arr2[0]));
        bool flag=true;
        for(int i=0;i<26;i++){
            if(arr1[i]!=arr2[i]){
                flag=false;
                break;
            }
        }
        if(flag){
            puts("YES");
        }else{
            puts("NO");
        }
    }
    return 0;
}

Thursday, 27 December 2012

UVA - 10209 - Is This Integration ? (C solution)

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

int main()
{
    double x,y,z,l,areaOfRect;
    double pi=acos(-1);
    while(scanf("%lf",&x)==1){
        areaOfRect=x*x;
        y = areaOfRect * (1 - sqrt(3) + pi / 3);
        z = areaOfRect * (2 * sqrt(3) - 4 + pi / 3);
        l = areaOfRect * (4 - sqrt(3) - 2 * pi/ 3);
        printf("%.3f %.3f %.3f\n", y,z,l);
    }
    return 0;
}

Saturday, 22 December 2012

UVA - 10170 - The Hotel with Infinite Rooms

#include <stdio.h>

long fact(long x,long num,long ans){
    if(x>=ans)
        return num;
    return fact(x+num,++num,ans);
}

int main() {
    long x,y;
    while((scanf("%ld %ld",&x,&y))==2){
        long ans=fact(x,x,y);
        printf("%ld\n",ans);
    }
    return 0;
}

UVA - 11313 - Gourmet Games

#include <stdio.h>

int main() {
    int x,y,i,n;
    scanf("%d",&n);
    for(i=0;i<n;i++) {
        scanf("%d %d",&x,&y);
        if((x-1)%(y-1)!=0){
            puts("cannot do this");
        }else{
            printf("%d\n",(x-1)/(y-1));
        }
    }
    return 0;
}

Thursday, 20 December 2012

UVA - 11909 - Soya Milk (C solution)

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

int main()
{
    int l,w,h,theta;
    double pi=acos(-1);
    while(scanf("%d %d %d %d",&l,&w,&h,&theta)==4){
        double d=l*tan(theta*pi/180.0);
        double ans;
        if(d > h){
            ans=0.5*h*h*l*w/d;
        }else{
            ans=l*w*((h)-(d*0.5));
        }
        printf("%.3lf mL\n", ans);
    }
    return 0;
}

UVA - 10991 - Region (C Solution)

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

int main()
{
    double r1,r2,r3;
    int cases,i;
    scanf("%d",&cases);
    for(i=0;i<cases;i++){
      scanf("%lf %lf %lf",&r1,&r2,&r3);

      double m1= r2+r3;
      double m2= r1+r3;
      double m3= r1+r2;

      double theta1=acos((m2*m2+m3*m3-m1*m1)/(2*m2*m3));
      double theta2=acos((m1*m1+m3*m3-m2*m2)/(2*m1*m3));
      double theta3=acos((m2*m2+m1*m1-m3*m3)/(2*m2*m1));

      double med=(m1+m2+m3)/2.0;
      double areaOfTri=sqrt(med*(med-m1)*(med-m2)*(med-m3));

      double areaofArc1=0.5*theta1*r1*r1;
      double areaofArc2=0.5*theta2*r2*r2;
      double areaofArc3=0.5*theta3*r3*r3;

      double ans=areaOfTri-areaofArc1-areaofArc2-areaofArc3;
      printf("%.6f\n", ans);
    }
    return 0;
}

UVA - 10347 - Medians (C solution)

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

int main()
{
    double m1,m2,m3;
    while(scanf("%lf %lf %lf",&m1,&m2,&m3)==3){
        double med=(m1+m2+m3)/2.0;
        double area=(4/3.0)*sqrt(med*(med-m1)*(med-m2)*(med-m3));
        if(!(area>0)){
            area=-1.0;
        }
        printf("%.3lf\n",area);
        }
    return 0;
}