题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3257
2 11 7F 08 08 08 7F 38 54 54 54 18 00 41 7F 40 00 00 41 7F 40 00 38 44 44 44 38 00 00 00 00 00 3F 40 38 40 3F 38 44 44 44 38 7C 08 04 04 08 00 41 7F 40 00 38 44 44 48 7F 5 14 08 3E 08 14 04 02 01 02 04 40 40 40 40 40 04 02 01 02 04 14 08 3E 08 14
Case 1: # # ## ## # # ## # # # # # # # # # # # ### # # ### # # ### # ## # ## # ##### # # # # # # # # # # # ## # # # ## # # ##### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ### ### ### ### # # ### # ### #### Case 2: # # # # # # # # # # # # # # # # # # ### ### # # # # # # # # #####
思路:
首先想到这题是十六进制转换为二进制,我们先令tt为1,然后在用位运算向左移动(<<),
每移动一次就让所有的a[]和当前的tt&(与)一次,如果值为1那么就说明当前的a[i]的二进制
中在此处有1,也就是应该输出“#”,(主要是用到了位运算的‘<<’和‘&’);
再细细体味一下吧!
代码如下:
//#pragma warning (disable:4786) #include <cstdio> #include <cmath> #include <cstring> #include <string> #include <cstdlib> #include <climits> #include <ctype.h> #include <queue> #include <stack> #include <vector> #include <utility> #include <deque> #include <set> #include <map> #include <iostream> #include <algorithm> using namespace std; const double eps = 1e-9; //const double pi = atan(1.0)*4; const double pi = 3.1415926535897932384626; #define INF 1e18 //typedef long long LL; //typedef __int64 LL; #define MAXN 80*5+47 int main() { int t; int n; int a[MAXN]; int cas = 0; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i = 0; i < 5*n; i++) { scanf("%X",&a[i]); } int tt; printf("Case %d:\n\n",++cas); for(int i = 0; i < 7; i++) { tt = 1; tt <<= i; for(int j = 0; j < 5*n; j++) { if(j != 0 && j %5 == 0) printf(" "); if(a[j] & tt) printf("#"); else printf(" "); } printf("\n"); } printf("\n"); } return 0; }
hdu 3257 Hello World!(位运算 & 模拟),布布扣,bubuko.com
hdu 3257 Hello World!(位运算 & 模拟)
原文地址:http://blog.csdn.net/u012860063/article/details/38639827