标签:min nbsp dig bool out include void common dell
1 #include <iostream> 2 using namespace std; 3 4 /* 5 *判断素数 6 */ 7 8 bool isPrime(unsigned long digit) 9 { 10 if(digit <= 3) 11 return digit > 1; 12 else if(digit % 2 == 0 || digit % 3 == 0) 13 return false; 14 else 15 { 16 for (unsigned short i = 5;i * i <= digit;i += 6) 17 { 18 if(digit % i == 0 || digit % (digit + 2) == 0) 19 return false; 20 } 21 return true; 22 } 23 } 24 25 /* 26 *处理特殊情况 27 */ 28 int span(int digit_one,int digit_two) 29 { 30 if (isPrime(digit_one) && isPrime(digit_two)) 31 return digit_one * digit_two; 32 else if(digit_one % digit_two == 0 || digit_two % digit_one == 0) 33 return digit_one >= digit_two ? digit_one : digit_two; 34 else 35 return 0; 36 } 37 38 39 40 41 /* 42 *辗转相除法 43 */ 44 45 /* 46 int Common(int digit_one,int digit_two) 47 { 48 int residue = 1; 49 while(residue) 50 { 51 residue = digit_one % digit_two; 52 digit_one = digit_two; 53 digit_two = residue; 54 } 55 return digit_one; 56 } 57 */ 58 59 60 /* 61 *更相减损法 62 * 63 */ 64 65 /* 66 处理之前的函数,将数字化简,如果都是二的倍数 67 */ 68 void Dell_Before(int& digit_one,int& digit_two) 69 { 70 while(digit_one % 2 == 0 && digit_two % 2 == 0) 71 { 72 digit_one /= 2; 73 digit_two /= 2; 74 } 75 } 76 /* 77 求两个数的最大值,如果想化简也可以用 三元表达式: 78 digit_one > digit_two ? digit_one : digit_two 79 */ 80 int Get_Max(int digit_one,int digit_two) 81 { 82 if(digit_one >= digit_two) 83 return digit_one; 84 else 85 return digit_two; 86 } 87 //如同上,求最小值 88 int Get_Min(int digit_one,int digit_two) 89 { 90 if(digit_one <= digit_two) 91 return digit_one; 92 else 93 return digit_two; 94 } 95 96 int Common(int digit_one,int digit_two) 97 { 98 int result = 0; 99 Dell_Before(digit_one,digit_two); 100 while(true) 101 { 102 int digit_min = Get_Min(digit_one,digit_two); 103 int digit_max = Get_Max(digit_one,digit_two); 104 result = digit_max - digit_min; 105 if(result == digit_min) 106 { 107 return result; 108 } 109 else 110 { 111 digit_one = digit_two; 112 digit_two = result; 113 } 114 } 115 } 116 117 118 119 int main() 120 { 121 cout<<Common(30,40)<<endl; 122 return 0; 123 }
标签:min nbsp dig bool out include void common dell
原文地址:https://www.cnblogs.com/royaladd/p/9557538.html