返回列表 發帖

求最大公因數

本帖最後由 tonyh 於 2021-8-17 17:30 編輯

  1. import java.util.Scanner;

  2. public class Ch01 {

  3.         public static void main(String[] args) {
  4.                 int x, y, smaller, gcd=0;
  5.                 Scanner s=new Scanner(System.in);
  6.             System.out.print("請依序輸入兩個正整數: ");
  7.                 x=s.nextInt();
  8.                 y=s.nextInt();
  9.                 smaller=x<y?x:y;
  10.                 for(int i=1; i<=smaller; i++)
  11.                         if(x%i==0 && y%i==0)
  12.                                 gcd=i;
  13.                 System.out.printf("%d與%d的最大公因數為: %d",x,y,gcd);
  14.         }
  15. }
複製代碼
  1. import java.util.Scanner;

  2. public class Ch01 {

  3.         public static void main(String[] args) {
  4.                 int x, y, smaller, gcd=0;
  5.                 Scanner s=new Scanner(System.in);
  6.             System.out.print("請依序輸入兩個正整數: ");
  7.                 x=s.nextInt();
  8.                 y=s.nextInt();
  9.                 smaller=x<y?x:y;
  10.                 for(int i=smaller; i>=1; i--)
  11.                 {
  12.                         if(x%i==0 && y%i==0)
  13.                         {
  14.                                 gcd=i;
  15.                                 break;
  16.                         }
  17.                 }
  18.                 System.out.printf("%d與%d的最大公因數為: %d",x,y,gcd);
  19.         }
  20. }
複製代碼
  1. import java.util.Scanner;

  2. public class Ch01 {

  3.         public static void main(String[] args) {
  4.                 int x, y;
  5.                 Scanner s=new Scanner(System.in);
  6.             System.out.print("請依序輸入兩個正整數: ");
  7.                 x=s.nextInt();
  8.                 y=s.nextInt();
  9.                 System.out.printf("%d與%d的最大公因數為: %d",x,y,gcd(x,y));
  10.         }
  11.        
  12.         static int gcd(int x, int y)
  13.         {
  14.                 while(x%y!=0)
  15.                 {
  16.                         int tmp=x%y;
  17.                         x=y;
  18.                         y=tmp;
  19.                 }
  20.                 return y;
  21.         }
  22. }
  23. /*     x    y         x%y
  24.       56 / 35 = 1 ... 21
  25.       35 / 21 = 1 ... 14
  26.       21 / 14 = 1 ... 7
  27.       14 / 7  = 2 ... 0
  28. */
複製代碼
  1. import java.util.Scanner;

  2. public class Ch01 {

  3.         public static void main(String[] args) {
  4.                 int x, y;
  5.                 Scanner s=new Scanner(System.in);
  6.             System.out.print("請依序輸入兩個正整數: ");
  7.                 x=s.nextInt();
  8.                 y=s.nextInt();
  9.                 System.out.printf("%d與%d的最大公因數為: %d",x,y,gcd(x,y));
  10.         }
  11.        
  12.         static int gcd(int x, int y)
  13.         {
  14.                 if(x%y==0)
  15.                         return y;
  16.                 else
  17.                         return gcd(y, x%y);
  18.         }
  19. }
  20. /*     x    y         x%y
  21.       56 / 35 = 1 ... 21
  22.       35 / 21 = 1 ... 14
  23.       21 / 14 = 1 ... 7
  24.       14 / 7  = 2 ... 0
  25. */
複製代碼

返回列表