Patryk27
Z Twoim NWD daje 0.34 ms.
Po zmianie int na uint -> 0.36 ms.
A taki kod daje 0.36 ms:
using System;
namespace ConsoleApplication1
{
class Program
{
static int nwd(int a, int b)
{
int c;
do
{
c = a % b;
a = b;
b = c;
} while (0 != (b = c));
return a;
}
static void Main(string[] args)
{
int count, separator;
string numbers;
Int32.TryParse(Console.ReadLine(), out count);
for (int i = 0; i < count; i++)
{
separator = (numbers = Console.ReadLine()).IndexOf(' ');
Console.WriteLine(nwd(
Int32.Parse(numbers.Substring(0, separator)),
Int32.Parse(numbers.Substring(++separator, numbers.Length - separator))));
}
}
}
}
Zmiana Int32.Parse na Convert.ToInt32 - > 0.34 ms
Zmiana Int32.TryParse na int.TryParse i Int32.Parse na Convert.ToInt32 -> 0.33 ms
Dodatkowa zmiana Convert.ToInt32 na int.Parse -> 0.33 ms
Zastosowanie .Split(' ') i tablicy -> 0.35 ms
Zamiana .IndexOf(' ') na pętle -> 0.33 ms
for (separator = 0; separator < numbers.Length; separator++)
if (' ' == numbers[separator])
break;
Kod końcowy, czas wykonania 0.31 ms:
class Program
{
static int nwd(int a, int b)
{
int c;
do
{
c = a % b;
a = b;
b = c;
} while (0 != b);
return a;
}
static void Main()
{
int count, separator, i;
string numbers;
int.TryParse(System.Console.ReadLine(), out count);
for (i = 0; i < count; i++)
{
numbers = System.Console.ReadLine();
for (separator = 0; separator < numbers.Length; separator++)
if (' ' == numbers[separator])
break;
System.Console.WriteLine(nwd(
int.Parse(numbers.Substring(0, separator)),
int.Parse(numbers.Substring(++separator, numbers.Length - separator))));
}
}
}