看看效果用vi编写,G++编译的
刚开始的时候把getmax()写到main()后边去了,新手,看来光看书是不行的,还得多练手
把代码贴出来:求最大公约数的算法可以这样理解:
对于:36 4
36%4 = 0
就是4
对于:36 42
42%36=6
36%6=0
就是6
对于:36 17
36%17=2
17%2=1
2%1 = 0
就是1
这个算法也叫辗转相除,加油。

刚开始的时候把getmax()写到main()后边去了,
把代码贴出来:
- /*
- * function getmax() is used to Calculate
- *the greatest common denominator
- *of two number
- */
- #include <iostream>
- int getmax(int x, int y) {
- int temp;
- if (x < y) {
- temp = x;
- x = y;
- y = temp;
- }
- if (x % y == 0) {
- return y;
- } else {
- while (x % y) {
- temp = x % y;
- x = y;
- y = temp;
- }
- return y;
- }
- }
- int main() {
- int m, n, i;
- std::cout << "Please input two integer numbers: " << std::endl;
- std::cin >> m >> n;
- i = getmax(m, n);
- std::cout << "The greatest common denominator of m and n is:"
- << i << std::endl;
- return 0;
- }
对于:36 4
36%4 = 0
就是4
对于:36 42
42%36=6
36%6=0
就是6
对于:36 17
36%17=2
17%2=1
2%1 = 0
就是1
这个算法也叫辗转相除,加油。
