POJ 2105 IP Address(水~)

来源:互联网 发布:250bp进入编程 编辑:程序博客网 时间:2024/05/22 06:08

Description
把二进制的IP地址转化为十进制
Input
第一行为数据组数n,之后n行每行一个字符串表示一个IP地址
Output
对于每组用例,输出转化为十进制后的IP地址
Sample Input
4
00000000000000000000000000000000
00000011100000001111111111111111
11001011100001001110010110000000
01010000000100000000000000000001
Sample Output
0.0.0.0
3.128.255.255
203.132.229.128
80.16.0.1
Solution
水题,注意数据可能不等长,有的后面有空格,有的没有
Code

#include<stdio.h>int main(){    int n,a[10]={0},b[10]={0},c[10]={0},d[10]={0},i,m;    char s[40];    scanf("%d",&n);    m=n;    getchar();    while(n)    {        gets(s);        for(i=0;i<8;i++)//8位一段             a[n]=2*a[n]+s[i]-48;        for(i=8;i<16;i++)            b[n]=2*b[n]+s[i]-48;        for(i=16;i<24;i++)            c[n]=2*c[n]+s[i]-48;        for(i=24;i<32;i++)            d[n]=2*d[n]+s[i]-48;        n--;    }    for(i=m;i>0;i--)        printf("%d.%d.%d.%d\n",a[i],b[i],c[i],d[i]);}
0 0