C# dasturlash tilidagi yechimlari    (Mono, dotnet7)

A. Hello Hi!

using System.IO;
FileInfo f = new FileInfo("shunchaki.txt");
if(f.Exists)
{
  Console.WriteLine("Hi!");
 f.Delete();
}
else
{
  Console.WriteLine("Hello");
  FileStream s = f.Create();
  s.Close();
}

B.  Homework

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;

namespace homework
{
	class Program
	{
		public static string[] Words(string s)
		{
			string[] res = s.Split().Where(x => (x != "")).OrderBy(x => x).Select(x => x).ToArray();
			HashSet<string> hs = new HashSet<string>(res);
			return hs.ToArray();
		}
		public static string[] Cleaned(string[] b, string [] s)
		{
			List<string> sl = new List<string>();
				
			foreach(string i in s)
			{
				if(!b.Contains(i))
				{
					sl.Add(i);
				}
			}
			return sl.ToArray();
		}
		public static int WriteToBaza(string[] s)
		{
			FileInfo f = new FileInfo("bazaz.txt");
			StreamWriter sw = f.AppendText();
			foreach(var i in s)
			{
				sw.WriteLine(i);
			}
			sw.Close();
			string[] ds = File.ReadAllLines("bazaz.txt");
			return ds.Length;
		}
		public static void Main(string[] args)
		{
	
			string s = File.ReadAllText("input.txt");
	
			FileInfo f = new FileInfo("bazaz.txt");
			if(!f.Exists)
			{
				StreamWriter w =  f.CreateText();
				w.WriteLine(DateTime.Now.ToString("hh:mm:ss"));
				w.Close();
			}
			string[] baza = File.ReadAllLines("bazaz.txt");
			string[] m = Words(s);
			string[] mv = Cleaned(baza,m);
			int count = WriteToBaza(mv);
			foreach(var i in mv)
			{

				Console.WriteLine(i);
			}
			Console.WriteLine(count - 1);
 
		}
	}
}

C.  Parol

using System;
using System.IO;
class Program{
  static void Main(){
				FileInfo f = new FileInfo("file1");
                StreamWriter sw,swout;
                StreamReader sr,srin;
                srin = new StreamReader("input.txt");
                swout = new StreamWriter("output.txt");
                string s, res;
                int k;
                if (!f.Exists)
                {
                    s = srin.ReadLine(); 
                    k = int.Parse(srin.ReadLine());
                    sw = f.CreateText();
                    sw.WriteLine(s);
                    sw.WriteLine(k);
                    sw.Close();
                    swout.WriteLine("Parol yangilandi!");
                    
                }
                else
                {
                    s = srin.ReadLine();
                    sr = new StreamReader("file1");
                    res = sr.ReadLine();
                    k = int.Parse(sr.ReadLine());
                    sr.Close();
                    if (res == s)
                    {
                      
                        swout.WriteLine("Xush kelibsiz");
                        f.Delete();
                    }
                    else if (k == 1)
                    {
                       
                        swout.WriteLine("Juda ko'p muvaffaqiyatsiz urinish qildingiz, 5 daqiqa kuting");
                        f.Delete();
                    }
                    else
                    {
                      
                        swout.WriteLine("Parol xato, Qayta urunib ko'ring");
                        f.Delete();
                        sw = f.CreateText();
                        sw.WriteLine(res);
                        sw.WriteLine(k - 1);
                        sw.Close();
                    }
                   
                }
             
                srin.Close();
                swout.Close();
  }
  }

D.  Spell

using System;
using System.IO;

namespace itree
{
	class Program
	{
		public static void Main(string[] args)
		{
			string[] tovush = { "ey","bi","si","di","i","ef","ji","eych","ay","jey","key","el","em","en","ov","pi","kyu","ar","es","ti","yu","vi", "dablyu", "iks", "vay","zed"};
			FileInfo fsatr = new FileInfo("satr1.txt");
			string satr = Console.ReadLine();
			FileInfo countr = new FileInfo("countr1.txt");
			StreamWriter swcount;
			StreamReader srcount;
			StreamWriter swsatr;
			StreamReader srsatr;
			int k;
			if (!(countr.Exists))
			{
			swcount = countr.CreateText();
			swcount.WriteLine("0");
			swcount.Close();
			}
			srcount = new StreamReader("countr1.txt");
			k = int.Parse(srcount.ReadLine());
			if (k == 0)
			{
			    swsatr = fsatr.CreateText();
				swsatr.WriteLine(satr);
				swsatr.Close();
			}
			srsatr = new StreamReader("satr1.txt");
			string sssss = srsatr.ReadLine();
			
			Console.WriteLine(sssss[k] + " - " + tovush[sssss[k] - 97]);
			k++;
			srcount.Close();
			srsatr.Close();
			 using (FileStream stream = new FileStream("countr1.txt", FileMode.Truncate))
            {
                // Clear the contents of the file
                stream.SetLength(0);
            }
			swcount = countr.AppendText();
			swcount.Write(k);
			swcount.Close();
			
			if ( sssss.Length == k )
			{
				fsatr.Delete();
				countr.Delete();
			}

		}
	}
}

E.  Noodatiy dastur #2

using System;
using System.IO;
class Program{
 
static void update(string name,FileInfo f, int k, out StreamWriter swcount)
		{
			using (FileStream stream = new FileStream(name, FileMode.Truncate))
            {
                // Clear the contents of the file
                stream.SetLength(0);
            }
			swcount = f.AppendText();
			swcount.Write(k);
			swcount.Close();
		}
static void Main(){
FileInfo f1 = new FileInfo("salom.txt");
			FileInfo f2 = new FileInfo("alik.txt");
			FileInfo count = new FileInfo("countr.txt");
			FileInfo count1 = new FileInfo("countr1.txt");
			FileInfo count2 = new FileInfo("countr2.txt");
			StreamReader sr,sr1,sr2; 
			StreamWriter sw,sw1,sw2;
			if (!count.Exists)
			{
				sw = count.CreateText();
				sw.Write("1");
				sw.Close();
				
				sw1 = count1.CreateText();
				sw1.Write("1");
				sw1.Close();
				
				sw2 = count2.CreateText();
				sw2.Write("1");
				sw2.Close();
			}
			sr = new StreamReader("countr.txt");
			sr1 = new StreamReader("countr1.txt");
			sr2 = new StreamReader("countr2.txt");
			if(f1.Exists)
			{
				Console.WriteLine("Hasan");
				int l = int.Parse(sr.ReadLine());
				double x = Math.Sqrt(2*l +0.25) - 0.5;
				if(  x == Math.Truncate(x))
				 {
					f1.Delete();
					FileStream fs2 = f2.Create();
					fs2.Close();
				 }
				sr.Close();
				l++;
				update("countr.txt",count,l,out sw);
				
			}
			else if(f2.Exists)
			{
				Console.WriteLine("Husan");
				int l1 = int.Parse(sr1.ReadLine());
				double x1 = Math.Sqrt(2*l1 +0.25) - 0.5;
				if(x1 == Math.Truncate(x1))
				{
					f2.Delete();
				}
				sr1.Close();
				l1++;
				update(count1.Name,count1,l1,out sw1);
			}
			else
			{
				Console.WriteLine("Qulmamat");
				int l2 = int.Parse(sr2.ReadLine());
				double x2 = Math.Sqrt(2*l2 +0.25) - 0.5;
				if (x2 == Math.Truncate(x2))
				{
				FileStream fs1 = f1.Create();
				fs1.Close();
				}
				sr2.Close();
				l2++;
				update(count2.Name,count2,l2,out sw2);
			}
}
}

F. Baxtingdan ko'r yoki "Qo'rqinchli" biletlar

using System;
using System.IO;
using static System.Console;
namespace biletlar
{
    class Program
    {
        static void Main(string[] args)
        {
                FileInfo f = new FileInfo("baza.txt");
                StreamWriter sw, swin, swout;
                string s = ReadLine();
                if (!f.Exists)
                {
                    sw = f.CreateText();
                    sw.WriteLine(s);
                    sw.Close();
                }
                else
                {
                    sw = f.AppendText();
                    sw.WriteLine(s);
                    sw.Close();
                }

                string[] ss = File.ReadAllLines("baza.txt");
                int n = ss.Length;
                for (int i = 1; i <= n; i++)
                {
                    if (n % i == 0)
                    {
                        WriteLine(ss[i - 1]);
                    }
                   
                }
                

            }
        }
    
}

 

C++ daturlash tilidagi yechimlar.

A. Hello, Hi!

#include <bits/stdc++.h>
using namespace std;

int main()
{
	string name = "borrr.txt";
	ifstream f(name);
	if(f.good())
	{
		cout << "Hi!";
		remove(name.c_str());
	}
	else
	{
		cout <<"Hello";
		ofstream fout(name);
		fout << "endi bor" << endl;
		fout.close();
	}
	f.close();
	
}

B. Homework

#include <bits/stdc++.h>
using namespace std;


vector <string> words;
bool isThere(string s)
{
  for(string i : words)
  {
    if(i == s) return false;
  }
  return true;
}
int Sana(string ph)
{
  string h;
  int c = 0;
  ifstream fin(ph);
  while(fin >> h)
  {
    c++;
  }
  fin.close();
  return c;
}
bool isInBaza(string ph,string s)
{
  ifstream fin(ph);
  string g;
  while(fin >> g)
  {
    if(g == s){
      fin.close();
      return false;
    }
  }
  fin.close();
  return true;
}
int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  string ph = "baza.txt";
  if(!ifstream(ph))
  {
    ofstream fout(ph);
    fout << "!!!" << endl;
    fout.close();
  }
  
  string w;
  while(cin >> w)
  {
    ofstream fout(ph,ios::app);
    if(isThere(w) and isInBaza(ph,w)) 
    {
      words.push_back(w);
      fout << w << endl;
    }
    fout.close();
  }
  sort(words.begin(),words.end());
  for(string i : words)
  {
    cout << i << endl;
  }
  int c = Sana(ph);
  cout << c - 1 << endl;
  
}

C. Parol

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream srin("input.txt");
    ofstream swout("output.txt");
    string s, res;
    int k;

    if (!ifstream("file1")) {
        getline(srin, s);
        srin >> k;
        ofstream sw("file1");
        sw << s << endl;
        sw << k << endl;
        sw.close();
        swout << "Parol yangilandi!" << endl;
    } else {
        getline(srin, s);
        ifstream sr("file1");
        getline(sr, res);
        sr >> k;
        sr.close();
        if (res == s) {
            swout << "Xush kelibsiz" << endl;
            remove("file1");
        } else if (k == 1) {
            swout << "Juda ko'p muvaffaqiyatsiz urinish qildingiz, 5 daqiqa kuting" << endl;
            remove("file1");
        } else {
            swout << "Parol xato, Qayta urunib ko'ring" << endl;
            remove("file1");
            ofstream sw("file1");
            sw << res << endl;
            sw << k - 1 << endl;
            sw.close();
        }
    }

    srin.close();
    swout.close();

    return 0;
}

D. Spell

#include <bits/stdc++.h>

using namespace std;

int main()
{
	string t[] = { "ey","bi","si","di","i","ef","ji","eych","ay","jey","key","el","em","en","ov","pi","kyu","ar","es","ti","yu","vi", "dablyu", "iks", "vay","zed"};

	string fname = "matn.txt", fcount = "count.txt";
    string satr,str;
    cin >> satr;
    ifstream fin(fname);
    if(!fin.good())
    {
  
    	ofstream wr(fname);
    	wr << satr << endl;
        wr.close();
        ofstream count(fcount);
        count << "0";
        count.close();
	}
	fin.close();
	int k;
	 ifstream read(fcount);
	 read >> k;
	 read.close();
	 ifstream f(fname);
	 f >> str;
	 f.close();
	 cout << str[k] << " - " << t[str[k] - 97] << '\n';
	 k++;
	 ofstream write(fcount);
	 write << k << endl;
	 write.close();
	 if (k == str.size()) 
	 {
	 	remove(fname.c_str());
	 }
	 
	}

E. Noodatiy dastur #2

#include <iostream>
#include <fstream>
#include <cmath>

void update(const std::string& name, std::ofstream& swcount, int k) {
    std::ofstream stream(name, std::ofstream::trunc);
    swcount.open(name, std::ofstream::app);
    swcount << k;
    swcount.close();
}

int main() {
    std::ofstream sw, sw1, sw2;
    std::ifstream sr, sr1, sr2;
    std::string count = "countr.txt";
    std::string count1 = "countr1.txt";
    std::string count2 = "countr2.txt";
    std::string f1 = "hello.txt";
    std::string f2 = "alik.txt";

    if (!std::ifstream(count)) {
        sw.open(count); sw << "1"; sw.close();
        sw1.open(count1); sw1 << "1"; sw1.close();
        sw2.open(count2); sw2 << "1"; sw2.close();
    }

    sr.open(count);
    sr1.open(count1);
    sr2.open(count2);

    if (std::ifstream(f1)) {
        std::cout << "Hasan\n";
        int l; sr >> l;
        double x = std::sqrt(2 * l + 0.25) - 0.5;
        if (x == std::trunc(x)) {
            std::remove(f1.c_str());
            std::ofstream fs2(f2); fs2.close();
        }
        sr.close();
        l++;
        std::ofstream sw;
        update(count, sw, l);
    }
    else if (std::ifstream(f2)) {
        std::cout << "Husan\n";
        int l1; sr1 >> l1;
        double x1 = std::sqrt(2 * l1 + 0.25) - 0.5;
        if (x1 == std::trunc(x1)) {
            std::remove(f2.c_str());
        }
        sr1.close();
        l1++;
        std::ofstream sw1;
        update(count1, sw1, l1);
    }
    else {
        std::cout << "Qulmamat\n";
        int l2; sr2 >> l2;
        double x2 = std::sqrt(2 * l2 + 0.25) - 0.5;
        if (x2 == std::trunc(x2)) {
            std::ofstream fs1(f1); fs1.close();
        }
        sr2.close();
        l2++;
        std::ofstream sw2;
        update(count2, sw2, l2);
    }

    return 0;
}

F. Baxtingdan ko'r yoki “Qo'rqinchli” biletlar

#include <bits/stdc++.h>
using namespace std;

int main()
{
	string fname = "biletqw.txt", fcount = "sssana.txt";
    string satr = "wewr",str = "weew";
    getline(cin,satr);
   
    ofstream file(fname, ios::app);
	if(!file.is_open())
	{
		ofstream creat(fname);
		creat << satr << endl;
		creat.close();
	}
	else
	{
		file << satr << endl;
		file.close(); 
	}
	int k = 1;
	ifstream sana(fcount);
	if(sana >> k) k++;
	sana.close();
	ofstream wrt(fcount);
	wrt << k;
	wrt.close();
	ifstream read(fname);
	string word;
	for(int i = 0; i < k; i++)
	{
		read >> word;
		if (k %(i+1) == 0)
			cout << word << endl;
	}
}

 

Python dasturlash tilidagi yechimlar.    (Python3.12,  PyPy3.10)

A. Hello, Hi!

import os

filename = "myFile.txt"

if os.path.exists(filename):
    print("Hi!")
    os.remove(filename)
else:
    print("Hello")
    f = open(filename, "w")
    f.close()

B. Homework

import os
def AddWords(s,baza):
  ss = sorted(set(s))
  
  for i in ss:
    if i in baza: continue
    else : 
      a = open('baza.txt','a')
      a.write( i + '\n')
      a.close()
      print(i)

name = "baza.txt"
f = open("input.txt", "r")
s = f.read().split()
f.close()
if( not os.path.exists(name)):
  baza = open(name,'w')
  baza.write('!@#$\n')
  baza.close()
b = open(name,'r')
bb = b.read().split()
b.close()
AddWords(s,bb)
b = open(name,'r')
bb = b.read().split()
b.close()
print(len(bb) - 1)

C. Parol

import os
def main():
    res = ""
    k = 0

    if not os.path.exists("file1.txt"):
        s = input()
        k = int(input())
        with open("file1.txt", "w") as sw:
            sw.write(s + "\n")
            sw.write(str(k) + "\n")
        print("Parol yangilandi!")
    else:
        s = input()
        with open("file1.txt", "r") as sr:
            res = sr.readline().strip()
            k = int(sr.readline().strip())
        if res == s:
            print("Xush kelibsiz")
            os.remove("file1.txt")
        elif k == 1:
            print("Juda ko'p muvaffaqiyatsiz urinish qildingiz, 5 daqiqa kuting")
            os.remove("file1.txt")
        else:
            print("Parol xato, Qayta urunib ko'ring")
            os.remove("file1.txt")
            with open("file1.txt", "w") as sw:
                sw.write(res + "\n")
                sw.write(str(k - 1) + "\n")
if __name__ == '__main__':
    main()

D. Spell

import os
t = ["ey","bi","si","di","i","ef","ji","eych","ay","jey","key","el","em","en","ov","pi","kyu","ar","es","ti","yu","vi", "dablyu", "iks", "vay","zed"]
s = input()
n =  'matn.txt'
c = 'count.txt'
if(not os.path.exists(n)):
    fmatn = open(n,'w')
    fmatn.write(s)
    fmatn.close()
    fcount = open(c,'w')
    fcount.write('0')
    fcount.close()
fcread = open(c)
k = int(fcread.read())
fcread.close()
fmread = open(n)
m = fmread.read()
fmread.close()
print(m[k],'-',t[ord(m[k]) - 97])
k+=1
fcwr = open(c,'w')
fcwr.write(str(k))
fcwr.close()
if(len(m) == k) : os.remove(n)

E. Noodatiy dastur #2

import math
import os


def update(name, f, k):
    with open(name, 'w') as file:
        file.truncate(0)

    with open(name, 'a') as file:
        file.write(str(k))


def main():
    f1 = "hello.txt"
    f2 = "alik.txt"
    count = "countr.txt"
    count1 = "countr1.txt"
    count2 = "countr2.txt"

    if not os.path.exists(count):
        with open(count, 'w') as file:
            file.write("1")

    if not os.path.exists(count1):
        with open(count1, 'w') as file:
            file.write("1")

    if not os.path.exists(count2):
        with open(count2, 'w') as file:
            file.write("1")

    with open("countr.txt", 'r') as sr:
        with open("countr1.txt", 'r') as sr1:
            with open("countr2.txt", 'r') as sr2:
                if os.path.exists(f1):
                    print("Hasan")
                    l = int(sr.readline())
                    x = math.sqrt(2 * l + 0.25) - 0.5
                    if x == math.trunc(x):
                        os.remove(f1)
                        open(f2, 'w').close()

                    l += 1
                    update("countr.txt", count, l)

                elif os.path.exists(f2):
                    print("Husan")
                    l1 = int(sr1.readline())
                    x1 = math.sqrt(2 * l1 + 0.25) - 0.5
                    if x1 == math.trunc(x1):
                        os.remove(f2)

                    l1 += 1
                    update("countr1.txt", count1, l1)

                else:
                    print("Qulmamat")
                    l2 = int(sr2.readline())
                    x2 = math.sqrt(2 * l2 + 0.25) - 0.5
                    if x2 == math.trunc(x2):
                        open(f1, 'w').close()

                    l2 += 1
                    update("countr2.txt", count2, l2)


if __name__ == "__main__":
    main()

F. Baxtingdan ko'r yoki “Qo'rqinchli” biletlar.

import os
s = input()
name = "wor.txt"
if not os.path.exists(name):
    f = open(name,'w')
    f.write("begin ")
    f.close()
add = open(name,'a')
add.write(s + " ")
add.close()
fread = open(name)
s = fread.readline()
fread.close()
a = s.split()
k = len(a) - 1
for i in range(1,k+1):
    if k % i == 0 : print(a[i])

 

Agar sizda bu yechimlardan optimalroq yechimlar mavjud  bo'lsa, Robocontest.uz telegram kanalidagi Shu contest uchun qo'yilgan post kommentariyasiga jo'nating