Advertisement
ntamas

rendezés és a többi + refek

Jan 21st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.85 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 bool isprime(int szam)
  11.     {
  12.       bool prim;
  13.       bool kilep = false;
  14.       if (szam == 0 || szam == 1)
  15.       {
  16.         prim = false;
  17.       }
  18.       else if (szam == 2)
  19.       {
  20.         prim = true;
  21.       }
  22.       else
  23.       {
  24.         prim = true;
  25.         for (int i = 2; i <= Math.Sqrt(szam) && !kilep; i++)
  26.         {
  27.           if (szam % i == 0)
  28.           {
  29.             prim = false;
  30.             kilep = true;
  31.           }
  32.         }
  33.       }
  34.       return prim;
  35.     }
  36.     static uint primekszama(ref int tombhatar1, ref int tombhatar2)
  37.     {
  38.       uint count = 0;
  39.       for (int i = tombhatar1; i <= tombhatar2; i++)
  40.       {
  41.         if (isprime(i))
  42.         {
  43.           count++;
  44.         }
  45.       }
  46.       return count;
  47.     }
  48.     static int nedikprim(int kezdoertek, int db)
  49.     {
  50.       uint szamlal = 0;
  51.       int i = kezdoertek;
  52.       do
  53.       {
  54.         if(isprime(i))
  55.         {
  56.           szamlal++;
  57.         }
  58.         i++;
  59.       }while(szamlal < db);
  60.       return i - 1;
  61.     }
  62.     static void tombfeltolt(ref int[,] tomb, Random vel, ref int tombhatar1, ref int tombhatar2)
  63.     {
  64.       int generalt = 0;
  65.       bool talal = false;
  66.       int i;
  67.       if(primekszama(ref tombhatar1, ref tombhatar2) < tomb.Length)
  68.       {
  69.         Console.WriteLine("Az intervallumon generálható prímek száma kevesebb, mint a mátrix mérete.");
  70.         Console.WriteLine("Legalább {0} db prímnek kell lennie és a tömbhatár {1} és {2} közt kell legyen!", tomb.Length, tombhatar1, nedikprim(tombhatar1, tomb.Length));
  71.       }
  72.       else
  73.       {
  74.         do
  75.         {
  76.           generalt = vel.Next(tombhatar1, tombhatar2+1);
  77.         } while (!isprime(generalt));
  78.         tomb[0, 0] = generalt;
  79.         for (int j = 0; j < tomb.GetLength(0); j++)
  80.         {
  81.           if (j == 0)
  82.           {
  83.             i = 1;
  84.           }
  85.           else
  86.           {
  87.             i = 0;
  88.           }
  89.           for (; i < tomb.GetLength(1); i++)
  90.           {
  91.             do
  92.             {
  93.               talal = false;
  94.               do
  95.               {
  96.                 generalt = vel.Next(tombhatar1, tombhatar2 + 1);
  97.               }while (!isprime(generalt));
  98.               for (int k = 0; k < tomb.GetLength(0); k++)
  99.               {
  100.                 for (int l = 0; l < tomb.GetLength(1); l++)
  101.                 {
  102.                   if (generalt == tomb[k, l])
  103.                   {
  104.                     talal = true;
  105.                     break;
  106.                   }
  107.                 }
  108.               }
  109.             } while (talal);
  110.             tomb[j, i] = generalt;
  111.           }
  112.         }
  113.       }
  114.     }
  115.     static void kiirat(ref int[,] tomb)
  116.     {
  117.       for (int j = 0; j < tomb.GetLength(0); j++)
  118.       {
  119.         for (int i = 0; i < tomb.GetLength(1); i++)
  120.         {
  121.           Console.Write("{0, 4} ", tomb[j, i]);
  122.         }
  123.         Console.WriteLine();
  124.       }
  125.     }
  126.     static void primekdb(ref int[,] tomb, ref uint primdb)
  127.     {
  128.       primdb = 0;
  129.       for (int j = 0; j < tomb.GetLength(0); j++)
  130.       {
  131.         for (int i = 0; i < tomb.GetLength(1); i++)
  132.         {
  133.           if (isprime(tomb[j, i]) == true)
  134.           {
  135.             primdb++;
  136.           }
  137.         }
  138.       }
  139.     }
  140.     static void paritas(ref int[,] tomb, ref uint paros, ref uint paratlan)
  141.     {
  142.       paros = 0;
  143.       paratlan = 0;
  144.       for (int j = 0; j < tomb.GetLength(0); j++)
  145.       {
  146.         for (int i = 0; i < tomb.GetLength(1); i++)
  147.         {
  148.           if (tomb[j, i] % 2 == 0)
  149.           {
  150.             paros++;
  151.           }
  152.           else
  153.           {
  154.             paratlan++;
  155.           }
  156.         }
  157.       }
  158.     }
  159.     static void kereses(ref int[,] tomb, ref int adott, ref uint sor, ref uint oszlop)
  160.     {
  161.       sor = 0;
  162.       oszlop = 0;
  163.       for (int j = 0; j < tomb.GetLength(0); j++)
  164.       {
  165.         for (int i = 0; i < tomb.GetLength(1); i++)
  166.         {
  167.           if (tomb[j, i] == adott)
  168.           {
  169.             sor = Convert.ToUInt32(j + 1);
  170.             oszlop = Convert.ToUInt32(i + 1);
  171.           }
  172.         }
  173.       }
  174.       if (sor == 0 && oszlop == 0)
  175.       {
  176.         Console.WriteLine("Az elem nem található a tömbben.");
  177.       }
  178.       else
  179.       {
  180.         Console.WriteLine("A begépelt elem sorindexe: {0}, oszlopindexe: {1}", sor, oszlop);
  181.       }
  182.     }
  183.     static void attoltes(ref int[,] tomb, ref int[] tomb2, ref bool beki)
  184.     {
  185.       uint k = 0;
  186.       for(int j = 0; j < tomb.GetLength(0); j++)
  187.       {
  188.         for (int i = 0; i < tomb.GetLength(1); i++ )
  189.         {
  190.           if (beki == true)
  191.           {
  192.             tomb2[k++] = tomb[j, i];
  193.           }
  194.           else
  195.           {
  196.             tomb[j, i] = tomb2[k++];
  197.           }
  198.         }
  199.       }
  200.     }
  201.     static void rendezes(ref int[,] tomb, ref int[] tomb2)
  202.     {
  203.       bool beki = true;
  204.       attoltes(ref tomb, ref tomb2, ref beki);
  205.       int tarolo;
  206.       uint minindex = 0;
  207.       for (uint i = 0; i < tomb2.Length - 1; i++)
  208.       {
  209.         minindex = i;
  210.         for (uint j = i + 1; j < tomb2.Length; j++)
  211.         {
  212.           if (tomb2[minindex] > tomb2[j])
  213.           {
  214.             minindex = j;
  215.           }
  216.         }
  217.         if (minindex != i)
  218.         {
  219.           tarolo = tomb2[minindex];
  220.           tomb2[minindex] = tomb2[i];
  221.           tomb2[i] = tarolo;
  222.         }
  223.       }
  224.       beki = false;
  225.       attoltes(ref tomb, ref tomb2, ref beki);
  226.     }
  227.     static void Main(string[] args)
  228.     {
  229.       Console.Write("Írja be a tömb sorainak számát: ");
  230.       uint sorszam = Convert.ToUInt32(Console.ReadLine());
  231.       Console.Write("Írja be a tömb oszlopainak számát: ");
  232.       uint oszlopszam = Convert.ToUInt32(Console.ReadLine());
  233.       uint paros = 0, paratlan = 0, primdb = 0, sor = 0, oszlop = 0;
  234.       int[,] tomb = new int[sorszam, oszlopszam];
  235.       int[] tomb2 = new int[tomb.Length];
  236.       Random vel = new Random();
  237.       Console.Write("Írja be a tömb alsó határát: ");
  238.       int tombhatar1 = Convert.ToInt32(Console.ReadLine());
  239.       Console.Write("Írja be a tömb felső határát: ");
  240.       int tombhatar2 = Convert.ToInt32(Console.ReadLine());
  241.       tombfeltolt(ref tomb, vel, ref tombhatar1, ref tombhatar2);
  242.       kiirat(ref tomb);
  243.       Console.WriteLine();
  244.       paritas(ref tomb, ref paros, ref paratlan);
  245.       primekdb(ref tomb, ref primdb);
  246.       Console.WriteLine("Párosak darabszáma: {0}\nPáratlanok darabszáma: {1}", paros, paratlan);
  247.       Console.WriteLine("Prímek darabszáma: {0}", primdb);
  248.       Console.Write("Adja meg a keresendő számot: ");
  249.       int adott = Convert.ToInt32(Console.ReadLine());
  250.       kereses(ref tomb, ref adott, ref sor, ref oszlop);
  251.       rendezes(ref tomb, ref tomb2);
  252.       kiirat(ref tomb);
  253.       Console.ReadKey();
  254.     }
  255.   }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement