lightOJ 1354 - IP Checking 【字符串处理&&进制转化】
2015-11-08 19:10
302 查看
1354 - IP Checking
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.
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.
PROBLEM SETTER: JANE ALAM JAN
思路:
直接将用点(.)分开的数都计算出来,然后保存到两个数组中,然后进行比较就行了,如果都相等,就输出OK!否则输出NO!
代码如下:
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
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
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.
Output
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 |
思路:
直接将用点(.)分开的数都计算出来,然后保存到两个数组中,然后进行比较就行了,如果都相等,就输出OK!否则输出NO!
代码如下:
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; char a[50],b[50]; int c[5],d[5]; int len1,len2; int main() { int T; scanf("%d",&T); int N=T; getchar(); while(T--) { scanf("%s%s",a,b); len1=strlen(a); len2=strlen(b); memset(c,0,sizeof(c)); memset(d,0,sizeof(d)); int t=0; for(int i=0;i<len1;i++) { if(a[i]!='.') { c[t]=c[t]*10+a[i]-'0'; } else { t++; } } t=0; for(int i=0;i<len2;i++) { if(b[i]!='.') { d[t]=d[t]*2+b[i]-'0'; } else { t++; } } int i; for(i=0;i<4;i++) { if(c[i]!=d[i]) break; } printf("Case %d: ",N-T); if(i!=4) { printf("No\n"); } else { printf("Yes\n"); } } return 0; }
相关文章推荐
- 典型用户 .
- 接口 构造器[构造函数]
- 《高性能网站建设指南》
- scanf 与 gets HDU1062
- windows xp部署iis+php+mysql
- 【C】【笔记】《C语言深度剖析》第七章 文件结构
- poj 3468 A Simple Problem with Integers(线段树更新区间)
- 典型用户和场景
- meanshift 图像分割
- 2015-09-28Javascript(一)
- 《大道至简》第六章读后感
- 【C】【笔记】《C语言深度剖析》第六章 函数
- findstr()与strfind()的区别
- Python_强制数据类型转换
- 微信企业号开发(二)-回调接口
- 下拉列表Spinner 、自动补齐AutoCompleteTextView、
- mean shift 图像分割
- PL/SQL入门理解(一)
- js--webSocket入门
- finish/onDestroy/System.exit()的区别