<Sicily>Greatest Common Divisors
一、题目描述
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.
二、输入
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.
三、输出
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
四、解题思路
题意:从low到high之间找出既能被a整除,又能被b整除的数,如果没有输出No answer
思路:这道题没什么好讲,就是遍历从high到low开始找一个既能被a整除又能被b整除就行了。
五、代码
#include<iostream>
using namespace std;
int main()
{
int times;
cin >> times;
while(times--)
{
int a, b, low, high;
cin >> a >> b >> low >> high;
bool result;
int divisor;
for(divisor = high; divisor >= low; divisor--)
{
if(a % divisor == 0 && b % divisor == 0) {result = true; break;}
result = false;
}
if(result) cout << divisor << endl;
else cout << "No answer" << endl;
}
return 0;
}
<Sicily>Greatest Common Divisors的更多相关文章
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU1423:Greatest Common Increasing Subsequence(LICS)
Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...
- greatest common divisor
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...
- Greatest Common Increasing Subsequence hdu1423
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- silverlight wpf DataTemplate Command binding
<Grid x:Name="LayoutRoot" Background="White"> <CommonControl:NoapDataGr ...
- m_Orchestrate learning system---十四、数据表中字段命名规则
m_Orchestrate learning system---十四.数据表中字段命名规则 一.总结 一句话总结:a.保证唯一 b.见名知意 1.注意php中的数组类函数和字符串类函数的前缀? 数组类 ...
- spm 捕获
目录 自动捕获 从library cache 中加载 从SQL 调优级加载 1.自动捕获 当optimizer_capture_sql_plan_baselines参数设置为TRUE,查询优化器自动存 ...
- AndroidStudio EventBus报错解决方法its super classes have no public methods with the @Subscribe
首先说明,以前我用eventBus的jar包写得项目demo,前几天就写了一个EventBus的实例,这次我没用jar包,直接用gradle引用的,可是demo写完了,报错: its super cl ...
- Glide错误java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
解决办法 在使用Glide的那段代码加是否在主线程判断 if(Util.isOnMainThread()) { Glide.with(ClassifyItemDetailActivity.this). ...
- JavaScript学习——JS对象和全局函数
1. Array对象 数组的特点:长度可变!数组的长度=最大角标+1 2.Boolean对象 如果value 不写,那么默认创建的结果为false 3.Date对象 getTime()返回1970年1 ...
- Java数据库连接——PreparedStatement的使用
首先了解Statement和PreparedStatement的区别: 由此可见,一般使用PreparedStatement. 操作数据库SU(Course表),其中Course属性有Cno,Cnam ...
- 如何把非服务程序(一般的应用程序)注册为Windows服务
非服务程序:不是标准的服务形式的程序吧,只是普通的应用程序. 1.要实现这个功能要用到微软提供的两个小工具“instsrv.exe”和“srvany.exe”,工具可以从微软下载安装工具包得到:htt ...
- angular.js学习-ng-grid
ng-grid是基于AngularJS和JQuery的富表格控件,由AngularUI Team领衔开发,到目前为止已有2354次Commit,1076个Fork. AngualrUI:http:/ ...
- CDR都特价了,你还用破解版?
目前,CDR X6特价活动,从18的年的元旦一直持续到现在,真可谓是痛快淋漓,大喜若狂,但是,似乎好像是....这么强有力的活动马上就要告一段落了,因为限量1000 套的抢购要卖!完!了!难不难过,痛 ...