/*=====================================
IP Address
Time Limit:1000MS Memory Limit:30000KB
Total Submit:3394 Accepted:1530 Special Judge
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:
2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0
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
Source
Mexico and Central America Region 2004
======================================*/

题目大概意思就是输入N,再输入N行0/1序列串,每个0/1序列串都是32bit的字符,

要求把每个0/1序列串转换成IP地址并输出。对每一行0/1序列串输出一行,就是对应的IP地址。

思路:

  其实挺简单的,主要是注意输入时按字符输入,输入后要转成数字。输入字符要注意清空行尾的回车符。

 #include<stdio.h>
int main()
{
int a[]={},i,j,k;
char ch;
int N;
freopen("ip.in","r",stdin);
scanf("%d",&N);
getchar();//接收回车符
for(k=;k<N;k++)
{
for(i=;i<;i++)
{
a[i]=;
for(j=;j<;j++)
{
ch=getchar();
a[i]=a[i]*+(ch-'');
}
}
getchar();//接收回车符
printf("%d.%d.%d.%d\n",a[],a[],a[],a[]);
}
return ;
}

华东师大OJ:IP Address【IP地址转换】的更多相关文章

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

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

  2. 套接字编程相关函数(1:套接字地址结构、字节序转换、IP地址转换)

    1. 套接字地址结构 1.1 IPv4套接字地址结构 IPv4套接字地址结构通常也称为“网际套接字地址结构”,它以sockaddr_in命名,定义在<netinet/in.h>头文件中.下 ...

  3. python整数与IP地址转换

    python整数与IP地址转换 [转] 我们有时会将一个整数与IP地址进行互换,用python代码实现很简单将一个整数如2000000,变为一个IP地址的方式 >>> import ...

  4. lwip IP address handling 关于 IP 地址的 操作 API接口

    lwip 2.0.3  IP address handling /** * @file * IP address API (common IPv4 and IPv6) */ 1.u32_t ipadd ...

  5. python整数与IP地址转换 [转]

    我们有时会将一个整数与IP地址进行互换,用python代码实现很简单将一个整数如2000000,变为一个IP地址的方式 >>> import socket >>> ...

  6. ip地址转换

    通常,我们用点分十进制字符串表示ipv4地址(192.168.1.1),用十六进制字符串表示ipv6地址(fe80::20c:29ff:fee9:4bcc). ipv4转换函数 #include &l ...

  7. [转]字符型IP地址转换成数字IP的SQL函数

    使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助.      /**//*--调用示例       sele ...

  8. 华为上机:IP地址转换

    IP地址转换 描述: IP地址的长度为32,即有2^32-1个地址.IP地址一般采用点分十进制表示法,例如"192.168.1.1".IP地址也可以直接用一个32位的整数进行表示. ...

  9. IP地址转换成Long型数字的算法

    在应用程序开发中,涉及到IP地址的存储,大部分开发人员都将其存为String(或文本类型).能否将固定格式为m.n.x.y的IP地址转换成 Long型的数字呢?答案是肯定的.在数据库层面,可以直接将结 ...

随机推荐

  1. 第一个PHP程序

    <html> <head> <title><?php echo"这是第一个php程序"?></title> <st ...

  2. susy-Toolkit 之翻译

    Toolkit工具包 The Susy 2.0 toolkit is built around our shorthand syntax. Use the shorthand to control e ...

  3. MonogoDB的GirdFS

    GirdFS是一种在MongoDB中存储大二进制文件的机制. mongofiles内置在MongoDB发布版中,可以用来在GridFS中上传.下载.列示.查找或删除文件. $ echo "H ...

  4. python类的定义和使用

    python中类的声明使用关键词class,可以提供一个可选的父类或者说基类,如果没有合适的基类,那就用object作为基类. 定义格式: class 类名(object): "类的说明文档 ...

  5. IOS源码封装成.bundle和.a文件,以及加入xib的具体方法,翻遍网络,仅此一家完美翻译!! IOS7!!(3) 完美结局

    以上翻译有误解之处,现在简单做法如下: 经过深入研究,才感觉明白了内部机制,现在简单介绍于下,主要步骤:xcode5 创建库项目,删掉测试文件和默认创建的类,添加viewController类带xib ...

  6. BS模式的模型结构详解

    编号:1004时间:2016年4月12日16:59:17功能:BS模式的模型结构详解 URL:http://blog.csdn.net/icerock2000/article/details/4000 ...

  7. toLocaleString

  8. spark mllib k-means算法实现

    package iie.udps.example.spark.mllib; import java.util.regex.Pattern; import org.apache.spark.SparkC ...

  9. iOS学习笔记---网络请求

    一.HTTP协议的概念 HTTP协议:Hyper Text Transfer Protocol(超文本传输协议)是用于从万维网服务器传送超文本到本地浏览器的传输协议.HTTP是一个应用层协议,由请求和 ...

  10. 268. Missing Number

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...