您的位置:首页 > 编程语言 > C语言/C++

Sicily 4427. Greatest Common Divisors

2016-01-17 11:09 411 查看
Time Limit: 5 secs, Memory Limit: 256 MB

Description

A common divisor for two positive numbers is a number which both numbers are divisible by. It’s easy to calculate the greatest common divisor between tow numbers. But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low<=d<=high. It is possible that there is no common divisor in the given range.

Input

The first line contains an integer T (1<=T<=10)- indicating the number of test cases.

For each case, there are four integers a, b, low, high (1<=a,b<=1000,1<=low<=high<=1000) in one line.

Output

For each case, print the greatest common divisor between a and b in given range, if there is no common divisor in given range, you should print “No answer”(without quotes).

Sample Input

3

9 27 1 5

9 27 10 11

9 27 9 11

Sample Output

3

No answer

9

Problem Source

2011年新手赛Warm Up by AcFast

^_^ JUST DO IT!

#include <iostream>

using namespace std;

pair<bool, int> gcd(int a, int b, int low, int high)
{
for (int i = high; i >= low; i--)
if (a % i == 0 && b % i == 0)
return make_pair(true, i);
return make_pair(false, 0);
}

int main()
{
int T, a, b, low, high;
cin >> T;
while (T--)
{
cin >> a >> b >> low >> high;
if (gcd(a, b, low, high).first)
cout << gcd(a, b, low, high).second << endl;
else
cout << "No answer" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Sicily C++ pair numbers it