CSharp · Console
using System;
public class Program
{
public static int[] GcdExtended(int a, int b)
{
int[] result = new int[3]; // d x y
if (b == 0)
{
result[0] = a; result[1] = 1; result[2] = 0;
return result;
}
result = GcdExtended(b, a % b);
int s = result[2];
result[2] = result[1] - (a / b) * result[2];
result[1] = s;
return result;
}
public static void GcdExtendedWrapper(int a, int b)
{
int[] result = GcdExtended(a, b);
if (result[0] > 1)
{
Console.WriteLine("No answer!");
}
else
{
if (result[1] < 0)
{
result[1] += b; result[2] -= a;
}
Console.WriteLine(string.Format("Answer: {0} {1}", result[1], result[2]));
}
}
public static void Main()
{
Console.WriteLine("UniLecs");
GcdExtendedWrapper(7, 11); // 8 -5
GcdExtendedWrapper(5, 3); // 2 -3
}
}