#include <iostream>
using namespace std;
int NumberOf1Between1AndN(int n);
int NumberOf1(const char* strN);
int PowerBase10(unsigned int n);
int NumberOf1Between1AndN(int n) {
if (n <= 0) {
return 0;
}
char strN[50];
sprintf_s(strN, "%d", n);
return NumberOf1(strN);
}
int NumberOf1(const char* strN) {
if (!strN || *strN < '0' || *strN > '9' || *strN == '\0')
return 0;
int first = *strN - '0';
unsigned int length = static_cast<unsigned int>(strlen(strN));
if (length == 1 && first == 0) {
return 0;
}
if (length == 1 && first > 0) {
return 1;
}
// 假设strN是“21345”
// numFirstDigit是数字10000~19999的第一位中的数目
int numFirstDigit = 0;
if (first > 1) {
numFirstDigit = PowerBase10(length - 1);
}
else if (first == 1) {
numFirstDigit = atoi(strN + 1) + 1;
}
// numOtherDIgits是1346~21345除第一位之外的数位中的数目
int numOtherDigits = first*(length - 1) * PowerBase10(length - 2);
// numRecursive是1~1345中的数目
int numRecursive = NumberOf1(strN + 1);
return numFirstDigit + numOtherDigits + numRecursive;
}
int PowerBase10(unsigned int n) { // 10^n
int result = 1;
for (unsigned int i = 0; i < n; i++) {
result *= 10;
}
return result;
}
int main() {
int n = NumberOf1Between1AndN(12);
cout << "ans=" << n <<endl;
system("pause");
}
1~n整数中1出现的次数【不懂】
最新推荐文章于 2020-10-10 21:46:01 发布