Input
輸入N(1 <= N <= 10^9)
Output
輸出包含1的個數
Input示例
12
Output示例
5
從低位一步步往高位推
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int main()
{
int n;
int t,tn;
LL ans = 0;
int mul = 1;
scanf ("%d",&n);
tn = n;
while (tn)
{
t = tn % 10;
if (t == 0)
ans += n / (mul * 10) * mul; //高位數字
else if (t == 1)
{
ans += n / (mul * 10) * mul;
ans += (n % mul) + 1;
}
else //只與高位有關
ans += (n / (mul * 10) + 1) * mul;
mul *= 10;
tn /= 10;
// printf ("%lld\n",ans);
}
printf ("%lld\n",ans);
return 0;
}