/*=====================================
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. 又见蒙特卡洛——python模拟解决三门问题

    三门问题很有意思,wiki用不同方法将原理讲的很透彻了,我跟喜欢其中这种理解方式:无论参赛者开始的选择如何,在被主持人问到是否更换时都选择更换.如果参赛者先选中山羊,换之后百分之百赢:如果参赛者先选中 ...

  2. 在php中需要用到的mysql数据库的简单操作

    1.数据库连接 1.1用windows命令行链接数据库服务器 几个DOS命令 在DOS环境下命令后面没有分号,在MySQL环境下,命令后面有分号 进入盘符: 语法:盘符: 进入盘符下的某个文件夹 语法 ...

  3. 9、SQL基础整理(两表连接exists,join on,union)

    exists的用法 select *from haha where exists (select *from bumen where bumen.code = haha.bumen and bumen ...

  4. 七、CCScene

    CCScene一般情况是游戏里面的根节点,称之为"场景",运行游戏时需要通过CCDirector启动第一个场景.当然,游戏稍微复杂一点的话,可能会包含很多个场景,这就涉及到场景的切 ...

  5. 在SourceInsight中用快捷键打开文件所在的目录

    创建一个Custom Command:  ShellExecute open %d.  然后关联一个快捷键.

  6. 决策树模型 ID3/C4.5/CART算法比较

    决策树模型在监督学习中非常常见,可用于分类(二分类.多分类)和回归.虽然将多棵弱决策树的Bagging.Random Forest.Boosting等tree ensembel 模型更为常见,但是“完 ...

  7. Mapnik 编译安装过程

    首先总结一下,Linux(windows上没有测试过)上Mapnik的编译与测试就是一部心酸血泪史呀,如果您没有做好思想准备,那就出门左转,看点有意思的去吧,编译这个太煎熬了. 安装PostgreSQ ...

  8. Debian 入门安装与配置1

    Debian 入门安装与配置1 最近安装了多个发行版本的Linux,包括Ubuntu.Fedora.Centos和Debian,发现只有Debian在界面和稳定性等综合特性上表现最优,自己也最喜欢,所 ...

  9. Think Python - Chapter 03 - Functions

    3.1 Function callsIn the context of programming, a function is a named sequence of statements that p ...

  10. Why did Jimmy Wales invest in Quora? Is he afraid that it will take over Wikipedia?

    QUESTION: Why did Jimmy Wales invest in Quora? Is he afraid that it will take over Wikipedia? Answer ...