第四屆 藍橋杯C/C++ 高職 5公約數公倍數

/*求兩個整數的最大公約數和最小公倍數的功能。*/
#include<stdio.h>
#include<iostream.h>
void swap(int *a,int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}

void myfunc(int a, int b)
{
    int m,n,r;
    if(a<b) swap(&a,&b);
    m=a;n=b;r=a%b;
    while(r!=0)
    {
        a=b;b=r;
        r=a%b;
    }
    cout<<a/b<<endl;
    printf("%d\n",b); // 最大公約數
    printf("%d\n", m*a/b); // 最小公倍數
    
}

int main()
{
    int a,b;
    cin>>a;
    cin>>b;
    myfunc(a,b);
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章