/*=====================================
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. 在windows上搭建ftp服务

    在控制面板->程序和功能->打开或关闭Windows功能中开启ftp和IIS信息服务管理器 在控制面板->管理工具中打开Internet信息服务管理器->添加ftp站点 建好之 ...

  2. BZOJ 2241 打地鼠

    暴力. 这怎么这么快.... #include<iostream> #include<cstdio> #include<cstring> #include<a ...

  3. 初次使用百度地图API

    因为项目需要,不得不使用百度地图的API,以前从未了解过API,这不是唬人,真的,所以对百度地图API充满了恐惧,但是到后面,已经麻木了.期间遇到过很多错误,每一个都弄得头大,借博客的名义把平时遇到的 ...

  4. html网页标题

    HTML代码 <html> <head> <!--<title>定义网页标题,显示在浏览器的标题--> <title>网页标题</ti ...

  5. 【LeetCode OJ】Reverse Words in a String

    Problem link: http://oj.leetcode.com/problems/reverse-words-in-a-string/ Given an input string, reve ...

  6. Windows共享设定-使用net use添加网络盘带上账号密码

    食欲 net use \\10.11.1.2\ipc$ /user:dmnm\usr "pwd"

  7. 7、网页制作Dreamweaver(悬浮动态分层导航)

    悬浮动态分层导航的制作: 1.首先在<head>里面引用一个JQUERY的文件以用来制作鼠标点击动画效果(从网站上下载即可) <script language="javas ...

  8. WPF Step By Step 自定义模板

    WPF Step By Step 自定义模板 回顾 上一篇,我们简单介绍了几个基本的控件,本节我们将讲解每个控件的样式的自定义和数据模板的自定义,我们会结合项目中的具体的要求和场景来分析,给出我们实现 ...

  9. Linux Qt动态库的创建和使用

    一.创建动态库 编写一个共享库类,比如: //..base.h class Base : public QObject { Q_OBJECT public: ); void PrintLog(QStr ...

  10. LeetCode OJ String to Integer (atoi) 字符串转数字

    #include <iostream> #include <assert.h> using namespace std; int ato(const char *str) { ...