题目链接:

id=2105">http://poj.org/problem?id=2105

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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

题意:非常easy, 就是每一个案例给出一个32位的2进制数字串。 要求依照每8位转换为8进制输出(中间有‘.’)就可以!

代码例如以下:

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int N;
int i, j;
char s[117];
while(cin >> N)
{
while(N--)
{
cin>>s;
int ans[4], k = 7, l = 0;
int sum = 0;
for(i = 0; i < 32; i++)
{
sum +=(s[i]-'0')*pow(2.0,k);
k--;
if(i%8==7)
{
ans[l++] = sum;
sum = 0;
k = 7;
}
}
cout<<ans[0]<<'.'<<ans[1]<<'.'<<ans[2]<<'.'<<ans[3]<<endl;
}
}
return 0;
}

poj2105 IP Address(简单题)的更多相关文章

  1. poj 2105 IP Address(水题)

    一.Description Suppose you are reading byte streams from any device, representing IP addresses. Your ...

  2. [LeetCode] 1108. Defanging an IP Address

    Description Given a valid (IPv4) IP address, return a defanged version of that IP address. A defange ...

  3. [LintCode] Restore IP Address 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  4. 华东师大OJ:IP Address【IP地址转换】

    /*===================================== IP Address Time Limit:1000MS Memory Limit:30000KB Total Subm ...

  5. UVa 1590 IP网络(简单位运算)

    Description   Alex is administrator of IP networks. His clients have a bunch of individual IP addres ...

  6. [LeetCode] Restore IP Address [28]

    题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...

  7. LeetCode 1108. Defanging an IP Address (IP 地址无效化)

    题目标签:String 题目给了我们一组 ip address,让我们把 . 变成 [.],这题可以用replace,但是这样做的话,好像没意义了.所以还是走一下array,具体看code. Java ...

  8. hectf2020部分简单题题解wp

    HECTF 我真是又菜又没时间肝题..又又又只水了波简单题... Reverse 1.Hello_Re file查一波 32bit,拖进IDA中 老规矩shift+F12 查看字符串: 跳转 F5查看 ...

  9. 【LeetCode】468. Validate IP Address 解题报告(Python)

    [LeetCode]468. Validate IP Address 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

随机推荐

  1. LeetCode: Interval

    (1)Merge Intervals https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, m ...

  2. 深入学习Oracle分区表及分区索引

    关于分区表和分区索引(About Partitioned Tables and Indexes)对于10gR2而言,基本上可以分成几类: •       Range(范围)分区 •       Has ...

  3. Java基础——异常处理

    异常的层次结构 所有的异常类都是 java.lang.Exception 类的子类型.异常类都是 Throwable 类的子类.除了异常类 Error 类也是由 Throwable 类产生的的子类1. ...

  4. delphi 转换sql server 中的 bit类型

    FieldByName('e').AsBoolean = false 其中e为 sql server 中的bit类型.

  5. java 中的Exception RuntimeException 区别

    在java的异常类体系中: 1.Error和RuntimeException是非检查型异常,其他的都是检查型异常; 2.所有方法都可以在不声明throws的情况下抛出RuntimeException及 ...

  6. Free Pascal的IDE界面乱码解决方法

    右击[Free Pascal]快捷图标,选[属性] [属性]窗口中,选择[选项]页,下拉[当前代码页(C)],选择[437 (美国)] 按[确定] 注意:[开始]菜单和桌面中的快捷图标都得设置.

  7. storm入门教程 第一章 前言[转]

    1.1   实时流计算 互联网从诞生的第一时间起,对世界的最大的改变就是让信息能够实时交互,从而大大加速了各个环节的效率.正因为大家对信息实时响应.实时交互的需求,软件行业除了个人操作系统之外,数据库 ...

  8. 使ViewFlipper中的WebView实现手势效果

    使ViewFlipper中的WebView实现手势效果   今天写Blog阅读器的时候遇到了这个问题,把它分享给大家,让同样是新手们少走冤枉路始初写这个功能的时候,用过了好多方法,也耗了不少时间去研究 ...

  9. 双nginx(主备、主主)反向代理tomcat实现web端负载均衡

    经过以前做完的产品,受前公司几位前辈技术大拿指点,来自己动手实现并总结一下web端的负载解决方法,高手请略过,个人认知有限,请各位指正错误. 下面是结构图: 我的系统环境是Fedora22(适用rea ...

  10. TP分析

    http://blog.csdn.net/l627859442/article/details/7633457 http://blog.chinaunix.net/uid-27717694-id-37 ...