import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/**
* 小易最近在数学课上学习到了集合的概念,集合有三个特征:1.确定性 2.互异性 3.无序性. 小易的老师给了小易这样一个集合: S = { p/q | w
* ≤ p ≤ x, y ≤ q ≤ z }
* 需要根据给定的w,x,y,z,求出集合中一共有多少个元素。小易才学习了集合还解决不了这个复杂的问题,需要你来帮助他。
*
* @author pomay
*
*/
public class Wangyi_gather
{
public static int gather(int w, int x, int y, int z)
{
// 方法一:用List先求p/q,再去重
// List<Double> gather = new ArrayList<Double>();
// for (int i = w; i <= x; i++)
// {
// for (int j = y; j <= z; j++)
// {
// 去重
// if (!gather.contains((i * 1.0) / j))
// {
// gather.add((i * 1.0) / j);
// }
// }
// }
// 方法二:元素无序,不重复,直接用set
Set<Double> gather = new HashSet<Double>();
for (int i = w; i <= x; i++)
{
for (int j = y; j <= z; j++)
{
gather.add((i * 1.0) / j);
}
}
return gather.size();
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
// 输入包括一行:一共4个整数分别是:以空格分隔
// w(1 ≤ w ≤ x)
int w = sc.nextInt();
// x(1 ≤ x ≤ 100)
int x = sc.nextInt();
// y(1 ≤ y ≤ z)
int y = sc.nextInt();
// z(1 ≤ z ≤ 100)
int z = sc.nextInt();
System.out.println(gather(w, x, y, z));
}
}
网易之小易最近在数学课上学习到了集合的概念,集合有三个特征:1.确定性 2.互异性 3.无序性.需要根据给定的w,x,y,z,求出集合中一共有多少个元素。
最新推荐文章于 2022-06-07 11:52:54 发布