POJ 2105 IP Address

Problem Description
Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of ‘1s’ and ‘0s’ (bits) to a dotted decimal format. A dotted decimal format for an IP address is form by grouping 8 bits at a time and converting the binary representation to decimal representation. Any 8 bits is a valid part of an IP address. To convert binary numbers to decimal numbers remember that both are positional numerical systems, where the first 8 positions of the binary systems are:
27 26 25 24 23 22 21 20

128 64 32 16 8 4 2 1

Input
The input will have a number N (1<=N<=9) in its first line representing the number of streams to convert. N lines will follow.

Output
The output must have N lines with a doted decimal IP address. A dotted decimal IP address is formed by grouping 8 bit at the time and converting the binary representation to decimal representation.

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(){
    int n;
    scanf("%d",&n);getchar();
    while(n--){
        char str[35];
        int i;
        gets(str);
       // for(i=0;i<32;i++)
         //   scanf("%c",&str[i]);
        int sum[4];
        int j;
        //printf("!!!\n");
        for(int j=3;j>=0;j--){
                sum[j]=0;
            for(i=1;i<=8;i++){
                if(str[(j+1)*8-i]=='1'){
                    sum[j]+=pow(2,i-1);
                /**printf("sum[%d]=%d\n",j,sum[j]);putchar(str[(j+1)*8-i]);
                putchar(10);**/
                }
            }
        }
        printf("%d.%d.%d.%d\n",sum[0],sum[1],sum[2],sum[3]);
        for(i=0;i<4;i++)
            sum[i]=0;
    }
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int a[8]={1,2,4,8,16,32,64,128};
int main(){
    int n;
    scanf("%d",&n);
    while(n--){
        char str[35];

            scanf("%s",str);
        int i,sum[5],j=0;
        for(i=31;i>=0;i--){
            if(i%8==7){
                sum[++j]=0;
            }
            if(str[i]=='1'){
                sum[j]+=a[7-i%8];
            }
        }
        printf("%d.%d.%d.%d\n",sum[4],sum[3],sum[2],sum[1]);
    }
}

时间: 2024-09-20 07:35:08

POJ 2105 IP Address的相关文章

POJ 2105 IP Address (strtoll函数)

Time Limit: 1000MS Memory Limit: 30000K Description Suppose you are reading byte streams from any device, representing IP addresses. Your task is to convert a 32 characters long sequence of '1s' and '0s' (bits) to a dotted decimal format. A dotted de

poj 2105 IP Address【模板 power2】

这道题就是一个字符串处理的问题... 一次AC,很爽... #include <stdio.h> char IP[35]; //存放最开始的二进制数 int address[5]; //存放最后的答案 int power2(int n) { //自己写的2的次方 int i; int result=1; for (i=0;i<n;i++) result<<=1; //不停 *2 return result; } //将二进制转成4个十进制 void work() { int

[J2MEQ&amp;A]WTK初始化WMAClient报错XXX has no IP address的解释

[J2ME]WTK初始化WMAClient报错XXX has no IP address的解释   [现象] 当通过WTK运行一个J2ME程序时,KToolbar显示这样的错误: 错误形态一 WMAClient: localhost has no IP address: java.net.UnknownHostException: xxxx: xxxx Warning: Failed to initialize WMA message routing support ClientConnecto

Get ip address from hostname in C using Linux sockets

Here are 2 methods to get the ip address of a hostname : The first method uses the traditional gethostbyname function to retrieve information about a hostname/domain name.Code 1 #include<stdio.h> //printf 2 #include<string.h> //memset 3 #inclu

获取本机外网IP地址(External IP Address)示例

原文:http://www.cnblogs.com/Csharpblogs/articles/2218156.html import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Matcher; import

Virtual IP Address的实现

VIP在HA的使用 目前的互联网业务对服务器的依赖度极高,几乎不能容忍非计划的停机.但是机器的故障是无法100%避免的,所以,出现了HA技术,使用冗余的来保证服务可用. 但是,光有冗余还不行,还需要检测故障,故障切换. 目前的流程就是: 正常情况时,灾备系统只是监控服务器的运行. 服务器宕机,灾备系统发现故障. 灾备系统把流量切换到备份服务器. VIP就是用在最后一步,它保证了灾备切换的透明.因为IP无需改变. 一种VIP实现方法 有一种方法就是通过ARP协议实现VIP,因为在数据链路层,数据帧

Virtual IP address

只是翻译一下维基百科的内容. 概念 虚拟IP地址(VIP或VIPA)是一个IP地址指向多个应用程序驻留的单个服务器,多个域名,或者多个服务器,而不是被指向一个特定的服务器或网卡(NIC).传入的数据包会发送到虚拟地址并路由到真实的网络接口. 一个服务器IP地址依赖于网卡上的MAC地址.并且每一个逻辑IP地址只能指向一个网卡.然而,VIP地址能够让若干不同应用和虚拟应用的服务器集合只使用一个逻辑IP地址. VIP有若干变化和实现场景,包括通用地址冗余协议(CARP)和代理地址解析协议(proxy

The host file--map a name to an IP address

The host file is located at \windows\system32\drivers\etc\* and the format is ----------------------------------------------------------------------------------------------------------------- # Copyright (c) 1993-2009 Microsoft Corp. # # This is a sa

Genymotion模拟器无法开启的解决方法——Unable to start the virtual device,The virtual device got no IP address

前言 最近重装了电脑的系统,由win7换成了win8.1.android开发环境也重新配置了一遍.其他的都还好,就是genymotion模拟器一直开启失败. 自己尝试了很多方法,比如卸载重装软件,重新设置VirtualBox的IP等等.但一直都解决不了. 解决方法 Google了一下我的问题,在stackoverflow上找到了解决方法.在此分享给大家: 打开VirtualBox,找到我们的android虚拟机,设置-->系统-->将内存大小调小即可. 原文地址:http://www.cnbl