PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
An IP address is a 32 bit address formatted in the following way
a.b.c.d
where a, b, c, d are integers each ranging from 0 to 255. Now you are given two IP addresses, first one in decimal form and second one in binary form, your task is to find if they are same or not.
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case starts with two lines. First line contains an IP address in decimal form, and second line contains an IP address in binary form. In binary form, each of the four parts contains 8 digits. Assume that the given addresses are valid.
For each case, print the case number and "Yes" if they are same, otherwise print "No".
Sample Input |
Output for Sample Input |
2 192.168.0.100 11000000.10101000.00000000.11001000 65.254.63.122 01000001.11111110.00111111.01111010 |
Case 1: No Case 2: Yes |
#include<stdio.h> #include<string.h> #include<math.h> int pow(int x,int y) { int i,ans=1; for(i=1;i<=y;i++) ans*=x; return ans; } int a[4]; int s[8]; int b[4]; int main() { int t; int T=1; int i,j; int flag=0; int sum; scanf("%d",&t); while(t--) { scanf("%d.%d.%d.%d",&a[0],&a[1],&a[2],&a[3]); scanf("%d.%d.%d.%d",&b[0],&b[1],&b[2],&b[3]); for(j=0;j<4;j++) { i=0; memset(s,0,sizeof(s)); while(a[j]) { s[i]=a[j]%2; i++; a[j]/=2; } sum=0; for(i=7;i>=0;i--) sum+=s[i]*pow(10,i); if(sum==b[j]) flag=1; else { flag=0; break; } } printf("Case %d: ",T++); if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }