Files
python/TangDou/KaoShi/数位递增数.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

28 lines
836 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
/*
一个正整数如果任何一个数位不大于右边相邻的数位,则称为一个数位递增的数,
例如1135是一个数位递增的数,而1024不是一个数位递增的数。
给定正整数 n,请问在整数 11 至 n 中有多少个数位递增的数?
*/
using namespace std;
int n, cnt;
bool iSDiZeng(int t) {
//哨兵
int x = 10; //这里定义x为10,是为了便于将个位数的大小与t作比较,
//个位数永远小于10
while (t) {
if (t % 10 > x) return false;
x = t % 10; // x一直在更新
t /= 10; // n分解出一个数便舍去一个数
}
return true;
}
int main() {
cin >> n;
for (int i = 11; i <= n; i++)
if (iSDiZeng(i)) cnt++;
cout << cnt << endl;
return 0;
}