思路:这道题,我没有按网上的dp模板去套一个代码出来(套不出来),这道题我想了很久。。。才看的差不多别人的代码,现在先不慌写这题啦,先总结一点点,以后再写一遍。
#include<iostream>
#include<cmath>
using namespace std;
typedef long long int ll;
ll dp[12];
void init(){
for(int i = 1; i <= 12; i++)//dp[i]存的是1~i位数中1的个数
dp[i] = dp[i-1] *10 +pow(10,i-1);//前i位数中1的个数乘10 + 第i位数为1的个数
}
int main(){
init();
ll n;
cin >> n;
ll now = 1, hou=0, ans = 0,cun,len = 0;
while(n){
cun = n % 10;//cun表示当前的最后一位数
n /= 10;
len++;//len 表示的是长度
if(cun > 1)//当当前的最后一位数大于1
ans += now + cun *dp[len-1];//now表示百位为1的次数,cun表示最后一位的数字*前len-1个数中1的个数
else if(cun == 1)
ans += hou + 1 + dp[len-1];
hou += now * cun;//如n为 34526 , now = 5 ,hou为26
now *= 10;
}
cout << ans << endl;
return 0;
}
这个是我的51nod算法题的最后一题,我一定会回来填坑的!!!