#include using namespace std; /** 12345-->f(12345)=1*2^4+2*2^3+3*2^2+4*2^1+5*2^0 f(1234) =1*2^3+2*2^2+3*2^1+4*2^0 2*f(1234) =1*2^4+2*2^3+3*2^2+4*2^1 所以形成递归表示式 */ //利用递归计算出F(x) int f(int x) { if (x == 0) return 0; return f(x / 10) * 2 + x % 10; } //利用循环进行计算 int f2(int x) { int res = 0; int i = 0; while (x) { res += x % 10 * (1 << i); i++; x /= 10; } return res; } int main() { cout << f(5) << endl; cout << f2(12345) << endl; //极大值是1e9-1=99999999 cout << f2(1e9 - 1) << endl; //输出4599 return 0; }