Holey_yan

20140318_KUAS_VisualC++_Exam04

Mar 19th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. /************************************/
  2. /** 20140318 Exam 04               **/
  3. /** Maker  : Yan                    **/
  4. /** E-Mail : [email protected] **/
  5. /** Date   : 2014/03/18 pm.11:30    **/
  6. /************************************/
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. void main()
  12. {
  13.     //定義變數, number_a, number_b 分別為輸入之兩數值
  14.     int number_a = 0, number_b = 0;
  15.     printf("Enter number, -1 is exit program.\n");
  16.  
  17.     //當number_a 或是 number_b \值不為-1 時執行迴圈
  18.     while(number_a != -1 && number_b != -1)
  19.     {
  20.         //輸入number_a
  21.         printf("Enter first  number: ");
  22.         scanf("%d", &number_a);
  23.         //強制特別條件: 當number_a 輸入為-1 時立即結束程式
  24.         if(number_a == -1)
  25.         {
  26.             printf("Your Program is exiting ..\n");
  27.             system("pause");
  28.             return;
  29.         }
  30.  
  31.         //輸入number_b
  32.         printf("Enter second number: ");
  33.         scanf("%d", &number_b);
  34.         //強制特別條件: 當number_a 輸入為-1 時立即結束程式
  35.         if(number_b == -1)
  36.         {
  37.             printf("Your Program is exiting ..\n");
  38.             system("pause");
  39.             return;
  40.         }
  41.  
  42.         //定義變數min, min 為number_a 與number_b 之較小者
  43.         int min = 0;
  44.         if(number_a < number_b)
  45.         {
  46.             min = number_a;
  47.         }
  48.         else
  49.         {
  50.             min = number_b;
  51.         }
  52.  
  53.         /*尋找公因數
  54.           公因數之定義: 兩數能被一數值整除, 其數值即為兩數之公因數
  55.           因公因數必比兩數小, 在此利用迴圈逐一檢查*/
  56.         for(int i = 1; i <= min; i++)
  57.         {
  58.             //當迴圈數值i 可整除number_a 與number_b 時, 此數即為公因數而輸出
  59.             if(number_a % i == 0 && number_b % i == 0)
  60.             {
  61.                 if(i == 1)
  62.                 {
  63.                     printf("%d", i);
  64.                 }
  65.                 else if(i != 1)
  66.                 {
  67.                     printf(", %d", i);
  68.                 }
  69.  
  70.             }
  71.         }
  72.         printf("\n");
  73.     }
  74.  
  75.     system("pause");
  76. }
Advertisement
Add Comment
Please, Sign In to add comment