C - Jumbled Communication Kattis - communication (数学&&打表)

本文介绍了解决一个特定的天气传感器乱序通讯问题的方法,该传感器使用复杂的位运算来扰乱其发送的数据,防止与其他制造商的产品混用。通过分析传感器的通讯协议,我们发现它应用了x^(x<<1)的表达式来扰乱每个字节。为了解码这些数据,我们提供了一个C++实现的解码算法,通过预先计算和查找表来快速逆向转换乱序的字节。

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

C - Jumbled Communication Kattis - communication (数学&&打表)

Your best friend Adam has recently bought a Raspberry Pi and some equipment, including a wireless temperature sensor and a 433MHz receiver to receive the signals the sensors sends. Adam plans to use the Raspberry Pi as an in-door display for his weather sensor. As he is very good with electronics, he quickly managed to get the receiver to receive the signals of the sensor. However, when he looked at the bytes sent by the sensor he could not make heads or tails of them. After some hours looking through a lot of websites, he found a document explaining that his weather sensor scrambles the data it sends, to prevent it from being used together with products from other manufacturers.
Luckily, the document also describes how the sensor scrambles its communication. The document states 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 << operator is 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, the transmission needs to be unscrambled. However, Adam is not good at programming (actually he is a pretty bad programmer). So he asked you to help him and as a good friend, you are always happy 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 the weather 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 1
5
58 89 205 20 198
Sample Output 1
22 55 187 12 66

打表;

#include<bits/stdc++.h>
using namespace std;

long long a[305];

void dabiao()
{
    for(int i = 0; i <= 255; i ++)
    {
        a[i^(i<<1)&255] = i;
    }
}

int main()
{
    dabiao();
    int t;
    cin >> t;
    while(t--)
    {
        int x;
        cin >> x;
        cout << a[x];
        if(t==0) cout << endl;
        else cout << ' ';
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值