GCD (Greatest Common Divisor)
2013-03-20 19:49
453 查看
欧几里德算法(GCD),又称辗转相除法。
练习题:1、Uva 10193 All
you need is love //P.S.The Beatles的歌名《All You
Need Is Love》。
//扯了一大堆,原来就是求两个数是否互质。。
//代码一: int gcd(int a, int b){ return b ? gcd(b, a%b):a; }
//代码二: int gcd(int a, int b){ while((a %= b) && (b %= a)) ; return a + b; }
练习题:1、Uva 10193 All
you need is love //P.S.The Beatles的歌名《All You
Need Is Love》。
//扯了一大堆,原来就是求两个数是否互质。。
#include <iostream> #include <cstring> using namespace std; int gcd(int a,int b){ return b? gcd(b, a%b):a; } int main() { int a, b; int T; char s[32]; int i, n; cin>>T;n=1; while(n<=T){ cout<<"Pair #"<<n++<<": "; cin>>s; a = 0; for(i=0;s[i];++i){ a =a*2+s[i]-'0'; } cin>>s; b = 0; for(i=0;s[i];++i){ b =b*2+s[i]-'0'; } if(gcd(a,b)==1) cout<<"Love is not all you need!\n"; else cout<<"All you need is love!\n"; } return 0; }
相关文章推荐
- Greatest Common Divisor (GCD) - Euclidean algorithm
- GCD(Greatest common divisor)求最大公约数算法
- GCD (greatest common divisor)【求最大公约数】
- 最大公约数GCD(Greatest Common Divisor)算法小结
- Calculate the Greatest Commom Divisor (GCD) and Lowest Common Multiple (LCP) of Two Integers
- The greatest common divisor gcd(最大公约数)
- [计算最大公因子]Finding the greatest common divisor (gcd) of 2 numbers
- 算法分析---求最大公约数 gcd(int x,int y) (greatest common divisor )
- Eculid算法递归地求解GCD(Greatest Common Divisor,最大公约数)
- Greatest Common Divisor(GCD) of Two Numbers
- Euclid Greatest Common Divisor-GCD - 欧几里得 最大公约数
- Greatest Common Divisor(GCD) of Two Numbers
- Greatest Common Divisor(GCD) of Two Numbers
- More than one way to get gcd(greatest common divisor,最大公约数)
- Algorithms - 最大公约数(greatest common divisor)-欧几里得(Euclid) 算法 及 代码
- 最大公约数(Greatest Common Divisor)
- hdu 5207 Greatest Greatest Common Divisor【筛法】【思维】
- SGU 499:Greatest Greatest Common Divisor
- HDU 5207 Greatest Greatest Common Divisor
- ssu 499 Greatest Greatest Common Divisor