首页 > 试题广场 >

集合

[编程题]集合
  • 热度指数:2854 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
小易最近在数学课上学习到了集合的概念,集合有三个特征: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

输入

1 10 1 1

输出

10

import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

/**
 * [编程题] 集合
 * @author Administrator
 * 用HashSet求解
 */
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		double w=sc.nextInt(),x=sc.nextInt(),y=sc.nextInt(),z=sc.nextInt();
		Set<Double> set=new HashSet<Double>();
		for(double i=w;i<=x;i++){
			for(double j=y;j<=z;j++){
				if(!set.contains(i/j))
					set.add(i/j);
			}
		}
		System.out.println(set.size());
		sc.close();
	}
}


发表于 2017-09-14 10:30:08 回复(0)
#!/usr/bin/env python
#-*- coding:utf8 -*-
def getNum(w, x, y, z):
    l = []
    if y == z:
        return x-w+1
    for i in range(w, x+1):
        for j in range(y, z+1):
            n = float(i)/j
            l.append(n)
    return len(list(set(l)))

if __name__ == '__main__':
    w, x, y, z = map(int , raw_input().split())
    print getNum(w, x, y, z)


发表于 2017-05-08 20:17:15 回复(0)

思路分析

题意就是给分数判重,显然我们不能直接算,因为浮点数是不精确的,乘以1.0000就可以了,然后丢进set里就好了。

#include<iostream>
#include<set>
using namespace std;
int getSetNum(int w, int x, int y, int z)
{
    int count = 0;
    set<double> s;
    for (int i = w; i <= x; i++)
        for (int j = y; j <= z; j++)
        {
            s.insert(i*1.0000 / j);
        }
    count = s.size();
    return count;
}
int main()
{
    int w, x, y, z;
    cin >> w >> x >> y >> z;
    int ans;
    ans = getSetNum(w, x, y, z);
    cout << ans << endl;
    return 0;
}

发表于 2017-04-11 09:05:59 回复(0)
#include <stdio.h>
#define N 10000
int find(float arr[],int n,float x);
int main(){
	int w,x,y,z,i,j,len=0;
	float temp,arr[N];
	scanf("%d %d %d %d",&w,&x,&y,&z);
	for(i=w;i<=x;i++){
		for(j=y;j<=z;j++){
			temp=(float)i/j;
			if(!find(arr,len,temp)){
				arr[len++]=temp;
			}	
		}
	}
	printf("%d",len);
	return 0;
}
/*判断元素是否在数组中,存在则返回1,否则为0*/
int find(float arr[],int n,float x){
	int i;
	for(i=0;i<n;i++){
		if(arr[i]==x) return 1;
	}
	return 0;
}

纯C写~~~,感觉自己棒棒哒~~
发表于 2017-03-29 23:39:09 回复(1)
# -*- coding: UTF-8 -*- 

import sys
value=map(int,sys.stdin.readline().strip().split())
#使用 set 满足集合的无序性,确定性,唯一性
s=set()
for p in range(value[0],value[1]+1):
    for q in range(value[2],value[3]+1):
        s.add(p*1.0/q)
print len(s)

发表于 2017-03-28 20:34:58 回复(0)
#coding=utf-8
import sys
value=map(int,sys.stdin.readline().strip().split())
dic={}
for p in range(value[0],value[1]+1):
    for q in range(value[2],value[3]+1):
        dic[p*1.0/q]=0
print len(dic)

发表于 2017-03-27 14:55:31 回复(0)
#include <iostream>
#include <unordered_set>
using namespace std;

int main()
{
	unordered_set<double> s;
	int w, x, y, z;
	double temp;
	cin >> w >> x >> y >> z;
	for (double p = w; p <= x; p++)
	{
		for (double q = y; q <= z; q++)
		{
			temp = p / q;
			if (s.find(temp) == s.end())		s.insert(temp);
		}
	}
	cout << s.size() << endl;
}
编辑于 2017-03-26 22:52:28 回复(0)
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {
public static void main(String args[]){
	Scanner myscanner=new Scanner(System.in);
	int w=myscanner.nextInt();
	int x=myscanner.nextInt();
	int y=myscanner.nextInt();
	int z=myscanner.nextInt();
	myscanner.close();
	float p=0;
	Set mySet=new HashSet();
	for(float i=w;i<=x;i++){
		for(float j=y;j<=z;j++){
			p=i/j;
			mySet.add(Float.toString(p));
		}
	}
	System.out.println(mySet.size());
	
}

}


发表于 2017-03-26 17:31:43 回复(1)
var readline = require('readline')
var ri = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})

ri.on('line', function (line) {
  var nums = line.match(/(\d+)/g)
  var l = []
  // console.log(nums)
  for (var i = parseFloat(nums[0]); i <= nums[1]; i ++) {
    for (var j = parseFloat(nums[2]); j <= nums[3]; j ++) {
      // console.log(i, j)
      var t = i/j
      if (l.indexOf(t) === -1) {
        l.push(t)
      }
    }
  }
  console.log(l.length)
})

发表于 2017-03-26 15:08:12 回复(2)
import fractions
w, x, y, z = map(int, raw_input().split())
res = set()
for i in range(w, x+1):
	for j in range(y, z+1):
		res.add(fractions.Fraction(i, j))
print len(res)
介绍fractions。这个比起依靠浮点好多了。这个时候python居然可以稍稍作弊一下。也算是对速度补偿?

发表于 2017-04-14 02:02:12 回复(0)
w, x, y, z = [int(i) for i in input().split()]
tempDict = {}
for p in range(w, x + 1):
    for q in range(y, z + 1):
        tempKey = str(p / q)
    if tempKey in tempDict:
        tempDict[tempKey] += 1
    else:
        tempDict[tempKey] = 1
print(len(tempDict.keys()))

发表于 2020-07-27 15:24:07 回复(0)
#include<iostream>
#include<vector>
#include<string>
#include<set>

using namespace std;





int main()
{   
    set<float> unique;
    float w, x, y, z;
    cin >> w >> x >> y >> z;
    for(float i=w;i<=x;i++)
        for (float j = y; j <= z; j++)
        {
            unique.insert(i / j);

        }
    cout << unique.size() << endl;


}
发表于 2019-07-22 09:37:35 回复(0)
import sys
w,x,y,z=list(map(int,sys.stdin.readline().strip().split()))
if y==z:
    print(x-w+1)
elif x==w:
    print(z-y+1)
else:
    result=[]
    for i in range(w,x+1):
        for j in range(y,z+1):
            result.append(float(i)/j)
    print(len(list(set(result))))
发表于 2018-05-17 15:19:29 回复(0)
#include <iostream>
#include <set>
int main()
{
    int w,x,y,z;
    std::cin>>w>>x>>y>>z;
    std::set<double> sd;
    for(int i=w; i<=x; i++)
        for(int j=y; j<=z; j++)
            sd.insert((double)i/j);
    std::cout<<sd.size();
}
发表于 2018-02-05 23:26:22 回复(0)
#include <iostream>
#include <algorithm>
using namespace std;

int main(int argc, const char * argv[]) {
    int w, x, y, z;
    cin>>w>>x>>y>>z;
    int arr[10000], len = 0;
    for (int i = w; i <= x; i++) {
        for (int j = y; j <= z; j++) {
            int ele = ((float)i / (float)j) * 10000;    //乘数要足够大,否则由于位数不够,导致将不同的两个浮点数算为同一个数。
            int *end = arr + len;
            if (find(arr, arr + len, ele) == end) {
                arr[len] = ele;
                len++;
            }
        }
    }
    cout<<len;
    return 0;
}
发表于 2017-10-19 18:56:52 回复(0)
# include <iostream>
# include <algorithm>
# include <set>

using namespace std;

class MyUnion{
public:
int x;
int y;
MyUnion(int x, int y){
this->x = x;
this->y = y;
}
bool operator <(MyUnion other) const
{
return x * other.y > other.x * y;
}

};


int main(){


int w, x, y, z;
while (cin >> w >> x >> y >> z){
set<MyUnion> smu;
for (int i = w; i <= x; i++){
for (int j = y; j <= z; j++){
MyUnion mu(i, j);
smu.insert(mu);
}
}
cout << smu.size() << endl;
}


return 0;
}

发表于 2017-08-07 13:21:56 回复(1)
dnq头像 dnq
w, x, y, z = map(int, raw_input().split())
anw = [float(i) / j for i in xrange(w, x + 1) for j in xrange(y, z + 1)]
print len(set(anw))

发表于 2017-08-06 23:15:40 回复(0)
package tom;

import java.util.*;
public class Main {
    public static void main(String[] args){
    Scanner sc=new Scanner(System.in);
    while(sc.hasNext()){
    int w=sc.nextInt();
    int x=sc.nextInt();
    int y=sc.nextInt();
    int z=sc.nextInt();
   
    int[] dp1=new int[x-w+1];
    int[] dp2=new int[z-y+1];
    int sum=0;
    for(int i=0;i<dp1.length;i++){
    dp1[i]=w+i;
    }
    for(int i=0;i<dp2.length;i++){
    dp2[i]=y+i;
    }
   
    Set<Double> set=new LinkedHashSet<Double>();
    int k=0;
    for(int i=0;i<dp1.length;i++){
    for(int j=0;j<dp2.length;j++){
    set.add((double)dp1[i]/dp2[j]);
    }
    }
    System.out.println(set.size());
    }
    }
}

发表于 2017-07-29 15:47:01 回复(0)
#include <iostream>
#include <set>
 
using namespace std;
int main(){
    intw,x,y,z;
    cin>>w>>x>>y>>z;
    set<double> st;
    for(doublep=w;p<=x;p++){
        for(doubleq=y;q<=z;q++){
            st.insert(p/q);
        }
    }
    cout<<st.size()<<endl;
    return0;
}

编辑于 2017-07-25 10:33:32 回复(0)
我想问一下,为什么 p 、q 也表示整数呢?(也可以是小数啊?)题目中并没有说明
发表于 2017-07-21 09:11:59 回复(0)