PAT-Advanced Math
Abstract:数论相关,虽然有些可以不用数学方法解
1049 Counting Ones (30分)
解法一
#include<iostream>
#include<algorithm>
int countDigitOne(int n)
{
int countr = 0;
for (long long i = 1; i <= n; i *= 10) {
long long divider = i * 10;
countr += (n / divider) * i + std::min(std::max(n % divider - i + 1, 0LL), i);
}
return countr;
}
int main() {
int num, sum = 0;
std::cin >> num;
std::cout << countDigitOne(num);
}