http://oj.acm.zstu.edu.cn/JudgeOnline/problem.php?id=4435
懵~ 后来问别人,他让我试着用二进制反推十进制,就晓得了(用(-2)^n)。
这个是学长写的代码 //我写的时候,将它先变成二进制,再给每一个奇数位且值为1的数组的后一位+1,但学长是每得一位就让后面一位变化,也会继续影响接下来每一个数 就不用考虑为2得情况了
1 #include<bits/stdc++.h> 2 using namespace std; 3 //2^i== (-2)^(i+1)+(-2)^i 4 int ans[50], en; 5 int main() 6 { 7 int t; 8 scanf("%d", &t); 9 while(t--){ 10 int n, k; 11 scanf("%d%d", &n, &k); 12 13 en = 0; 14 while(n){ 15 ans[en] = n & 1; //就是得二进制的思路 16 n >>= 1; // n/=2 17 if((en & 1) && (ans[en] & 1) && k == -2) n++; //&1 判断是否为奇数 //如果满足条件,就会让后一位0>1,1>0 18 en++; 19 } 20 if(en){ 21 for(int i = en - 1; i >= 0; --i) printf("%d", ans[i]); 22 puts(""); //换行 23 } 24 else{ 25 puts("0"); 26 } 27 } 28 return 0; 29 }