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!
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; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 【CF 应用开发大赛】IT Share(IT分享网)
- 关于指针的一些事情
- IT职场英语,必须学会
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- it人员要看饮食保健养生谚语
- Lua中ipair和pair的区别
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法