You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
702 B
35 lines
702 B
2 years ago
|
#include <bits/stdc++.h>
|
||
|
|
||
|
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;
|
||
|
}
|