描述
输出7有关数字的个数,包括7的倍数,还有包含7的数字(如17,27,37…70,71,72,73…)的个数(一组测试用例里可能有多组数据,请注意处理)
输入描述:
一个正整数N。(N不大于30000)
输出描述:
不大于N的与7有关的数字个数,例如输入20,与7有关的数字包括7,14,17.
示例1
输入:
20
10
输出:
3
1
代码
public class Huawei挑7 {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String input = "";
while ((input = bufferedReader.readLine()) != null) {
int count = 0;
int num = Integer.parseInt(input);
for (int i = 7; i <= num; i++) {
if (i % 7 == 0 || contain7(i)) {
count++;
}
}
System.out.println(count);
}
}
/**
* 如果包含7,对10不断求模,最终都会等于7
* @param n
* @return
*/
public static boolean contain7(int n) {
while (n > 0) {
if (n % 10 == 7) {
return true;
} else {
n /= 10;
}
}
return false;
}
}