北大ACM2105——IP Address

这题,水题一个,题目的意思是将输入的32位二进制数,每八位转换成十进制,以IP地址的形式输出。

下面的是AC的代码:

#include <iostream>
#include <cstring>
using namespace std;

int num[8] = {128, 64, 32, 16, 8, 4, 2, 1};

int main()
{
//	freopen("data.txt", "r", stdin);
	int N;
	char str[40];
	cin >> N;
	while(N--)
	{
		cin >> str;
		int length = strlen(str);
		int k = 0;
		for(int i = 0; i < length; i++)
		{
			if(i % 8 == 0 && i)
			{
				cout << k << '.';
				k = 0;
			}
			k += (str[i] - '0') * num[i % 8];
		}
		cout << k << endl;
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-31 17:56:02

北大ACM2105——IP Address的相关文章

在windows下运行docker的问题【Error getting IP address: ***】

环境配置系统:windows 10docker:Docker Toolbox https://www.docker.com/products/docker-toolbox 问题描述windows下安装完Docker Toolbox后运行Docker Quickstart Terminal可能会看到如下错误 Creating Machine default... Running pre-create checks... Creating machine... Error creating mach

Java Regex match IP address

Reference: [1] https://www.mkyong.com/regular-expressions/how-to-validate-ip-address-with-regular-expression/ import java.util.regex.Matcher; import java.util.regex.Pattern; public class IPAddressValidator{ private Pattern pattern; private Matcher ma

router 分配ip address(dhcp)和dhcp中继

路由器的主要功能是网络寻址,有时候也需要用来分配ip地址.但用路由器分配地址又会加大路由器的负载,所以,有的时候我们可以把分配地址的工作交给一台PC. 1.使用router分配ip address: Router>en Router#conf t Enter configuration commands, one per line.  End with CNTL/Z. Router(config)#int vlan 1 Router(config-if)#ip dhcp pool vlan1  

poj2105 IP Address(简单题)

题目链接:http://poj.org/problem?id=2105 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 decima

Debian static ip address

1 // /etc/network/interfaces 2 auto eth0 3 iface eth0 inet static 4 address <Your ip address> 5 netmask <Your netmask> 6 gateway <Your gateway address> 7 broadcast <Your broadcast address> 8 9 // /etc/resolv.conf 10 nameserver <

If application data needs to be sent to IP address xx.xx.xx.xx, how does it work in underneath network?

This is to illustrate the communication between two separate machines which don't have a direct physical connection. Application data is generated and passed to layer 3 (network layer) which wrapps up the data with the destination IP address. Then ha

Bringing up interface eth0: Determining if ip address 10.109.67.81 is already in use for device eth0...

重启网卡出现提示: Bringing up interface eth0:  Determining if ip address 10.109.67.81 is already in use for device eth0...                                                            [  OK  ] Bringing up interface eth1:  Determining if ip address 10.109.67.83

oracle 11g RAC安装节点二执行结果错误CRS-5005: IP Address: 192.168.1.24 is already in use in the network

[[email protected] ~]# /u01/app/oraInventory/orainstRoot.sh Changing permissions of /u01/app/oraInventory. Adding read,write permissions for group. Removing read,write,execute permissions for world. Changing groupname of /u01/app/oraInventory to oins

Linux下network提示Determining if ip address

[[email protected] NetworkManager]# service network restart正在关闭接口 eth0: [确定]关闭环回接口: [确定]弹出环回接口: [确定]弹出界面 eth0: Determining if ip address 192.168.8.102 is already in use for device eth0... vi /etc/sysconfig/network-scripts/ifcfg-eth0 添加   ARPCHECK=no