算法

递归-2 最大公约数

2017-03-16  本文已影响0人  路灯下的黑猫H

//

//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

上一篇 下一篇

猜你喜欢

热点阅读