-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC语言 求两数的最大公约数和最小公倍数.html
36 lines (36 loc) · 4.14 KB
/
C语言 求两数的最大公约数和最小公倍数.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<div class="cnblogs_code">
<pre><span style="color: #008080;"> //</span>作者:凯鲁嘎吉 - 博客园 <em id="__mceDel">http://www.cnblogs.com/kailugaji/</em></pre>
<pre><span style="color: #008080;"> 1</span> #include<stdio.h>
<span style="color: #008080;"> 2</span> <span style="color: #008000;">//</span><span style="color: #008000;">最大公约数</span>
<span style="color: #008080;"> 3</span> <span style="color: #0000ff;">int</span> gys(<span style="color: #0000ff;">int</span> x,<span style="color: #0000ff;">int</span><span style="color: #000000;"> y){
</span><span style="color: #008080;"> 4</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> r;
</span><span style="color: #008080;"> 5</span> <span style="color: #0000ff;">while</span>(y!=<span style="color: #800080;">0</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 6</span> r=x%<span style="color: #000000;">y;
</span><span style="color: #008080;"> 7</span> x=<span style="color: #000000;">y;
</span><span style="color: #008080;"> 8</span> y=<span style="color: #000000;">r;
</span><span style="color: #008080;"> 9</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">10</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> x;
</span><span style="color: #008080;">11</span> <span style="color: #000000;">}
</span><span style="color: #008080;">12</span> <span style="color: #008000;">//</span><span style="color: #008000;">最小公倍数</span>
<span style="color: #008080;">13</span> <span style="color: #0000ff;">int</span> gbs(<span style="color: #0000ff;">int</span> x,<span style="color: #0000ff;">int</span><span style="color: #000000;"> y){
</span><span style="color: #008080;">14</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> z;
</span><span style="color: #008080;">15</span> z=x*y/<span style="color: #000000;">gys(x,y);
</span><span style="color: #008080;">16</span> <span style="color: #0000ff;">return</span><span style="color: #000000;"> z;
</span><span style="color: #008080;">17</span> <span style="color: #000000;">}
</span><span style="color: #008080;">18</span>
<span style="color: #008080;">19</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> main(){
</span><span style="color: #008080;">20</span> <span style="color: #0000ff;">int</span><span style="color: #000000;"> x,y;
</span><span style="color: #008080;">21</span> printf(<span style="color: #800000;">"</span><span style="color: #800000;">Please input two numbers:\n</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008080;">22</span> scanf(<span style="color: #800000;">"</span><span style="color: #800000;">%d %d</span><span style="color: #800000;">"</span>,&x,&<span style="color: #000000;">y);
</span><span style="color: #008080;">23</span> <span style="color: #0000ff;">if</span>(x<y){<span style="color: #008000;">//</span><span style="color: #008000;">交换x, y</span>
<span style="color: #008080;">24</span> x=x+<span style="color: #000000;">y;
</span><span style="color: #008080;">25</span> y=x-<span style="color: #000000;">y;
</span><span style="color: #008080;">26</span> x=x-<span style="color: #000000;">y;
</span><span style="color: #008080;">27</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">28</span> printf(<span style="color: #800000;">"</span><span style="color: #800000;">%d 与 %d 的最大公约数为 %d\n</span><span style="color: #800000;">"</span><span style="color: #000000;">,x,y,gys(x,y));
</span><span style="color: #008080;">29</span> printf(<span style="color: #800000;">"</span><span style="color: #800000;">%d 与 %d 的最小公倍数为 %d\n</span><span style="color: #800000;">"</span><span style="color: #000000;">,x,y,gbs(x,y));
</span><span style="color: #008080;">30</span> }</pre>
</div>
<p>结果为:</p>
<p><img src="https://images2018.cnblogs.com/blog/1027447/201803/1027447-20180317171044247-1690964442.png" alt="" /></p>
<p> </p>