题目链接:

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. cimge 这次能够图片大小尺寸

    CImage imSrc,imDest;imSrc.Load(……);//读入原始图片imDest.Create(……)//创建新大小的图片imSrc.StretchBlt(imDest.GetDC( ...

  2. css3制作优惠券

    <div class="demo-container demo"><style> .demo{width:410px;} .stamp *{padding: ...

  3. java web 学习十五(jsp基础语法)

    任何语言都有自己的语法,JAVA中有,JSP虽然是在JAVA上的一种应用,但是依然有其自己扩充的语法,而且在JSP中,所有的JAVA语句都可以使用. 一.JSP模版元素 JSP页面中的HTML内容称之 ...

  4. Android ListView从网络获取图片及文字显示

    上一篇文章说的是ListView展示本地的图片以及文本,这一篇说一下如何从网络获取图片以及文本来显示.事实上,一般是先获取Josn或sml数据,然后解释显示.我们先从网上获取xml,然后对其进行解析, ...

  5. 《Python核心编程》 第八章 条件和循环

    8–1.条件语句. 请看下边的代码 # statement A if x > 0: # statement B pass elif x < 0: # statement C pass el ...

  6. js控制不同的时间段显示不同的css样式

    js控制不同的时间段显示不同的css样式 js函数,可以放到单独的js文件中也可以放到当前页的<head>标记之内 function getCSS(){        datetoday ...

  7. (三)相遇射线的3D碰撞盒

    序 在2D游戏中,我们知道处理碰撞时,需要设置精灵遮罩图.同样,进入3D,处理碰撞时需要3D模型作为“遮罩图”. 索尼克 飞檐走壁   目的 (1)处理模型间的碰撞问题         (2)获取鼠标 ...

  8. 【和我一起学python吧】python入门语法总结

    1.python是一个解释性语言: 一个用编译性语言比如C或C++写的程序可以从源文件(即C或C++语言)转换到一个你的计算机使用的语言(二进制代码,即0和1).这个过程通过编译器和不同的标记.选项完 ...

  9. 在虚拟机VM中安装的Ubuntu上安装和配置Hadoop

    一.系统环境: 我使用的Ubuntu版本是:ubuntu-12.04-desktop-i386.iso jdk版本:jdk1.7.0_67 hadoop版本:hadoop-2.5.0 二.下载jdk和 ...

  10. java计时代码

    public class Test{ public static void main(String[] args) { long startMili=System.currentTimeMillis( ...