НОД и НОК, двумерный массив - C#
Формулировка задачи:
Ребзяяя, помогите, есть прога на нахождение наибольшего общего делимого(НОД) и наибольшего общего кратного(НОК). Но заданна как в двумерном массиве.
вот то что было
и вот что я пытался исправить, но ошибкииии...
помогите кто сможет
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Введите массив целых положительных чисел через пробел");
String[] ss = Console.ReadLine().Split(' ');
int[] arr = new int[ss.Length];
for (int i = 0; i < arr.Length; i++) arr[i] = Int32.Parse(ss[i]);
int nok = arr[0];
int nod = nok;
for (int i = 1; i < arr.Length; i++)
{
nod = getNOD(nod, arr[i]);
nok = getNOK(nok, arr[i]); // функции getNOK и getNOD для двух чисел
}
Console.WriteLine("Для массива НОД = {0}, НОК = {1}", nod, nok);
Console.ReadLine();
}
// Возвращает наибольший общий делитель двух целых положительных чисел
static int getNOD(int a, int b) {
for (int i = (int) Math.Sqrt(Math.Max(a, b)); i >= 2; i--)
if (((a % i) == 0) & ((b % i) == 0)) {
return i;
}
return 1;
}
// Возвращает наименьшее общее кратное двух целых положительных чисел
static int getNOK(int a, int b)
{
return (int) a * b / getNOD(a, b);
}
}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication9
{
class Program
{
static void Main(string[] args)
{
const int m = 3, n = 3;
int j = 0;
int i = 0;
int[,] a = new int[m, n]
{
{2,4,6},
{8,10,12},
{14,16,18}
};
Console.WriteLine("Исходный массив");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++) ;
Console.Write(" " + a[i, j]);
Console.WriteLine();
}
{
int nok = a[0, 0];
int nod = nok;
nod = getNOD(nod, a[i,j]);
nok = getNOK(nok, a[i,j]);
}
/*Console.WriteLine("Для массива НОД = {0}, НОК = {1}", getNOK, getNOD);
Console.ReadLine();*/
}
static int getNOD(int c, int b)
{
for (int i = (int)Math.Min(c, b); i > 1; i--)
if (((c % i) == 0) & ((b % i) == 0))
{
return i;
}
return 1;
}
static int getNOK(int c, int b)
{
return (int) c * b / getNOD(c, b);
}
}
}Решение задачи: «НОД и НОК, двумерный массив»
textual
Листинг программы
static void Main()
{
int[,] a = { { 2, 4, 6 }, { 8, 10, 12 }, { 14, 16, 18 } };
int gcd = a[0, 0], lcm = 1;
foreach (var x in a)
{
gcd = getNOD(gcd, x);
lcm = getNOK(lcm, x);
}
Console.WriteLine("gcd = {0} lcm = {1}", gcd, lcm);
Console.ReadLine();
}