Advertisement
ntamas

nov. 6. 3. program

Nov 6th, 2013
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace oraimunka
  7. {
  8.   class Program
  9.   {
  10.     static bool isprime(int szam)
  11.     {
  12.       if (szam == 0 || szam == 1)
  13.       {
  14.         return false;
  15.       }
  16.       else if (szam == 2)
  17.       {
  18.         return true;
  19.       }
  20.       else
  21.       {
  22.         bool prim = true;
  23.         for (int i = 2; i < szam; i++)
  24.         {
  25.           if (szam % i == 0)
  26.           {
  27.             prim = false;
  28.             break;
  29.           }
  30.         }
  31.         return prim;
  32.       }
  33.     }
  34.     static void tombfeltoltes(int[] tomb, int elsoelem, int utolsoelem, int elemszam, bool prime, Random vel)
  35.     {
  36.       bool talal;
  37.       if (elsoelem > utolsoelem)
  38.       {
  39.         int cs;
  40.         cs = elsoelem;
  41.         elsoelem = utolsoelem;
  42.         utolsoelem = cs;
  43.       }
  44.       if (elemszam < 2)
  45.       {
  46.         elemszam = utolsoelem - elsoelem + 1;
  47.       }
  48.       if (prime == true)
  49.       {
  50.         do
  51.         {
  52.           tomb[0] = vel.Next(elsoelem, utolsoelem + 1);
  53.         }while(!isprime(tomb[0]));
  54.       }
  55.       else
  56.       {
  57.         tomb[0] = vel.Next(elsoelem, utolsoelem + 1);
  58.       }
  59.       for (int i = 1; i < elemszam; i++)
  60.       {
  61.         do
  62.         {
  63.           talal = false;
  64.           if (prime == true)
  65.           {
  66.             do
  67.             {
  68.                tomb[i] = vel.Next(elsoelem, utolsoelem + 1);
  69.             }while(!isprime(tomb[i]));
  70.           }
  71.           else
  72.           {
  73.             tomb[i] = vel.Next(elsoelem, utolsoelem + 1);
  74.           }
  75.           for (int j = 0; j < i; j++)
  76.           {
  77.             if (tomb[j] == tomb[i])
  78.             {
  79.               talal = true;
  80.             }
  81.           }
  82.         } while (talal);
  83.       }
  84.     }
  85.     static void Main(string[] args)
  86.     {
  87.       int elemszam = 5;
  88.       int[] tomb = new int[elemszam];
  89.       int[] tipp = new int[5] {8, 14, 17, 32, 81};
  90.       int tippelemszam = tipp.Length;
  91.       Random vel = new Random();
  92.       int talalat = 0;
  93.       int hanyadik = 0;
  94.       do
  95.       {
  96.         tombfeltoltes(tomb, 1, 90, elemszam, false, vel);
  97.         for (int i = 0; i < elemszam; i++)
  98.         {
  99.           Console.Write("{0}\t", tomb[i]);
  100.         }
  101.         talalat = 0;
  102.         for (int i = 0; i < elemszam; i++)
  103.         {
  104.           for (int k = 0; k < tippelemszam; k++)
  105.           {
  106.             if (tomb[i] == tipp[k])
  107.             {
  108.               talalat++;
  109.             }
  110.           }
  111.         }
  112.         hanyadik++;
  113.         Console.WriteLine("Találatok száma: {0}", talalat);
  114.         Console.WriteLine();
  115.       } while (talalat != 3);
  116.       for(int i = 0; i< tippelemszam; i++)
  117.       {
  118.         Console.Write("{0}\t", tipp[i]);
  119.       }
  120.       Console.WriteLine();
  121.       Console.WriteLine("A(z) {0}. generálásra lett 3 találat.", hanyadik);
  122.       Console.ReadKey();
  123.     }
  124.   }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement