Advertisement
ntamas

2D-s tömb elemeinek rendezése listába töltéssel

Feb 3rd, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.   class Program
  9.   {
  10.     static void attoltes(ref int[,] tomb, ref List<int> lista, ref bool beki)
  11.     {
  12.       int k = 0;
  13.       for (int j = 0; j < tomb.GetLength(0); j++)
  14.       {
  15.         for (int i = 0; i < tomb.GetLength(1); i++)
  16.         {
  17.           if (beki == true)
  18.           {
  19.             lista.Add(tomb[j, i]);
  20.           }
  21.           else
  22.           {
  23.             tomb[j, i] = lista[k++];
  24.           }
  25.         }
  26.       }
  27.     }
  28.     static void rendezes(ref int[,] tomb)
  29.     {
  30.       List<int> lista = new List<int>();
  31.       bool beki = true;
  32.       attoltes(ref tomb, ref lista, ref beki);
  33.       lista.Sort();
  34.       beki = false;
  35.       attoltes(ref tomb, ref lista, ref beki);
  36.     }
  37.     static void tombfeltolt(ref int[,] tomb, Random vel, ref int tombhatar1, ref int tombhatar2)
  38.     {
  39.       int generalt = 0;
  40.       bool talal = false;
  41.       int i;
  42.       generalt = vel.Next(tombhatar1, tombhatar2+1);
  43.       tomb[0, 0] = generalt;
  44.       for (int j = 0; j < tomb.GetLength(0); j++)
  45.       {
  46.         if (j == 0)
  47.         {
  48.           i = 1;
  49.         }
  50.         else
  51.         {
  52.           i = 0;
  53.         }
  54.         for (; i < tomb.GetLength(1); i++)
  55.         {
  56.           do
  57.           {
  58.             talal = false;
  59.             generalt = vel.Next(tombhatar1, tombhatar2 + 1);
  60.             for (int k = 0; k < tomb.GetLength(0); k++)
  61.             {
  62.               for (int l = 0; l < tomb.GetLength(1); l++)
  63.               {
  64.                 if (generalt == tomb[k, l])
  65.                 {
  66.                   talal = true;
  67.                   break;
  68.                 }
  69.               }
  70.             }
  71.           } while (talal);
  72.           tomb[j, i] = generalt;
  73.         }
  74.       }
  75.     }
  76.     static void kiirat(ref int[,] tomb)
  77.     {
  78.       for (int j = 0; j < tomb.GetLength(0); j++)
  79.       {
  80.         for (int i = 0; i < tomb.GetLength(1); i++)
  81.         {
  82.           Console.Write("{0, 4} ", tomb[j, i]);
  83.         }
  84.         Console.WriteLine();
  85.       }
  86.     }
  87.     static void Main(string[] args)
  88.     {
  89.       uint sorszam, oszlopszam;
  90.       Console.Write("Adja meg a sorok számát: ");
  91.       sorszam = Convert.ToUInt32(Console.ReadLine());
  92.       Console.Write("Adja meg az oszlopok számát: ");
  93.       oszlopszam = Convert.ToUInt32(Console.ReadLine());
  94.       int[,] tomb = new int[sorszam, oszlopszam];
  95.       Console.WriteLine();
  96.       int tombhatar1 = 1, tombhatar2 = 600;
  97.       Random vel = new Random();
  98.       tombfeltolt(ref tomb, vel, ref tombhatar1, ref tombhatar2);
  99.       kiirat(ref tomb);
  100.       rendezes(ref tomb);
  101.       Console.WriteLine();
  102.       kiirat(ref tomb);
  103.       Console.WriteLine();
  104.       Console.ReadKey();
  105.     }
  106.   }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement