题目描述
输出7有关数字的个数,包括7的倍数,还有包含7的数字(如17,27,37...70,71,72,73...)的个数
输入描述
一个正整数N。(N不大于30000)
输出描述
不大于N的与7有关的数字个数,例如输入20,与7有关的数字包括7,14,17.
输入例子
20
输出例子
3
算法实现
import java.util.Scanner;
/**
* Author: 王俊超
* Date: 2015-12-24 20:10
* Declaration: All Rights Reserved !!!
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt"));
while (scanner.hasNext()) {
int n = scanner.nextInt();
System.out.println(countSeven(n));
}
scanner.close();
}
private static int countSeven(int n) {
int result = 0;
for (int i = 7; i <= n; i++) {
if (i % 7 == 0) {
result++;
} else {
int m = i;
while (m != 0) {
if (m % 10 == 7) {
result++;
break;
} else {
m /= 10;
}
}
}
}
return result;
}
}