一、题目描述

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的更多相关文章

  1. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  2. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  3. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

  4. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

  5. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

    Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  6. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  7. 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 ...

  8. greatest common divisor

    One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...

  9. Greatest Common Increasing Subsequence hdu1423

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

随机推荐

  1. &quot;浪潮杯&quot;第六届ACM山东省省赛山科场总结

    从空间拷过来的.尽管已经过去一个月了.记忆犹新 也算是又一次拾起这个blog Just begin 看着一群群大牛还有队友男神的省赛总结都出了 我最终也耐不住寂寞 来做个流水账抒抒情好了 第一次省赛 ...

  2. 关于 折半查找 while 条件 &lt; , &lt;=

    int bin_search(int a[],int len,int key) { int low=0; int high=len-1; while(low<=high) //若为low< ...

  3. netty底层是事件驱动的异步库 但是可以await或者sync(本质是future超时机制)同步返回 但是官方 Prefer addListener(GenericFutureListener) to await()

    io.netty.channel 摘自:https://netty.io/4.0/api/io/netty/channel/ChannelFuture.html Interface ChannelFu ...

  4. Win7 如何禁用“切换用户”功能

    1.按win+r,输入gpedit.msc,点击确定: 2.依次点击计算机配置--管理模块--系统--登录,右侧列表中找到“隐藏“快速用户切换”的入口点”: 3.双击隐藏“快速用户切换”的入口点,点击 ...

  5. 在网页html中嵌入特殊字体

    1.字体格式 .EOT,适用于Internet Explorer 4.0+ .TTF或.OTF,适用于Firefox 3.5.Safari.Opera .SVG,适用于Chrome.IPhone 最常 ...

  6. POJ3764 The xor-longest Path(字典树)

    题意 给你一棵树,n个节点,n-1条边每条边i都有一个权值wi.定义任意两点间的权值为:这两点间的路径上的所有边的值的异或.比如a点和b点间有i,j,k三条边,那么ab两点间的权值为:wi^wj^wk ...

  7. jQuery第三课 点击按钮 弹出层div效果

    jQuery 事件方法 事件方法会触发匹配元素的事件,或将函数绑定到所有匹配元素的某个事件. 触发实例: $("button#demo").click() 上面的例子将触发 id= ...

  8. VUEJS开发规范

    VUEJS开发规范 基于组件化开发理解 组件命名规范 结构化规范 注释规范 编码规范 基于组件化开发理解 什么是组件? ``` 组件其实就是页面组成的一部分,好比是电脑中的每一个元件(如硬盘.键盘.鼠 ...

  9. 紫书 习题 8-2 UVa 1610 (暴力出奇迹)

    这道题我真的想的非常的复杂, 拿草稿纸一直在找规律,推公式, 然后总有一些特殊的情况. 然后就WA了N次.无奈之下看了别人的博客, 然后就惊了.直接暴力枚举两个相邻字符串 里面的所有可能就可以了--真 ...

  10. HDU 4069 数独

    好久没做题了,建图搞了好久…… 然后,判是否有多解的时候会把原来的答案覆盖掉…… 这里没注意,弄了一下午…… 代码: #include <iostream> #include <cs ...