【2015广东工业大学新生赛E】【c++函数 bitset读入二进制转十进制数】GDUT的实验室 十进制与二进制的比较

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
int casenum,casei;
UI a[4];
bitset<32>b[4];
bool check()
{
	for(int i=0;i<4;++i)if(a[i]!=b[i].to_ulong())return 0;
	return 1;
}
int main()
{
	scanf("%d",&casenum);
	for(casei=1;casei<=casenum;++casei)
	{
		for(int i=0;i<4;++i)scanf("%u%*c",&a[i]);
		for(int i=0;i<4;++i)cin>>b[i],cin.get();
		puts(check()?"Yes":"No");
	}
	return 0;
}
/*
【trick&&吐槽】
bitset读入二进制数很棒哦~

c++的读入——
数据按十进制输入输出。
如果要求按八进制或十六进制输入输出,在cin或cout中必须指明相应的数据形式,
oct为八进制,hex为十六进制,dec为十进制。
即cin>>oct(hex)>>x

c的读入——
%o 读入八进制数
%x 读入十六进制数

【题意】
192.168.1.1
11000000.10101000.00000001.00000001
65.254.63.122
01000001.11111110.00111111.01111011

给你二进制IP编码和十进制IP编码,让你判断两者是否一致。

【类型】
简单模拟 bitset

【分析】
这题可以按位模拟,把二进制转化为数值。
当然也可以利用bitset,让系统帮你转,2333!

我们可以直接用bitset读入二进制位。
而且,bitset的读入可以看做是从右往左读的,哎呀,不管是怎么读入的,总之,从结果上看,就是——b[0]是最低位。
然后,我们可以使得bitset转化为一个数。

具体函数包括——
bitset.to_string()
bitset.to_ulong()
bitset.to_ullong()

总之几行代码就可以AC啦!

*/

    原文作者:进制转换
    原文地址: https://blog.csdn.net/snowy_smile/article/details/50209401
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞