下の二つの関数の違いを教えて下さい。上の方は数字が大きくなると正しい答えが出ません
c
1#include <stdio.h> 2int yugurid(int number1, int number2) /*ユーグリッドの互除法による最大公約数*/ 3{ 4 int r, a, b; 5 6 if (number1 < number2){ 7 a = number1; 8 b = number2; 9 } 10 else { 11 a = number2; 12 b = number1; 13 } 14 15 r = b % a; 16 17 while (r != 0) 18 { 19 a = b; 20 b = r; 21 r = b % a; 22 } 23 return a; /* a = 最大公約数 */ 24}
c
1#include <stdio.h> 2 3int yugurid(int x, int y) 4{ 5 if(x % y == 0) 6 return y; 7 return yugurid(y, x % y); 8}
具体的に正しい結果が出ないときの、状況(number1,number2の値)は何ですか?
回答3件
あなたの回答
tips
プレビュー