求最大公约数-辗转相除法

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
int main(){
	int num1, num2, temp, n = 0;
    int max=0;
	scanf("%d %d", &num1, &num2);
    
	if (num1 == num2){
		printf("最大公约数为:%d", num1);
	}

	if (num1 > num2){
		temp = num1;
		num1 = num2;
		num2 = temp;
	}
	if (num2%num1 == 0){
		printf("最大公约数是:%d", num1);
	}
	else{
		//辗转相除法;
		max= num2%num1;
		num2 = num1;
		num1 = max;
		/*for (int i = 1; i < num1; i++){
			if (num1%i == 0 && num2%i == 0){
				max = i;
			}	
		}*/
		printf("最大公约数是:%d",num1);
	}
	system("pause");
	return 0;
}

 

猜你喜欢

转载自blog.csdn.net/qq_44370562/article/details/88621827
0条评论
添加一条新回复