6 喜歡 0 不喜歡
1.3k 瀏覽

Write a program that asks the user to enter two integers, then calculates and displays their greatest common divisor (GCD).

Hint: The classic algorithm for computing the GCD, known as Euclid's algorithm, goes as follow: let m and n be variables containing the two numbers. If n is 0, then stop: m contains the GCD. Otherwise, compute the remainder when m is divided by n. Copy n into m and copy the remainder into n. Then repeat the process, starting with testing whether n is 0.

Example input: 

12 28

Example output:

4

 

[正常] Coding (C) - 最新提問 分類:Introduction to Computer Programming I (C) | 用戶: (12.1k 分)
ID: 27175 - 從幾時開始: 無限制 - 到幾時結束: 無限制

標籤修改 用戶: | 1.3k 瀏覽

2 個回答

0 喜歡 0 不喜歡
內容已隱藏
#include * * ***** * **
int main()
{
** * * **** *** ** ** ** * n1, n2, i, gcd;
* *** *** * * ** *** * * * %d", &n1, * * * ***
* *** * * *** ****
****** * **** * ** * ** * i <= n1 *** ** i <= n2; ++i)
* ** ** * ******* ***
* ** * * * * ** * * * ** * ** * * ** * * Checks if i is factor of both integers
* * ***** ** * *** * **** *** *** ***** ***** * * * * ** * ** * n2%i==0)
** * ** ** ** * * * * ** * * * ** * * *** * ** *** = i;
* ** ** * ** ** *
* * ** * **** **
*** * * * * ** ******* * * * * * * **** * gcd);
* ** * ** ** * **** * * 0;
}
最新回答 用戶: (-233 分)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
0 喜歡 0 不喜歡
內容已隱藏
#include * * *** *
int main()
{
* **** * * *** * ** * n1, n2, i, gcd = 0;
* ** * ** * ** * ** ** *
* ** ** ** * *** * * ** ** * **** * **** *** * * %d", &n1, &n2);
** ** * * ** **** ** *
***** * ** * ** * * ** i <= n1 && i <= n2; ++i)
* *** ***** * * ****
** ** * ** * ** * ** * *** ** ** *** * ****** && n2%i==0)
** * **** ** * * ***** ** *** ***** ** ****** * * * ** * *** *** * * = i;
* * *** * * ** **** * **
* * **** * * *** *
*** * * * * **** ** * *** ** ** ** * ****
* ** * * *** * *** **
* ***** ** * * ******* ** * 0;
}
最新回答 用戶: (323 分)
0 0
Case 0: Correct output
Case 1: Correct output
Case 2: Correct output
Case 3: Correct output
Case 4: Correct output
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.179.156
©2016-2025

相關問題

6 喜歡 1 不喜歡
1 回答
[正常] Coding (C) - 最新提問 10月 10, 2017 分類:Introduction to Computer Programming I (C) | 用戶: admin (9.9k 分)
ID: 25954 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 1.5k 瀏覽
5 喜歡 0 不喜歡
3 回答
[正常] Coding (C) - 最新提問 10月 10, 2017 分類:Introduction to Computer Programming I (C) | 用戶: admin (9.9k 分)
ID: 25955 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 1.5k 瀏覽
12,783 問題
183,442 回答
172,219 留言
4,824 用戶