Advertisement
Levi0227

Sodoku-2021okt_erettsegi

Apr 19th, 2023 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7.  
  8. namespace ConsoleApp8
  9. {
  10.     class Program
  11.     {   static char [,] tabla= new char[10,10]; //0.ban nincsenek adatok
  12.         static void Main(string[] args)
  13.         {
  14.             Console.WriteLine("1. feladat");
  15.             Console.Write("\nAdja meg a bemeneti fájl nevét! ");
  16.             string fajl_nev=Console.ReadLine();
  17.             Console.Write("\nAdja meg egy sor számát! ");
  18.             int be_sor=int.Parse(Console.ReadLine());
  19.             Console.Write("\nAdja meg egy oszlop számát! ");
  20.             int be_oszlop=int.Parse(Console.ReadLine());
  21.  
  22.  
  23.             StreamReader sr=new StreamReader(fajl_nev);
  24.             for(int i=1;i<=9;i++)
  25.             {
  26.                 string [] egysor=sr.ReadLine().Split(" ");
  27.                 for (int j = 1; j <=9; j++)
  28.                 {
  29.                     tabla[i,j]=egysor[j-1][0];
  30.                 }
  31.                
  32.              }
  33.             //sr.Close();
  34.  
  35.             //for (int i = 1; i <=9; i++)
  36.             //{
  37.             //    for (int j = 1; j <=9; j++)
  38.             //    {
  39.             //      Console.Write("{0}\t", tabla[i,j]);
  40.             //    }
  41.             //    Console.WriteLine();
  42.             //}
  43.             Console.WriteLine("\n3. feladat");
  44.             if (tabla[be_sor, be_oszlop]==0)
  45.             { Console.WriteLine("\nAz adott helyet még nem töltötték ki!"); }
  46.             else {
  47.             Console.Write("Az adott helyen szereplő szám: {0}", tabla[be_sor, be_oszlop]);
  48.             }
  49.             Console.WriteLine("\nA hely a(z) {0}. résztáblához tartozik!", 3*(be_sor)/3+(be_oszlop)/3+1);
  50.            
  51.            
  52.             Console.WriteLine("\n4.Feladat");
  53.            int db4=0;
  54.             for (int i = 0; i <= 9; i++)
  55.             {
  56.                 for (int j = 0; j <= 9; j++)
  57.                 {
  58.                     if (tabla[i,j]=='0') //karakter ne maradjon le a''
  59.                     {
  60.                         db4++;
  61.                         }
  62.                 }
  63.  
  64.             }
  65.             Console.WriteLine("Az üres helyek aránya: {0}% ", (((double)db4/81)*100).ToString("F1"));
  66.  
  67.             Console.WriteLine("\n 5.Feladat");
  68.  
  69.             while(!sr.EndOfStream)
  70.             {
  71.                 string[] egysor=sr.ReadLine().Split(" ");
  72.                 char szam=egysor[0][0];//feladat leirás alapján első a szám
  73.                 int sor= int.Parse(egysor[1]);//második a sor
  74.                 int oszlop = int.Parse(egysor[2]);// harmadik az oszlop
  75.                 Console.WriteLine("\nA kiválasztott sor: {0} oszlop: {1}. A szám:{2}", sor, oszlop, szam);
  76.                
  77.                 if (tabla[sor, oszlop]!='0')
  78.                 { Console.WriteLine("A helyet már kitöltötték!"); }
  79.                 else if (VanSorban(sor, szam))
  80.                     Console.WriteLine("Az adott sorban már szerepel a szám.");
  81.                 else if (VanOszlopban(oszlop, szam))
  82.                     Console.WriteLine("Az adott oszlopban már szerepel a szám.");
  83.                 else if (VanSzektorban(((sor - 1) / 3 * 3 + (oszlop - 1) / 3 + 1), szam))
  84.                     { Console.WriteLine("A résztáblázatban már szerepel a szám."); }
  85.                 else
  86.                     Console.WriteLine("A lépés megléphető");
  87.             }
  88.             sr.Close();
  89.  
  90.         }
  91.         private static bool VanOszlopban(int oszlop, char szam)
  92.         {
  93.             int ez = 1;
  94.             while (ez <= 9 && tabla[ez, oszlop] != szam) ez++;
  95.             return ez <= 9;
  96.         }
  97.  
  98.         private static bool VanSorban(int sor, char szam)
  99.         {
  100.             int ez = 1;
  101.             while (ez <= 9 && tabla[sor, ez] != szam) ez++;
  102.             return ez <= 9;
  103.         }
  104.         private static bool VanSzektorban(int szektor, char szam)
  105.         {
  106.             int ti = (szektor - 1) / 3 * 3 + 1;
  107.             int tj = (szektor - 1) % 3 * 3 + 1;
  108.             int i = 0;
  109.             int j = 0;
  110.             while (i < 3 && tabla[ti + i, tj + j] != szam)
  111.             {
  112.                
  113.                 j++;
  114.                 if (j == 3)
  115.                 {
  116.                     j = 0;
  117.                     i++;
  118.                    
  119.                 }
  120.             }
  121.             return i < 3;
  122.         }
  123.  
  124.  
  125.  
  126.     }
  127.  
  128. }
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement