思路
先转换为二进制数,再用异或性质转换为格雷码~~~
(别问,问就是数电学了这个ha)
code
#include <iostream>
using namespace std;
int n;
unsigned long long int k;
const int N = 70;
int a[N], res[N];
int op(int x, int y)
{
if (x == y)
return 0;
else
return 1;
}
int main()
{
cin >> n >> k;
int i = 1;
while (k)
{
int rem = k % 2;
a[n + 1 - i] = rem;
i++;
k /= 2;
}
for (int i = 1; i <= n; i++)
{
if (i == 1)
{
res[i] = a[i];
}
else
{
res[i] = op(a[i - 1], a[i]);
}
}
for (int i = 1; i <= n; i++)
{
cout << res[i];
}
return 0;
}