Description:
Consider the following set:
S = { p/q | w <= p <= x, y <= q <= z }
That means S is the set of all rational numbers whose numerators are between w and x inclusive, and whose denominators are between y and z inclusive.
Given w, x, y, and z return the number of distinct elements in S.
Notes:
1. p, q, w, x, y and z are all integers.
2. x and z will be between 1 and 100, inclusive.
3. w will be between 1 and x, inclusive.
4. y will be between 1 and z, inclusive.
The input consists of separate lines containing w,x,y,z described above
The last line of the input contains four 0s.(0 0 0 0).
For each test case, output the number of distinct elements in S in separate lines.
Sample Input
1 1 1 1
1 10 1 1
1 2 1 2
2 4 2 4
1 100 1 100
0 0 0 0
1
10
3
7
6087
1) Returns:1
Only the value 1/1 is being considered.
2) Returns: 10
Here S contains the values 1,...,10.
3) Returns: 3
Here the values are 1/1, 1/2, 2/1, and 2/2. Since 2/2 = 1/1 the answer is 3.
程序:
#include<iostream>
#include<vector>
using namespace std;
bool Find(double x,vector<double>check)
{
for(int i = 0;i<check.size();i++)
{
if(x == check[i])
{
return true;
}
}
return false;
}
int calu(int w,int x,int y,int z)
{
int Num = 0;
double ans = 0;
vector<double>check;
for(int i = y;i <= z;i++)
{
for(int j = w;j <= x;j++)
{
ans = double(j)/double(i);
if(!Find(ans,check))
{
Num++;
check.push_back(ans);
}
}
}
return Num;
}
void main()
{
vector<int>Answer;
int w(0),x(0),y(0),z(0);
while(1)
{
cin>>w>>x>>y>>z;
if(w<1||x<1||y<1||z<1||w>x||y>z)
{
break;
}
else
{
Answer.push_back(calu(w,x,y,z));
}
}
for(int i = 0;i<Answer.size();i++)
{
cout<<Answer[i]<<endl;
}
}