1087 有多少不同的值
当自然数 n 依次取 1、2、3、……、N 时,算式 ⌊n/2⌋+⌊n/3⌋+⌊n/5⌋ 有多少个不同的值?(注:⌊x⌋ 为取整函数,表示不超过 x 的最大自然数,即 x 的整数部分。)
输入格式:
输入给出一个正整数 N(2≤N≤104)。
输出格式:
在一行中输出题面中算式取到的不同值的个数。
输入样例:
tex
2017
2017
输出样例:
tex
1480
1480
Solution:
java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
HashSet<Integer> res = new HashSet<Integer>();
for (int i = 1; i <= n; i++) {
res.add(i / 2 + i / 3 + i / 5);
}
System.out.println(res.size());
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
HashSet<Integer> res = new HashSet<Integer>();
for (int i = 1; i <= n; i++) {
res.add(i / 2 + i / 3 + i / 5);
}
System.out.println(res.size());
}
}