using System;
public class Test
{
public static void Main()
{
// your code goes here
int a,b,d,x,y;
string str;
str=Console.ReadLine();
int.TryParse(str,out a);
str=Console.ReadLine();
int.TryParse(str,out b);
d = gcdex(a,b,out x,out y);
Console.WriteLine("NWD({0},{1})={2}",a,b,d);
Console.WriteLine("Wspolczynniki przy a oraz b to: {0} {1}",x,y);
}
public static int gcdex(int a,int b,out int x,out int y)
{
int c,temp;
int top = -1;
int[] S = new int[10];
x = 1;
y = 0;
while(b != 0)
{
top++;
if(top == S.Length)
Array.Resize(ref S,2*S.Length);
S[top] = a/b;
temp = a;
a = b;
b = temp % b;
}
while(top != -1)
{
c = S[top];
top--;
temp = x;
x = y;
y = temp - c * y;
}
if(a < 0)
{
x *= -1;
y *= -1;
return -a;
}
else
return a;
}
}
Jak przepisać tę funkcję tak aby stos nie był potrzebny
Funkcja licząca NWD działa jednak przez to użycie stosu funkcja nie jest optymalna pamięciowo