Your best friend Adam has recently bought a RaspberryPi and some equipment, including a wireless temperaturesensor and a 433MHz receiver to receive the signalsthe sensors sends. Adam plans to use the Raspberry Pias an in-door display for his weather sensor. As he isvery good with electronics, he quickly managed to getthe receiver to receive the signals of the sensor. However,when he looked at the bytes sent by the sensor hecould not make heads or tails of them. After some hourslooking through a lot of websites, he found a documentexplaining that his weather sensor scrambles the datait sends, to prevent it from being used together withproducts from other manufacturers.Luckily, the document also describes how the sensor scrambles its communication. The documentstates that the sensor applies the expression x ^ (x << 1) to every byte sent. The ^operator is bit-wise XOR1, e.g., 10110000 ^ 01100100 = 11010100. The << operatoris a (non-circular) left shift of a byte value2, e.g., 10111001 << 1 = 01110010.In order for Adam’s Raspberry Pi to correctly interpret the bytes sent by the weather sensor, thetransmission needs to be unscrambled. However, Adam is not good at programming (actually heis a pretty bad programmer). So he asked you to help him and as a good friend, you are alwayshappy to oblige. Can you help Adam by implementing the unscrambling algorithm?
Input
The input consists of:• one line with an integer n (1 ≤ n ≤ 105), the number of bytes in the message sent by theweather sensor;• one line with n integers b1, . . . , bn (0 ≤ bi ≤ 255 for all i), the byte values of the message.
Output
Output n byte values (in decimal encoding), the unscrambled message.
Sample Input
5
58 89 205 20 198
Sample Output
22 55 187 12 66
由题意可以知道,是一个数经过了转化变为另一个数,现在给出变化后的数,求出改变前的数,因为数量不大,可以直接做索引表,然后对应的就直接查出来。这里给两种,一个就直接用^,&来建立,但要求比较熟悉这些运算,后面就是慢条斯理的先转化为二进制然后再进行运算。
#include <iostream>
#include <algorithm>
using namespace std;
int a[100000];
int b[100000];
int main()
{
for (int i = 0; i <= 255; i++)
{
int p;
p = (i << 1) & 255;
int q = (i) ^ (p);
a[q] = i;
}
int n;
cin >> n;
for (int i = 0; i < n; i++)
cin >> b[i];
for (int i = 0; i < n; i++)
cout << a[b[i]] << " ";
cout << endl;
return 0;
}
#include<cstdio>
#include<cstring>
using namespace std;
int a[8];
int b[8];
int value[300];
int main()
{
for (int j = 0; j < 256; j++) {
int temp=j;
int p = 0;
while (temp>1)
{
a[p] = temp % 2;
temp = temp / 2;
p++;
}
a[p] = temp;
for (int i = p + 1; i < 8; i++) {
a[i] = 0;
}
for (int i = 1; i < 8; i++) {
b[i] = a[i - 1];
}
b[0] = 0;
for (int i = 0; i <8; i++) {
if (a[i] != b[i])
b[i] = 1;
else
b[i] = 0;
}
int sum = 0;
for (int i = 7; i >= 0; i--) {
sum += sum;
sum += b[i];
}
value[sum] = j;
}
int n,x;
scanf("%d", &n);
while (n--)
{
scanf("%d", &x);
printf("%d", value[x]);
if (n > 0)
printf(" ");
}
printf("\n");
return 0;
}