题解 | #扭蛋机# -- 根据结果推过程
扭蛋机
https://www.nowcoder.com/practice/9d26441a396242a9a0f7d2106fc130c7
#include <algorithm> #include <iostream> #include <iterator> #include <string> using namespace std; int main() { int N = 0; cin>>N; string s; while(N>0) { if(N%2==0) { s+='3'; N=(N-2)/2; } else { s+='2'; N=(N-1)/2; } } reverse(s.begin(),s.end()); cout<<s; return 0; } // 64 位输出请用 printf("%lld")
奇数2*k+1和偶数2*k+2包含了所有的整数.其实输入一个数,想要推出前一个是什么:就是看当前的数为奇数还是偶数
1.奇数,则(奇数-1)/2
2.偶数,则(偶数-2)/2
此时推出前一个,循环表示当前数是否为0
结束循环时,结果为过程的逆序,reverse一下就可以了