小易最近在数学课上学习到了集合的概念,集合有三个特征: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
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();
}
}
#!/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)
题意就是给分数判重,显然我们不能直接算,因为浮点数是不精确的,乘以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;
}
#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写~~~,感觉自己棒棒哒~~
#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)
#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;
}
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());
}
}
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)
})
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())) #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();
}
#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;
}