可以一次計算多個數值的最大公因數 最小公倍數

[ 按此下載 ]
[ English Version is Available ]


程式碼 (僅供學習參考用) :
#include<stdio.h>
#include<stdlib.h>

int lcm(int a,int b);
int gcd(int c,int d,int e);

int main(void)
{
   
    int g=1,get[10],c,re,re2,re3,gcd2,k;
       
    printf(" 《最大公因數、最小公倍數 計算機》 \n");

while(g=g)
{
      
    while(g==g)
    {
      printf("\n請輸入第 %d 個數值 ,輸入 0 開始計算: ",g);
      scanf("%d",&get[g]);
     
      gcd2=get[g-1];
     
      if(get[g]==0)
        break;
     
      g++;
    }
   
    g--;
   
    for(k=1;k<=g;k++) /* 排序,從小的數值排到大的 */
    {
      for(int i=1;i<=g;i++)               
      {
        if(get[k]>get[i])
          c=get[k],get[k]=get[i],get[i]=c;
      }

    }    
   
    re=re2=get[g];
             
    while(g>=2)
    {
     re3=re2;
    
     re=lcm(re,get[g-1]);
    
     re2=lcm(get[g],get[g-1]);  /* 計算 這次 最小公倍數 */
     re2 = gcd(get[g],get[g-1],re2); 
     gcd2 = lcm(re2,re3); /* 計算 這次 和 上次 最小公倍數 的 最大公因數 */
     gcd2 = gcd(re3,re2,gcd2); /* 計算 這次 和 上次 最小公倍數 的 最小公倍數 */
    
     g--;
        
    }

     printf("\n最大公因數 : %d\n\n最小公倍數 : %d\n\n=====================================",re,gcd2);
    
   g=1;
  
   printf("\n\n請輸入第 %d 個數值 ,輸入 0 離開程式: ",g);
      scanf("%d",&get[g]),g++;
     
      if(get[1]==0)
      {
        printf("\n");          
        break;
      }
}

}
   
   

int lcm(int a,int b)
{
    int c=b;
   
    if(a<b)
    c=a,a=b,b=c; /* a b 互換 */
   
    b=a-b;
   
    if(b!=0)
    {   
     while(a%b!=0)
     {
      c=b;
      b=a%b;
      a=c;
     }
    }
    else
    b=c;
   
    return b;
   
}

int gcd(int c,int d,int e)
{

    e = c * d / e;

         
    return e;
   
}


Code ( for reference only ):

#include<stdio.h>
#include<stdlib.h>

int lcm(int a,int b);
int gcd(int c,int d,int e);

int main(void)
{
   
    int g=1,get[10],c,re,re2,re3,gcd2,k;
       
    printf(" < LCM & GCD CALCULATOR > \n");

while(g=g)
{
      
    while(g==g)
    {
      printf("\nEnter #%d Value , Enter 0 to begin calculate : ",g);
      scanf("%d",&get[g]);
     
      gcd2=get[g-1];
     
      if(get[g]==0)
        break;
     
      g++;
    }
   
    g--;
   
    for(k=1;k<=g;k++) /* Sort, from small to big */
    {
      for(int i=1;i<=g;i++)               
      {
        if(get[k]>get[i])
          c=get[k],get[k]=get[i],get[i]=c;
      }

    }    
   
    re=re2=get[g];
             
    while(g>=2)
    {
     re3=re2;
    
     re=lcm(re,get[g-1]);
    
     re2=lcm(get[g],get[g-1]);  /* Calculate the lcm this round */
     re2 = gcd(get[g],get[g-1],re2); 
     gcd2 = lcm(re2,re3); /* Calculate the lcm of the gcd of this round and last round */
     gcd2 = gcd(re3,re2,gcd2); /* Calculate the gcd of the gcd of this round and last round */
    
     g--;
        
    }

     printf("\nL C M : %d\n\nG C D : %d\n\n=====================================",re,gcd2);
    
   g=1;
  
   printf("\n\nEnter #%d Value , Enter 0 to exit : ",g);
      scanf("%d",&get[g]),g++;
     
      if(get[1]==0)
      {
        printf("\n");          
        break;
      }
}

}
   
   

int lcm(int a,int b)
{
    int c=b;
   
    if(a<b)
    c=a,a=b,b=c; 
   
    b=a-b;
   
    if(b!=0)
    {   
     while(a%b!=0)
     {
      c=b;
      b=a%b;
      a=c;
     }
    }
    else
    b=c;
   
    return b;
   
}

int gcd(int c,int d,int e)
{

    e = c * d / e;

         
    return e;
   
}


arrow
arrow
    全站熱搜

    finalfrank 發表在 痞客邦 留言(0) 人氣()