问题及代码:
/*
Copyright (c) 2014,烟台大学计算机与控制工程学院
All rights reserved.
文件名称:test.cpp
作 者:张学伟
完成日期:2016年11月19日
版 本 号:v1.0
运行过程:
#include <stdio.h>
#include <stdlib.h>
int gcd(int x, int y);
int main()
{
int m,n;
printf("输入两个数字:");
scanf("%d %d", &m, &n);
printf("最大公约数:");
printf("%d\n", gcd(m,n));
return 0;
}
int gcd(int a, int b)
{
int d;
if (b==0)
d=a;
else
d=gcd(b,a%b);
return d;
}
运行结果:
知识点总结:
用递归法求两数的最大公约数。
学习心得:
在编译过程中若其中一个数为零,则最大公约数为另一个数,若不为零则用相除取余的方法来做。