Hdu 1287 【关于异或要注意的问题】.cpp

题意:

  给出一串已加密的报文,报文的原内容是由这串密码和某一个大写字母异或而来的。

  已知这串报文仅由大写字母组成,请输出原报文

思路:

  水题,枚举A~Z

  如果加密报文的每一个大写字母和当前枚举的字母异或后在合法范围内,即都是大写字母,则该字母就是keyword..

 

Tips:

  ---***这里是重点***--

  ①. 规律,a^b = c --> a^c = b & b^c = a

  ②. 两个整型异或的结果不是整型,需要强制转换..

Code:

 1 #include <stdio.h>
 2 #include <cstring>
 3 
 4 int main()
 5 {
 6     int n;
 7     int arr[1024];
 8     bool flag;
 9     int ans;
10     while (~scanf("%d", &n)) {
11         ans = 0;
12         flag = true;
13         for (int i = 0; i < n; ++i)
14             scanf("%d", &arr[i]);
15         for (int i = 'A'; i <= 'Z'; ++i) {
16             flag = true;
17             for (int j = 0; j < n; ++j) {
18                 if ((int)(i^arr[j]) < 65 || (int)(i^arr[j]) > 90) {
19                     flag = false;
20                     break;
21                 }
22             }
23             if (flag) {
24                 ans = i;
25                 break;
26             }
27         }
28 
29         for (int i = 0; i < n; ++i)
30             putchar(ans^arr[i]);
31         puts("");
32     }
33     return 0;
34 }
View Code

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1287

原文地址:https://www.cnblogs.com/Griselda/p/3122640.html