【题目】
输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数。例如 输入12,从1到12这些整数中包含1 的数字有1,10,11和12,1一共出现了5次。
【code】
#include<iostream>
//#include<vector>
#include<stack>
#include<algorithm>
//#include<stack>
#include<queue>
#include<set>
using namespace std;
typedef long long ll;
ll numOf1Between1AndN(ll num)
{
ll cnt = 0;
for (ll i = 1; i <= num; i*=10)
{
ll div = (i * 10);
cnt += num / div * i + min(max(num % div - i + 1,0LL), i);
}
return cnt;
}
int main()
{
ll num = 1234;//689
cout << numOf1Between1AndN(num) << endl;
return 0;
}
/****
***/