1385. 幸运数字8
8
是小九的幸运数字,小九想知道在1~n
的数中有多少个数字含有8
。
样例
Example1
Input: n = 20
Output: 2
Explanation:
Only 8,18 contains 8.
Example2
Input: n = 100
Output: 19
Explanation:
8,18,28,38,48,58,68,78,80,81,82,83,84,85,86,87,88,89,98 contains 8.
注意事项
1 <= n <= 1000000
输入测试数据 (每行一个参数)
public class Solution {
/**
* @param n: count lucky numbers from 1 ~ n
* @return: the numbers of lucky number
*/
public int luckyNumber(int n) {
// Write your code here
int count = 0;
for(int i = 0; i <= n; i++){
int k = i;
while(k!=0){
if(k%10 == 8){
count++;
break;
}else{
k /= 10;
}
}
}
return count;
}
}