码迷,mamicode.com
首页 > 其他好文 > 详细

网易2017春招笔试真题编程题集合(7)——集合

时间:2017-06-21 13:42:47      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:set   输入   int   strong   多少   system   描述   void   包含   

小易最近在数学课上学习到了集合的概念,集合有三个特征:1.确定性 2.互异性 3.无序性.
小易的老师给了小易这样一个集合:
S = { p/q | w ≤ p ≤ x, y ≤ q ≤ z }
需要根据给定的w,x,y,z,求出集合中一共有多少个元素。小易才学习了集合还解决不了这个复杂的问题,需要你来帮助他。 

输入描述:
输入包括一行:
一共4个整数分别是w(1 ≤ w ≤ x),x(1 ≤ x ≤ 100),y(1 ≤ y ≤ z),z(1 ≤ z ≤ 100).以空格分隔
输出描述:
输出集合中元素的个数
输入例子:
1 10 1 1
输出例子:
10

代码:
//set接口不包含重复元素,实现此接口的HashSet类是无序的,满足集合的三个特征
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int w=sc.nextInt();
        int x=sc.nextInt();
        int y=sc.nextInt();
        int z=sc.nextInt();
        Set set=new HashSet();
        float res=0;
        for(float i=w;i<=x;i++){//不能用int,因为int/int的结果还是int的
            for(float j=y;j<=z;j++){
                res=i/j;
                set.add(res);
            }
        }
        System.out.println(set.size());
        sc.close();
    }
}

 

网易2017春招笔试真题编程题集合(7)——集合

标签:set   输入   int   strong   多少   system   描述   void   包含   

原文地址:http://www.cnblogs.com/dengyt/p/7058724.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!