美文网首页算法
递归-2 最大公约数

递归-2 最大公约数

作者: 路灯下的黑猫H | 来源:发表于2017-03-16 15:26 被阅读0次

//

//ViewController.m

//CocoTest_1

//

//Created by S u p e r m a n on 2017/3/14.

//Copyright © 2017年张浩. All rights reserved.

//

#import"ViewController.h"

- (void)viewDidLoad {

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

printf("最大公约数%d",greatestCommonFactor(98,63));

}

intgreatestCommonFactor(intn,intm) {

//n始终要大于m

if(n

inttemp = n;

n = m;

m = temp;

}

if(n%m ==0) {//递归结束

returnm;

}else{

inttemp_n = m;

inttemp_m = n%m;

returngreatestCommonFactor(temp_n,temp_m);

}

}

@end

相关文章

网友评论

    本文标题:递归-2 最大公约数

    本文链接:https://www.haomeiwen.com/subject/rfrjnttx.html