Tuesday, 26 March 2013

11827 - Maximum GCD

#include<stdio.h>
#define max(a,b) (a>b)?a:b

int num[100];

int gcd(int a, int b)
{
    if(a)
        return gcd(b%a, a);

    return b;
}

int main()
{
    int c,i=0,j=0,m;
    char buffer[100];
    //freopen("in.txt","r",stdin);
    scanf("%d\n",&c);
        while(c--)
        {
            gets(buffer);
            i=0,j=0;
            while(buffer[i])
            {
                num[j] = 0;
                while(buffer[i] && buffer[i]!=' ')
                    num[j] = num[j]*10 + buffer[i++]-'0';
                while(buffer[i]==' ')
                    i++;
                j++;
            }

            m=1;
            for(i=0;i<j;i++)
            {
                for(int k=i+1;k<j;k++)
                {
                    m = max(m,gcd(num[i], num[k]));
                }
            }
            printf("%d\n",m);
        }
    return 0;
}

575 - Skew Binary

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    char skem[100];
    int k = 0;
    long sum = 0;
    //freopen("in.txt","r",stdin);
    while(gets(skem))
    {
        int n = strlen(skem);
        int num = skem[0]-'0';
        if(n==1 && num == 0) break;
        for(int i=0;i<n;i++)
        {
            k=n-i;
            sum += ((skem[i]-'0')*((long)pow(2,k)-1));
        }
        printf("%ld\n",sum);
        sum = 0;
    }
    return 0;
}