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;
}

No comments:

Post a Comment