华东师大OJ:IP Address【IP地址转换】
/*=====================================
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地址转换】的更多相关文章
- LeetCode 1108. Defanging an IP Address (IP 地址无效化)
题目标签:String 题目给了我们一组 ip address,让我们把 . 变成 [.],这题可以用replace,但是这样做的话,好像没意义了.所以还是走一下array,具体看code. Java ...
- 套接字编程相关函数(1:套接字地址结构、字节序转换、IP地址转换)
1. 套接字地址结构 1.1 IPv4套接字地址结构 IPv4套接字地址结构通常也称为“网际套接字地址结构”,它以sockaddr_in命名,定义在<netinet/in.h>头文件中.下 ...
- python整数与IP地址转换
python整数与IP地址转换 [转] 我们有时会将一个整数与IP地址进行互换,用python代码实现很简单将一个整数如2000000,变为一个IP地址的方式 >>> import ...
- 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 ...
- python整数与IP地址转换 [转]
我们有时会将一个整数与IP地址进行互换,用python代码实现很简单将一个整数如2000000,变为一个IP地址的方式 >>> import socket >>> ...
- ip地址转换
通常,我们用点分十进制字符串表示ipv4地址(192.168.1.1),用十六进制字符串表示ipv6地址(fe80::20c:29ff:fee9:4bcc). ipv4转换函数 #include &l ...
- [转]字符型IP地址转换成数字IP的SQL函数
使用SQL函数可以实现许多的功能,下面为您介绍的是字符型IP地址转换成数字IP的SQL函数示例,供您参考,希望对您学习SQL函数能够有所帮助. /**//*--调用示例 sele ...
- 华为上机:IP地址转换
IP地址转换 描述: IP地址的长度为32,即有2^32-1个地址.IP地址一般采用点分十进制表示法,例如"192.168.1.1".IP地址也可以直接用一个32位的整数进行表示. ...
- IP地址转换成Long型数字的算法
在应用程序开发中,涉及到IP地址的存储,大部分开发人员都将其存为String(或文本类型).能否将固定格式为m.n.x.y的IP地址转换成 Long型的数字呢?答案是肯定的.在数据库层面,可以直接将结 ...
随机推荐
- My_Python的常用函数.
范围生成函数 class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range ...
- 产生冠军 map 的 应用 .
开始 比赛 , 每一次的 比赛 都会有人失败 , 如果产生英雄的话 , 那就是产生 唯一一个 没有被打败的人 , 就是英雄, . #include<stdio.h> #includ ...
- csdn第八名
编号:1028时间:2016年7月18日11:49:17功能:csdn第八名URL :http://blog.csdn.net/21aspnet
- Carthage
Carthage Carthage - 一个简单.去集中化的Cocoa依赖管理器
- Optimizing shaper — hashing filters (HTB)
I have a very nice shaper in my linux box :-) How the configurator works — it’s another question, he ...
- Core Java Volume I — 3.1. A Simple Java Program
Let’s look more closely at one of the simplest Java programs you can have—one that simply prints a m ...
- vmware上的Linux获取uuid
在挂载asm硬盘时需要硬盘的UUID 虚拟机配置中需要增加对UUID的支持. 在配置文件vmx文件中增加如下内容 disk.locking="FALSE" disk.EnableU ...
- autoproxy 规则
目前在 gfwList 中,有如下的规则格式: example.com 匹配:http://www.example.com/foo匹配:http://www.google.com/search?q=w ...
- Linux启动Apache支持.htaccess伪静态文件方法
第一.编辑httpd.conf文件 A - 在etc/httpd/conf/目录下的httpd.conf 文件,找到: LoadModule rewrite_module modules/mod_re ...
- 203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...