K - Jumbled Communication

Adam购买了一套包含无线温度传感器及接收器的设备,并计划使用Raspberry Pi显示数据。因传感器采用特殊的数据加密方式,需编写程序解密才能正确显示。本篇介绍了解密算法的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值