POJ 2105 IP Address

简介: 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]);
    }
}
目录
相关文章
|
7天前
|
云安全 监控 安全
|
12天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1352 8
|
6天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
427 10
|
18天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
1233 43
|
18天前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
1083 86
大厂CIO独家分享:AI如何重塑开发者未来十年
|
14天前
|
存储 自然语言处理 测试技术
一行代码,让 Elasticsearch 集群瞬间雪崩——5000W 数据压测下的性能避坑全攻略
本文深入剖析 Elasticsearch 中模糊查询的三大陷阱及性能优化方案。通过5000 万级数据量下做了高压测试,用真实数据复刻事故现场,助力开发者规避“查询雪崩”,为您的业务保驾护航。
627 32