Thursday, April 28, 2016

Published April 28, 2016 by with 0 comment

UVA 11879 : Multiple of 17 - solution

#include<cstdio>
#include<string.h>
using namespace std;

int main()
{
    char n[111];
    while (scanf("%s",n)!=EOF)
    {
        int l=strlen(n);
        if (l==1 && n[0]=='0') return 0;
        int r=0;
        for (int i=0;i<l;i++)
        {
            r=r*10+n[i]-'0';
            r=r%17;
        }
        if (r==0) printf("1\n");
        else printf("0\n");
    }
    return 0;
}
Read More
      edit
Published April 28, 2016 by with 0 comment

UVA 10591: Happy Number -solution

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

int happy(int n)
{
    int temp=n;
    bool visit[10000];
    memset(visit,false,10000);
    while (1)
    {
        int temp2=0;
        while (temp)
        {
        int a=temp%10;
        temp2+=a*a;
        temp/=10;
        }
        if (temp2==1) return 1;
        if (temp2==n) return 0;
        if (visit[temp2]) return 0;
        visit[temp2]=true;
        temp=temp2;
    }
}

int main()
{
    int t,c=1;
    scanf("%d",&t);
    while (t--)
    {
        int n;
        scanf("%d",&n);
        if (happy(n)) printf("Case #%d: %d is a Happy number.\n",c++,n);
        else printf("Case #%d: %d is an Unhappy number.\n",c++,n);
    }
    return 0;
}
Read More
      edit

Wednesday, April 27, 2016

Published April 27, 2016 by with 0 comment

UVA 10235 - Simply Emirp : Solution

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

bool mark[1000000];
void sieve()
{
    memset(mark,true,1000000);
    mark[1]=false;
    for (int i=4; i<1000000;i+=2)
        mark[i]=false;
    for (int i=3;i<1000;i+=2)
        if (mark[i])
            for (int j=i*i;j<1000000;j+=i*2)
                mark[j]=false;
    return;
}

int main()
{
    int n;
    sieve();
    while (scanf("%d",&n)==1)
    {
        int rev=0;
        int temp=n;
        while (temp)
        {
            rev=rev*10+temp%10;
            temp/=10;
        }
        if (mark[n])
        {
            if (mark[rev] && n!=rev) printf("%d is emirp.\n",n);
            else printf("%d is prime.\n",n);
        }
        else printf("%d is not prime.\n",n);
    }
    return 0;
}
Read More
      edit

Monday, April 25, 2016

Published April 25, 2016 by with 0 comment

UVA 12503 : Robot Instructions -Solution

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        int n,a;
        scanf("%d",&n);
        int j=1, pos=0;
        int ins[105]={0};
        char s[10], b[5];
        for(int i=1; i<=n; i++)
        {
            scanf("%s",s);
            if (s[0]=='L') ins[i] = -1;
            else if (s[0]=='R') ins[i] = 1;
            else
            {
                scanf("%s %d", b, &a);
                ins[i] = ins[a];
            }
            pos+=ins[i];
        }
        printf("%d\n",pos);
    }
    return 0;
}
Read More
      edit
Published April 25, 2016 by with 0 comment

UVA 11952: Arithmetic -Solution

#include<cstdio>
#include<string.h>
using namespace std;

char A[10],B[10],C[10];

int one ()
{
    int la=strlen(A);
    int lb=strlen(B);
    int lc=strlen(C);
    for (int i=0;i<la;i++) if (A[i]!='1') return 0;
    for (int i=0;i<lb;i++) if (B[i]!='1') return 0;
    for (int i=0;i<lc;i++) if (C[i]!='1') return 0;
    if (la+lb!=lc) return 0;
    return 1;
}
int lowestbase ()
{
    int base=0;
    int la=strlen(A);
    int lb=strlen(B);
    int lc=strlen(C);
    for (int i=0;i<la;i++) if (base<A[i]-'0') base=A[i]-'0';
    for (int i=0;i<lb;i++) if (base<B[i]-'0') base=B[i]-'0';
    for (int i=0;i<lc;i++) if (base<C[i]-'0') base=C[i]-'0';
    return base;
}

long long int convert(char s[],int base)
{
    long long int n=0;
    for (int i=0;s[i];i++)
    n=s[i]-'0'+n*base;
    return n;
}

int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%s + %s = %s",A,B,C);
        long long int a,b,c;
        if (one())
        {
            printf("1\n");
            continue;
        }
        int base=lowestbase();
        if (base) base++;
        else
        {
            printf("2\n");
            continue;
        }
        int i;
        for (i=base;i<19;i++)
        {
            a=convert(A,i);
            b=convert(B,i);
            c=convert(C,i);
            if (a+b==c)
            {
                printf("%d\n",i);
                i=111;
            }
        }
        if (i==19) printf("0\n");
    }
    return 0;
}
Read More
      edit
Published April 25, 2016 by with 0 comment

UVA 11192 : Group Reverse -Solution

#include<cstdio>
#include<string.h>
using namespace std;

int main()
{
    int g;
    while (scanf("%d",&g)==1)
    {
        if (g==0) break;
        char s[110];
        getchar();
        scanf("%s",s);
        int l=strlen(s);
        int nchar=l/g;
        for (int i=1;i<=g;i++)
        {
            int j=i*nchar;
            int t=nchar;
            while (t--) printf("%c",s[--j]);
        }
        printf("\n");
    }
    return 0;
}
Read More
      edit
Published April 25, 2016 by with 0 comment

UVA 11455. : Behold my quadrangle -Solution

#include<cstdio>
#include<algorithm>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while (t--)
    {
        unsigned long long int s[4];
        for (int i=0;i<4;i++) scanf("%llu",&s[i]);
        sort(s,s+4);
        if (s[0]==s[1]&&s[1]==s[2]&&s[2]==s[3])
            printf("square\n");
        else if (s[0]==s[1] && s[2]==s[3])
            printf("rectangle\n");      
        else if (s[0]+s[1]+s[2]>=s[3])
            printf("quadrangle\n");
        else printf("banana\n");
    }
    return 0;
}
Read More
      edit