<OJ_Sicily>Greatest Common Divisors
来源:互联网 发布:java注释的作用 编辑:程序博客网 时间:2024/05/17 06:06
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.
题目解释:求解两个数a和b在范围low和high之间的最大公约数。实质就是求a和b的公约数,然后符合该公约数在范围low和high之间是最大的
解题思路:使用一个for循环,从a和b中取较小的数,假如较小数是b,从b开始逐渐减一求得符合要求的值
#include <iostream>using namespace std;int main(int argc, const char * argv[]) { // insert code here... int T, a, b, low, high; cin >> T; while (T > 0) { cin >> a >> b >> low >> high; int tmp = 0; if (a < b) { tmp = a; a = b; b = tmp; } bool has_answer = false; int result = 0; for (int i = b; i >= 1 ; i--) { // 从小的数开始,逐个找到符合要求的值 if ((b % i) == 0 && (a % i) == 0 && i >= low && i <= high) { has_answer = true; result = i; break; } } if (has_answer) cout << result<< endl; else cout << "No answer" << endl; T--; } return 0;}
- <OJ_Sicily>Greatest Common Divisors
- 4427. Greatest Common Divisors
- 4427. Greatest Common Divisors
- <Sicily>Greatest Common Divisors
- Sicily 4427. Greatest Common Divisors
- Sicily 4427. Greatest Common Divisors
- Greatest common divisors & Fibonacci Numbers--Algorithms
- Greatest Greatest Common Divisor
- <OJ_Sicily>Longest Common Subsequence
- Common Divisors
- Greatest Greatest Common Divisor HD5207
- Greatest common divisor
- The Greatest Common Divisor
- GCD (Greatest Common Divisor)
- Greatest Common Increasing Subsequence
- Solving Greatest Common Divisor
- the greatest common divisor
- 最大公约数Greatest Common Divisor
- C++学习笔记 指针与引用的区别
- C#基础
- Sort---排序
- 安卓应用双启动工具 双开助手v2.0.10
- Java线程池使用说明
- <OJ_Sicily>Greatest Common Divisors
- 网络视频刷单调查:4分钟免费刷2.2万300元能买4000万点击
- Range——克隆内容cloneContents()
- Activity七大生命周期详解
- leetcode 234 Palindrome Linked List C++
- LeetCode:Longest Consecutive Sequence
- 星期几的不同脚本写法(推荐)和JS年月日格式
- G-Dezender批量解密
- 在Windows上编译Wireshark源代码