Saturday, January 14, 2017

UVa-579-Clock Hands

Problem link: Clock Hands

For detail information click hare 

Source code in C++:

#include<bits/stdc++.h>
#define file freopen("in.txt","rt",stdin)
int main()
{
   // file;
    int c=0,a;
    float h,m,H,d1,d2,ans;
    char str[10];
    while(gets(str))
    {
        a = strcmp(str,"0:00");
        if(a==0)
            break;
        if(strlen(str)==4)
        {
            h = str[0]-48;
            m = (str[2]-48)*10+(str[3]-48);

            H = h*60+m;
            d1 = H*0.5;
            d2 = m*6;
            ans= fabs(d1-d2);
            if(ans>180)
                ans-=360;
            printf("%.3f\n",fabs(ans));

        }
        else
        {
            h = (str[0]-48)*10+(str[1]-48);
            m = (str[3]-48)*10+(str[4]-48);

             H = h*60+m;
            d1 = H*0.5;
            d2 = m*6;
            ans= fabs(d1-d2);
            if(ans>180)
                ans-=360;
            printf("%.3f\n",fabs(ans));
        }
    }
    return 0;
}
 

Sunday, January 8, 2017

Lightoj-1354 IP-Checking

Problem Link: IP Checking

NB: To understand the following code first learn about strtok() library function in C.Click Here

Source code in C:

#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
    int test,i,sum=0,j=0,sum1,k,l;
    char str[50],str1[50];
    int a[5],b[5];
    char *temp, *temp1;
    int x,y;
    scanf("%d",&test);
    getchar();

    for(i=1; i<=test; i++)
    {
        gets(str);
        gets(str1);
        sum=0;
        k =0 ;
        temp = strtok(str, ".");
        while(temp!=NULL)
        {
            sum=0;
            for(j=0; j<strlen(temp); j++)
            {
                sum = sum*10 + (temp[j]-48);
            }
            a[k] = sum;
           // printf("%d ",a[k]);
            k++;
            temp = strtok(NULL, ".");
        }
        /*******************/
        k = 0;
        temp1 = strtok(str1, ".");
        while(temp1!=NULL)
        {
            sum=0;
            for(j=strlen(temp1)-1,l=0;j>=0;j--,l++)
            {
               sum = sum+(temp1[j]-48)* pow(2.00,(double)l);
            }

            b[k] = sum;
           // printf("%d ",a[k]);
            k++;
            temp1 = strtok(NULL, ".");
        }
        if(a[0]==b[0] && a[1]==b[1] && a[2]==b[2] && a[3]==b[3])
            printf("Case %d: Yes\n",i);
        else
           printf("Case %d: No\n",i);

    }
    return 0;
}

Friday, January 6, 2017

UVa-10189-Minesweeper Solution

Problem link: Minesweeper

Source Code in C++:

#include<bits/stdc++.h>
#define file freopen("in.txt","rt",stdin)
using namespace std;
char arr[110][110];
int ans[110][110];
int main()
{
    int n,m,c=0;
    char ch;
    // file;
    while(scanf("%d%d",&n,&m)==2)
    {
        if(n==0 && m==0)
            break;
        if(c!=0)
            cout<<endl;
        c++;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                cin>>ch;
                arr[i][j]=ch;
            }
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                int cnt=0;
                if(arr[i][j]=='.')
                {
                    if(arr[i][j+1]=='*' && i>=1 && i<=n && j+1>=1 && j+1<=m)
                        cnt++;
                    if(arr[i-1][j+1]=='*'&& i-1>=1 && i-1<=n && j+1>=1 && j+1<=m)
                        cnt++;
                    if(arr[i-1][j]=='*'&& i-1>=1 && i-1<=n && j>=1 && j<=m)
                        cnt++;
                    if(arr[i-1][j-1]=='*'&& i-1>=1 && i-1<=n && j-1>=1 && j-1<=m)
                        cnt++;
                    if(arr[i][j-1]=='*'&& i>=1 && i<=n && j-1>=1 && j-1<=m)
                        cnt++;
                    if(arr[i+1][j-1]=='*'&& i+1>=1 && i+1<=n && j-1>=1 && j-1<=m)
                        cnt++;
                    if(arr[i+1][j]=='*'&& i+1>=1 && i+1<=n && j>=1 && j<=m)
                        cnt++;
                    if(arr[i+1][j+1]=='*'&& i+1>=1 && i+1<=n && j+1>=1 && j+1<=m)
                        cnt++;

                    ans[i][j]=cnt;
                }
            }

        }
        printf("Field #%d:\n",c);
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                if(arr[i][j]=='*')
                {
                    printf("*");
                }
                else
                    cout<<ans[i][j];
            }

            cout<<endl;
        }
    }
    return 0;
}

Thursday, January 5, 2017

UVa 10018: Reverse and Add

Problem Link:Reverse and Add

NB:There is trick to solve this problem thus read the problem carefully.You may get compile error in C for input format.So implement the program in .cpp extension.

Source code in C++:

#include<stdio.h>
#define lli long long int
#define file freopen("in.txt","rt",stdin)
int main()
{
    // file;
    lli num,revnum,sum1,sum2,d,r,cnt,num1;
    int test;
    while(scanf("%d",&test)!=EOF)
    {
        while(test--)
        {
            cnt=0;
            scanf("%lld",&num);
            do
            {
                num1 = num;
                sum1=0;
                while(num>0)
                {
                    r = num%10;
                    num = num/10;
                    sum1 = sum1*10+r;
                }
                num=num1+sum1;
                sum2=0;
                while(num>0)
                {
                    r = num%10;
                    num = num/10;
                    sum2 = sum2*10+r;
                }
                cnt++;
                num=sum1+num1;
            }
            while((num1+sum1)!=sum2);
            printf("%lld %lld\n",cnt,sum2);
        }
    }
    return 0;
}