【习题 4-5 Uva1590】 IP Networks
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
假设从第i位开始有不一样的。
那么就把i+1..32位全都置0.
掩码的话类似。前i为全为1,后面32-i位全0.
尽量让后面的连续0(等于掩码最后的0的个数)晚一点出现。
这样网络表示的范围比较小(可供变化的范围比较小)。
【代码】
#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
using namespace std;
const int N = 1e3;
int n;
string ips[N+10];
string get_format(string s){
string temp = "";
int x = 0;
rep1(i,1,32){
x = x*2+(s[i-1]-'0');
if (i%8==0){
string temp1 = "";
if (x==0) temp1 = "0";
while (x>0){
temp1 =(char)(x%10+'0')+temp1;
x/=10;
}
temp = temp + temp1;
if (i<32) temp+=".";
}
}
return temp;
}
int main(){
//freopen("/home/ccy/rush.txt","r",stdin);
// freopen("/home/ccy/rush_out.txt","w",stdout);
ios::sync_with_stdio(0),cin.tie(0);
while (cin >> n){
rep1(i,1,n){
ips[i]="";
string s;
cin >> s;s+=".";
int x= 0;
rep1(j,0,(int)s.size()-1){
if (s[j]=='.'){
string temp = "";
rep1(k,1,8){
int temp1 = x%2;
temp = (char)(temp1+'0')+temp;
x/=2;
}
ips[i]+=temp;
x = 0;
}else{
x = x*10+s[j]-'0';
}
}
//cout<<ips[i]<<endl;
}
string ans1 = "";
string ans2 = "";
for (int i = 0;i < 32;i++){
int ok = 1;
rep1(j,1,n)
if (ips[j][i]!=ips[1][i])
ok = 0;
if (ok){
ans1 = ans1 + ips[1][i];
ans2 = ans2 + "1";
}else{
rep1(j,i,31) {
ans1+="0";
ans2+="0";
}
break;
}
}
//cout<<ans1<<endl;
//cout<<ans2<<endl;
cout<<get_format(ans1)<<endl;
cout<<get_format(ans2)<<endl;
}
return 0;
}
【习题 4-5 Uva1590】 IP Networks的更多相关文章
- [刷题]算法竞赛入门经典(第2版) 4-5/UVa1590 - IP Networks
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,0 ms) //UVa1590 - IP Networks #include<iost ...
- uva 1590 - IP Networks(IP地址)
习题4-5 IP网络(IP Networks, ACM/ICPC NEERC 2005, UVa1590) 可以用一个网络地址和一个子网掩码描述一个子网(即连续的IP地址范围).其中子网 掩码包含32 ...
- IP Networks UVA - 1590
Alex is administrator of IP networks. His clients have a bunch of individual IP addresses and he de ...
- UVA 1590 IP Networks JAVA
题意:输入m代表接下来的数据个数,计算接下来输入数据的网络掩码,和最小网络地址. 思路:①子网掩码:先将数据转为二进制,判断从哪一位开始有数据不一样,记下下标index,则子网掩码是index的前面是 ...
- Endless looping of packets in TCP/IP networks (Routing Loops)
How endless looping of packets in a TCP/IP network might occur? Router is a device used to interconn ...
- ease of rerouting traffic in IP networks without readdressing every host
https://en.wikipedia.org/wiki/Network_address_translation In the face of the foreseeable global IP a ...
- Uva 1590 IP Networks
这道题目是一道关于IP地址的题目,要深入理解这道题需要有一定的网络基础. 这道题目我第一次做的时候虽然也AC了,但代码写的比较复杂,不够精炼.近期刚刚参加了网络方面的培训,在有一定知识的基础上,又重写 ...
- POJ 2799 IP Networks
network address是前(32-n)随意 后n位全零 network mask是前(32-n)全一 后n位全零 本题主要利用位移操作,1ULL表示无符号长整型的常数1,这样写可防止不必要的溢 ...
- 一个CentOS7的开发环境部署,包括防火墙|VPN|多IP多网关|HTTP代理服务器设置等
http://www.lenggirl.com/code/centos7.html layout: post title: "一个CentOS7的开发环境部署,包括防火墙|VPN|HTTP代 ...
随机推荐
- CF 557A(Ilya and Diplomas-贪心)
A. Ilya and Diplomas time limit per test 1 second memory limit per test 256 megabytes input standard ...
- USACO Section 2.1 Ordered Fractions
/* ID: lucien23 PROG: frac1 LANG: C++ */ #include <iostream> #include <fstream> #include ...
- Nginx1.6 for centos6.5安装
Nginx 学习文档:http://www.cnblogs.com/zhoulf/archive/2013/02/09/2909653.html 下载地址:http://nginx.org/en/do ...
- QT 4.53 for VS2005 编译包
最近学QT,装了QT5,结果打开下载的项目(特别是那些书的源代码)有很多兼容性问题.于是又下载4.83,都是4版本系列的,应该会好很多吧,但居然也是这样.于是看了一下时间,觉得4.53于2009年底发 ...
- Single document interface和Multiple document interface
https://en.wikipedia.org/wiki/Single_document_interface https://msdn.microsoft.com/en-us/library/b2k ...
- bzoj4197 [Noi2015]寿司晚宴——状压DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4197 首先,两个人选的数都互质可以看作是一个人选了一个数,就相当于选了一个质因数集合,另一个 ...
- PCB MS CLR 聚合函数 joinString加排序实现
准备着手,动态列SQL查询,要实现动态列SQL,会运用到聚合函数,而SQL本身聚合函数有限, 无满足此用户任意字段组合变化,再加上工艺流程重复相同工序,如;沉铜1,沉铜2对应工序代码都是相同的 而通常 ...
- selenium3 + python - gird分布式(转载)
本篇转自博客:上海-小T 转载链接:https://blog.csdn.net/real_tino/article/details/53467406 Selenium grid是用来分布式执行测试用例 ...
- thinkphp3.2 验证码生成和点击刷新验证码
生成验证码的时候: public function verify_c(){ $Verify = new \Think\Verify(); $Verify->fontSize = 18; $Ver ...
- SAP computer之architecture
Simple-As-Possible computer introduces all the cruicial ideas behind computer operation without bury ...