计算ip地址

 

code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int bit[8]={128, 64, 32, 16, 8, 4, 2, 1};

int main()
{
    int n, sum, i;
    char str[35];
    while(scanf("%d", &n)!= EOF)
    {
        while(n--)
        {
            scanf("%s", str);
            sum=0;
            for(i=0; i<32; i++)
            {
                if(str[i]=='1') sum+=bit[i%8];

                if(i==7)  {printf("%d.", sum);  sum=0;};
                if(i==15) {printf("%d.", sum);  sum=0;};
                if(i==23) {printf("%d.", sum);  sum=0;};
                if(i==31) {printf("%d\n", sum); };
            }
        }
    }
    return 0;
}

posted on 2010-12-31 15:23  FreeAquar  阅读(169)  评论(0编辑  收藏  举报