华东师大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型的数字呢?答案是肯定的.在数据库层面,可以直接将结 ...
随机推荐
- vector 初始化
//数组初始化vector int iarray[]={1,2,3,4,5,6,7,8,9,0}; //count: iarray数组个数 size_t count=sizeof(iarray)/si ...
- BZOJ 1486 最小圈
二分答案是显然的,我们需要dfs版spfa判一下负环. 看起来是n^2其实很快. #include<iostream> #include<cstdio> #include< ...
- php5.2 连接 SQL Server2008
如果你见到下面这一段输出的话,那么你有福了!!!! Array ( [0] => Array ( [0] => IMSSP [SQLSTATE] => IMSSP [1] => ...
- array_count_values函数
array_count_values() 函数用于统计数组中所有值出现的次数. 本函数返回一个数组,其元素的键名是原数组的值,键值是该值在原数组中出现的次数. 例子 <?php $a=array ...
- 2012开源项目计划-WPF企业级应用整合平台
2012开源项目计划-WPF企业级应用整合平台 开篇 2012年,提前祝大家新年快乐,为了加快2012年的开发计划,特打算年前和大家分享一下2012年的开发计划和年后具体的实施计划,希望有兴趣或者有志 ...
- 根据域名获取IP地址,并探测是否可达
/* Author :decwang@2014.09.01 Mail :deworks@sina.com*/#define PRINTLOG printf//返回0表示成功,其他为失败. int ge ...
- Oracle字符集的查看查询和Oracle字符集的设置修改
本文主要讨论以下几个部分:如何查看查询oracle字符集. 修改设置字符集以及常见的oracle utf8字符集和oracle exp 字符集问题. 一.什么是Oracle字符集 Oracle字符集是 ...
- CString用法总结
概述:CString是MFC中提供的用于处理字符串的类,是一种很有用的数据类型. 它很大程度上简化了MFC中的许多操作,使得MFC在做字符串操作时方便了很多. 不管怎样,使用CString有很多的特殊 ...
- pthread_join和pthread_detach的用法(转)
一:关于join join join是三种同步线程的方式之一.另外两种分别是互斥锁(mutex)和条件变量(condition variable). 调用pthread_join()将阻塞自己,一直到 ...
- 关于display的那些事儿!
关于display的那些事儿! display,display,display!嘿嘿嘿!display这一CSS属性,还是相当神奇的哦!给它设置不同的值,被修饰的标签相应的就随之变换了自身的属性特性, ...