2018年2月28日 星期三

[C_MM179-易] 最大公因數與最小公倍數(C++)

[C_MM179-易] 最大公因數與最小公倍數

問題描述 
輸入四個整數數字,求出此四個整數數字的最大公因數與最小公倍數。例如:
10 12 18 20 最大公因數為 2 ,最小公倍數為 180 。
12 18 24 36 最大公因數為 6 ,最小公倍數為 72 。
輸入格式
輸入格式為一行包含四個整數值,每個數字之間以一個空白隔開。
輸出格式
輸出值為最大公因數與最小公倍數,每個數字之間以一個空白隔開。
Example
Sample Input:Sample Output:
10 12 18 20
12 18 24 36
2 180
6 72

  1. #include <iostream>  
  2. #include <algorithm>  
  3. using namespace std;  
  4. int factor(intintintint); //最大公因數  
  5. int multiple(intintintint); //最小公倍數  
  6. int main() {  
  7.     // [C_MM179-易] 最大公因數與最小公倍數  
  8.     int n1, n2, n3, n4;  
  9.     cin >> n1 >> n2 >> n3 >> n4;  
  10.     cout << factor(n1, n2, n3, n4) << " " << multiple(n1, n2, n3, n4) << endl;  
  11.       
  12.     return 0;  
  13. }  
  14. int factor(int a, int b, int c, int d){  
  15.     int s[4] = {a, b, c, d};  
  16.     sort(s, s+4);  
  17.     for(int i = s[0];i > 0;i--)  
  18.     {  
  19.         if(a%i==0 && b%i==0 && c%i==0 && d%i==0)  
  20.         {  
  21.             return i;  
  22.             break;  
  23.         }  
  24.     }  
  25. }   
  26. int multiple(int a, int b, int c, int d){  
  27.     int s[4] = {a, b, c, d};  
  28.     sort(s, s+4);  
  29.     for(int i = s[3];;i++)  
  30.     {  
  31.         if(i%a==0 && i%b==0 && i%c==0 && i%d==0)  
  32.         {  
  33.             return i;  
  34.             break;  
  35.         }  
  36.     }  
  37.       
  38. }  

[C_MM167-易] 質數輸出(C++)

[C_MM167-易] 質數輸出

質數輸出
問題描述 
質數是一種只能由數字 1 或本身能除盡的數字。如果需要輸出介於 N 到 M (含 N 與 M) 範圍所有的質數,最簡單的作法是利用雙迴圈。外迴圈逐一條列 N 至 M 每個數字,內迴圈則檢視被條列的數字是否為質數。一個數字 x 如果為質數,表示由 2 至 x-1 所有的數字皆無法除盡 x 。請寫一個程式,輸出介於任兩數字間的所有質數。
輸入說明 
每一行輸入兩個數字由單一空格分隔,分別表示題意的 N 與 M ( N, M>=0 且 N≤M≤2014 )。如果 N 與 M 皆為數字 0 則結束程式執行。
輸出說明 
輸出介於 N 與 M (含 N 與 M )範圍的所有質數,每輸出一個數字後方留單一空格。
範例 :

輸入範例輸出範例
2 10
13 18
0 0
2 3 5 7
13 17

  1. #include <iostream>  
  2. using namespace std;  
  3. int PRIME(intintintint*);   
  4. int main() {  
  5.     // [C_MM167-易] 質數輸出  
  6.     while(true)  
  7.     {  
  8.         int x, y;  
  9.         cin >> x >> y;  
  10.         int count = 0;    
  11.         int prime[10000];    
  12.         if(x != 0 && y != 0)  
  13.         {  
  14.             PRIME(x, y, count, prime);  
  15.         }  
  16.         else  
  17.         {  
  18.             break;  
  19.         }  
  20.     }  
  21.     return 0;  
  22. }  
  23. int PRIME(int x, int y, int r, int *p){    
  24.      for(int i = x;i <= y;i++)    
  25.      {    
  26.           int c = 0;    
  27.           for(int j = 1;j <= i;j++)    
  28.           {    
  29.                if(i % j == 0)    
  30.                {    
  31.                 c++;    
  32.             }    
  33.         }    
  34.         if(c == 2)    
  35.         {    
  36.             p[r] = i;    
  37.             r++;    
  38.         }    
  39.      }    
  40.          
  41.      if(r != 0)  
  42.      {    
  43.         for(int i = 0;i < r;i++)    
  44.         {    
  45.             cout << p[i] << " ";    
  46.         }    
  47.         cout << endl;    
  48.     }    
  49. }  

[C_MM159-易] 給定三邊長求三角形面積(C++)

[C_MM159-易] 給定三邊長求三角形面積

Problem Description 
小明想利用三線段做出一個三角形,並且計算出這個三角形的面積出來,因為圍出來的三角形不一定知道它的高是多少,因此可使用 「海龍公式」來求得面積。
海龍公式是假設三邊長的和為 s ,面積 S 可用下面公式表示:
CMM159.JPG
Input File Format 
Input 資料為給定的三邊長,資料型態為浮點數 。
Output Format 
先判斷此三邊長能否形成三角形,若可以再利用海龍公式算面積;如果不行,則 output 不能成為三角形 。
Example
Sample Input:Sample Output:
3
4
5
6.000000
5
5
6
12.000000
11
12
13
61.481705
4.5
5.2
8
10.928791
1
10
11
no answer


  1. #include <iostream>  
  2. #include <cmath>  
  3. #include <iomanip>  
  4. using namespace std;  
  5. double area(doubledoubledouble);  
  6. int main() {  
  7.     // [C_MM159-易] 給定三邊長求三角形面積  
  8.     double x, y, z;  
  9.     cin >> x >> y >> z;  
  10.     if(area(x, y, z) > 0)  
  11.     {  
  12.         cout << fixed << setprecision(6) << area(x, y, z) << endl;  
  13.     }  
  14.     else  
  15.     {  
  16.         cout << "no answer" << endl;  
  17.     }  
  18.     return 0;  
  19. }  
  20. double area(double a, double b, double c){  
  21.     if(a+b>c && a+c>b && b+c>a)  
  22.     {  
  23.         double s = (a+b+c)/2;  
  24.         double S = sqrt(s*(s-a)*(s-b)*(s-c));  
  25.         return S;  
  26.     }  
  27.     else  
  28.     {  
  29.         return -1;  
  30.     }  
  31. }  

[C_AR178-易] 猜數字之提示(C++)

[C_AR178-易] 猜數字之提示

Time Limit: 2 seconds
問題描述 :
已知,猜數字是兩個人玩的遊戲,一方出題,另一方猜。出題的人要先想好一個「沒有重複數字」的 4 位整數,當作是答案,不能讓猜的人知道。猜的人就可以開始猜。每猜一次,出題者就要根據猜數給出「幾 A 幾 B 」,當作是提示,其中 A 前面的數字表示「數字猜中且位置也猜中」的個數,而 B 前的數字表示「數字猜中且位置沒猜中」的個數。例如,答案為 6234 而猜數 5246 ,則提示「 1A2B 」,因為 2 不但被猜中且位置也對了,記為 1A ,而 6 和 4 這兩個數字猜中且位置沒猜中,因此記為 2B ,合起來就是 1A2B 。
針對每一個題目,都只猜一次,然後程式就輸出該次猜數的提示。
輸入說明 :
程式首先要求輸入一個正整數,代表接下來有幾對「答案 - 猜數」,然後依序輸入相應對數的「答案 - 猜數」
例如,
2
1234 4321
5678 5721
表示有 2 對「答案 - 猜數」,第一題的答案是 1234 ,而相應的猜數是 4321 ,第二題的答案是 5678 ,而相應的猜數是 5721 。
輸出說明 :
針對每一組 「答案 - 猜數」,輸出相應的提示,並換行
例如,針對上述輸入,第一題的答案是 1234 ,而相應的猜數是 4321 ,故提示 0A4B ,第二題的答案是 5678 ,而相應的猜數是 5721 故提示 1A1B 。所以輸出如下 :
0A4B
1A1B
範例 :

輸入範例輸出範例
2
1234 4321
5678 5721
0A4B
1A1B
  1. #include <iostream>  
  2. using namespace std;  
  3. int guessA(intint);  
  4. int guessB(intint);  
  5. int main() {  
  6.     // [C_AR178-易] 猜數字之提示  
  7.     int N;  
  8.     cin >> N;  
  9.     for(int i = 0;i < N;i++)  
  10.     {  
  11.         int ans, gue;  
  12.         cin >> ans >> gue;  
  13.         cout << guessA(ans, gue) << "A" << guessB(ans, gue) << "B" << endl;  
  14.     }  
  15.     return 0;  
  16. }  
  17. int guessA(int a, int b){  
  18.     string A = to_string(a);  
  19.     string B = to_string(b);  
  20.       
  21.     int Acount = 0;  
  22.     for(int i = 0;i < 4;i++)  
  23.     {  
  24.         if(A[i] == B[i])  
  25.         {  
  26.             Acount++;  
  27.         }  
  28.     }  
  29.     return Acount;  
  30. }  
  31. int guessB(int a, int b){  
  32.     string A = to_string(a);  
  33.     string B = to_string(b);  
  34.     int Bcount = 0;  
  35.     for(int i = 0;i < 4;i++)  
  36.     {  
  37.         for(int j = 0;j < 4;j++)  
  38.         {  
  39.             if(i != j && A[i] == B[j])  
  40.             {  
  41.                 Bcount++;  
  42.             }  
  43.         }  
  44.     }  
  45.     return Bcount;  
  46. }