Categories
Visual C# Телерик

Задача: Tribonacci Triangle

Решение

using System;

namespace TribonacciTriangle {
  class Program {
    static void Main() {
      long firstNumber = long.Parse(Console.ReadLine());
      long secondNumber = long.Parse(Console.ReadLine());
      long thirdNumber = long.Parse(Console.ReadLine());
      int linesCount = int.Parse(Console.ReadLine());

      Console.WriteLine(firstNumber);
      Console.WriteLine(secondNumber + " " + thirdNumber);

      int membersCount = 0;
      for (int i = 1; i <= linesCount; i++) {
        membersCount += i;
      }
      int nextLine = 3;
      int lineSize = 0;
      for (int i = 4; i <= membersCount; i++) {
        long currentNumber = firstNumber + secondNumber + thirdNumber;
        firstNumber = secondNumber;
        secondNumber = thirdNumber;
        thirdNumber = currentNumber;
        Console.Write(currentNumber + " ");
        lineSize++;
        if (lineSize == nextLine) {
          Console.WriteLine();
          nextLine += 1;
          lineSize = 0;
        }
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Sheets

Решение

using System;
using System.Collections.Generic;

namespace Sheets {
  class Program {
    static void Main() {
      int n = int.Parse(Console.ReadLine());
      int[] piecesList = new int[11];
      Dictionary sheets = new Dictionary();
      sheets.Add(0, "A0");
      sheets.Add(1, "A1");
      sheets.Add(2, "A2");
      sheets.Add(3, "A3");
      sheets.Add(4, "A4");
      sheets.Add(5, "A5");
      sheets.Add(6, "A6");
      sheets.Add(7, "A7");
      sheets.Add(8, "A8");
      sheets.Add(9, "A9");
      sheets.Add(10, "A10");

      piecesList[10] = 1;
      for (int i = 9; i >= 0; i--) {
        piecesList[i] = piecesList[i + 1] * 2;
      }

      for (int i = 0; i < piecesList.Length; i++) {
        if (n >= piecesList[i]) {
          n -= piecesList[i];
          sheets.Remove(i);
        }
      }

      foreach(var item in sheets) {
        Console.WriteLine(item.Value);
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Next Day

Решение

using System;

namespace TelerikExamples {
  class FireInTheMatrix {
    static void Main() {
      int fireWidth = int.Parse(Console.ReadLine());
      int sideDots = (fireWidth - 2) / 2;
      int middleDots = 0;

      for (int i = sideDots; i >= 0; i--) {
        Console.Write(new string('.', i));
        Console.Write(new string('#', 1));
        Console.Write(new string('.', middleDots));
        Console.Write(new string('#', 1));
        Console.Write(new string('.', i));
        Console.WriteLine();

        middleDots += 2;
      }

      middleDots = fireWidth - 2;

      for (int i = 0; i < fireWidth / 4; i++) {
        Console.Write(new string('.', i));
        Console.Write(new string('#', 1));
        Console.Write(new string('.', middleDots));
        Console.Write(new string('#', 1));
        Console.Write(new string('.', i));
        Console.WriteLine();
        middleDots -= 2;
      }
      Console.WriteLine(new string('-', fireWidth));
      int middleSlashes = fireWidth / 2;
      for (int i = 0; i < fireWidth / 2; i++) {
        Console.Write(new string('.', i));
        Console.Write(new string('\\', middleSlashes));
        Console.Write(new string('/', middleSlashes));
        Console.Write(new string('.', i));
        Console.WriteLine();
        middleSlashes--;
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Formula Bit 1

Решение

using System;

namespace FormulaBit1 {
  class Program {
    static int gridSize = 8;
    static int[, ] grid = new int[gridSize, gridSize];
    static int trackLength = 0;
    static int turnsCount = 0;
    static char previousDirection = 'D';
    static bool finished = false;

    static void Main() {
      for (int i = 0; i < gridSize; i++) {
        int number = int.Parse(Console.ReadLine());
        for (int j = 0; j < gridSize; j++) {
          int mask = 1 << j;
          int numberAndMask = number & mask;
          int bit = numberAndMask >> j;
          grid[i, gridSize - j - 1] = (bit == 1) ? 1 : 0;
        }
      }

      buildTrack(0, 7, 'D');

      if (finished == false) {
        Console.WriteLine("No {0}", trackLength);
      }
    }

    static void checkForTrackExit(int row, int column) {
      if (row == 7 && column == 0) {
        finished = true;
        Console.WriteLine("{0} {1}", trackLength, turnsCount);
      }
    }

    static void buildTrack(int row, int column, char currentDirection) {
      if (row < 0 || column < 0 || row >= grid.GetLength(0) || column >= grid.GetLength(1)) {
        return;
      }

      if (grid[row, column] == 1) {
        return;
      }

      switch (currentDirection) {
      case 'D':
        for (int i = row; i < gridSize; i++) {
          if (grid[i, column] == 0) {
            trackLength += 1;
            checkForTrackExit(i, column);
            if (i == (gridSize - 1)) {
              turnsCount++;
              previousDirection = 'D';
              buildTrack(i, column - 1, 'L');
            }
            continue;
          } else {
            turnsCount++;
            previousDirection = 'D';
            buildTrack(i - 1, column - 1, 'L');
            break;
          }
        }
        break;
      case 'L':
        for (int i = column; i >= 0; i--) {
          if (grid[row, i] == 0) {
            trackLength += 1;
            checkForTrackExit(row, i);
            if (i == 0) {
              if (previousDirection == 'D') {
                turnsCount++;
                previousDirection = 'L';
                buildTrack(row - 1, i, 'U');
              } else {
                turnsCount++;
                previousDirection = 'L';
                buildTrack(row + 1, i, 'D');
              }
            }
            continue;
          } else {
            if (previousDirection == 'U') {
              turnsCount++;
              previousDirection = 'L';
              buildTrack(row + 1, i + 1, 'D');
              break;
            } else {
              turnsCount++;
              previousDirection = 'L';
              buildTrack(row - 1, i + 1, 'U');
              break;
            }
          }

        }
        break;
      case 'U':
        for (int i = row; i >= 0; i--) {
          if (grid[i, column] == 0) {
            trackLength += 1;
            checkForTrackExit(i, column);
            if (i == 0) {
              turnsCount++;
              previousDirection = 'U';
              buildTrack(i, column - 1, 'L');
            }
            continue;
          } else {
            turnsCount++;
            previousDirection = 'U';
            buildTrack(i + 1, column - 1, 'L');
            break;
          }
        }
        break;
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Carpets

Решение

using System;

namespace Carpets {
  class Program {
    static void Main() {
      int fullSize = int.Parse(Console.ReadLine());
      int halfSize = fullSize / 2;

      for (int i = 1; i <= halfSize; i++) {
        Console.Write(new string('.', halfSize - i));
        string str = new string(' ', 2 * i);
        char[] chr = str.ToCharArray();
        for (int j = 0; j < chr.Length / 2; j++) {
          if (j % 2 == 0) {
            chr[j] = '/';
            chr[chr.Length - j - 1] = '\\';
          }
        }
        str = new string(chr);
        Console.Write(str);
        Console.Write(new string('.', halfSize - i));
        Console.WriteLine();
      }
      for (int i = 1; i <= halfSize; i++) {
        Console.Write(new string('.', i - 1));
        string str = new string(' ', 2 * (halfSize + 1) - 2 * i);
        char[] chr = str.ToCharArray();
        for (int j = 0; j < chr.Length / 2; j++) {
          if (j % 2 == 0) {
            chr[j] = '\\';
            chr[chr.Length - j - 1] = '/';
          }
        }
        str = new string(chr);
        Console.Write(str);
        Console.Write(new string('.', i - 1));
        Console.WriteLine();
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Neuron

Решение

using System;
using System.Collections.Generic;

namespace NeuronMapping {
  class Program {
    static void Main() {
      List numbersList = new List();
      uint inputNumber = 0;
      while (true) {
        if (uint.TryParse(Console.ReadLine(), out inputNumber)) {
          numbersList.Add(inputNumber);
        } else {
          break;
        }
      }

      for (int i = 0; i < numbersList.Count; i++) {
        for (int j = 0; j < 32; j++) {
          uint mask = (uint) 1 << j;
          uint numberAndMask = numbersList[i] & mask;
          uint bit = numberAndMask >> j;
          if (bit == 1) {
            break;
          } else {
            numbersList[i] = numbersList[i] | mask;
          }
        }
      }

      for (int i = 0; i < numbersList.Count; i++) {
        for (int j = 31; j >= 0; j--) {
          uint mask = (uint) 1 << j;
          uint numberAndMask = numbersList[i] & mask;
          uint bit = numberAndMask >> j;
          if (bit == 1) {
            break;
          } else {
            numbersList[i] = numbersList[i] | mask;
          }
        }
      }

      for (int i = 0; i < numbersList.Count; i++) {
        for (int j = 0; j < 32; j++) {
          uint mask = (uint) 1 << j;
          numbersList[i] = mask ^ numbersList[i];
        }
        Console.WriteLine(numbersList[i]);
      }
    }
  }
}
Categories
Visual C# Телерик

Задача: Pillars

Решение

using System;

namespace Pillars {
  class Program {
    static void Main() {
      int length = 8;
      int[] numbers = new int[8];
      int[] sum = new int[8];

      for (int i = 0; i < length; i++) {
        numbers[i] = int.Parse(Console.ReadLine());
      }
      for (int i = 0; i < length; i++) {
        int mask = 1 << i;
        for (int j = 0; j < length; j++) {
          int numberAndMask = numbers[j] & mask;
          int bit = numberAndMask >> i;
          if (bit == 1) {
            sum[i] += 1;
          }
        }
      }

      int leftMostColumn = -1;
      int fullCellsCount = 0;
      for (int i = 0; i < length; i++) {
        int rightSum = 0;
        for (int j = 0; j < i; j++) {
          rightSum += sum[j];
        }
        int leftSum = 0;
        for (int k = i + 1; k < length; k++) {
          leftSum += sum[k];
        }
        if (leftSum == rightSum) {
          leftMostColumn = i;
          fullCellsCount = leftSum;
        }
      }
      if (leftMostColumn >= 0) {
        Console.WriteLine(leftMostColumn);
        Console.WriteLine(fullCellsCount);
      } else {
        Console.WriteLine("No");
      }
    }
  }
}
Categories
Други

GitHub

https://github.com/Nanich87

Categories
PHP

Survey Format Conversion Factory

Фабриката за преобразуване на файлове с геодезически измервания се премести в GitHub!

https://github.com/Nanich87/Survey-Format-Conversion-Factory

Categories
PHP

Конвертиране от DINI21 към TXT или XML

Преобразуване на файлове от дигитален нивелир DINI21 към XML или TXT файл

Базов код на PHP:

<?php
$submit_button = "Преобразуване";
$upload_dir = "./upload";
$output_file = "";

if (
    isset($_POST["submit"], $_POST["format"]) &&
    $_POST["submit"] === $submit_button
) {
    if (is_uploaded_file($_FILES["file"]["tmp_name"])) {
        $old_filename = htmlentities(
            $_FILES["file"]["name"],
            ENT_QUOTES,
            "UTF-8"
        );
        $new_filename = sprintf(
            "%s_%s.txt",
            md5($_FILES["file"]["name"]),
            time()
        );
        if (
            move_uploaded_file(
                $_FILES["file"]["tmp_name"],
                $upload_dir . DIRECTORY_SEPARATOR . $new_filename
            )
        ) {
            $input_file = file_get_contents(
                $upload_dir . DIRECTORY_SEPARATOR . $new_filename
            );
            unlink($upload_dir . DIRECTORY_SEPARATOR . $new_filename);
            $file_formats = [
                "TXT" => "txt",
                "XML" => "xml",
            ];
            $file_format = isset($file_formats[$_POST["format"]])
                ? $file_formats[$_POST["format"]]
                : $file_formats["TXT"];
            if (
                preg_match_all(
                    "/(\d+\s+Start-Line\s+BF\s+\d+)(.*?)(\d+\s+End-Line\s+\d+)/ism",
                    $input_file,
                    $matches
                )
            ) {
                if ($file_format == "xml") {
                    $output_file = "";
                    $output_file .= "";
                    $output_file .= "1";
                    $output_file .= "";
                    $output_file .= "";
                }
                foreach ($matches[0] as $line) {
                    $line = explode("\n", $line);
                    $back_benchmark = null;
                    $forward_benchmark = null;
                    $elevation = null;
                    $length = null;
                    for ($i = 1; $i < count($line) - 3; $i++) {
                        if ($i % 3 == 0) {
                            $rb = explode(
                                " ",
                                preg_replace(
                                    "/\s\s+/",
                                    " ",
                                    trim($line[$i - 1])
                                )
                            );
                            $rf = explode(
                                " ",
                                preg_replace("/\s\s+/", " ", trim($line[$i]))
                            );
                            if ($rb[1] != "X" && $rf[1] == "X") {
                                $back_benchmark = $rb[1];
                                $elevation += $rb[5] - $rf[5];
                                $length += $rb[7] + $rf[7];
                                continue;
                            } elseif ($rb[1] == "X" && $rf[1] == "X") {
                                $elevation += $rb[5] - $rf[5];
                                $length += $rb[7] + $rf[7];
                                continue;
                            } elseif ($rb[1] == "X" && $rf[1] != "X") {
                                $forward_benchmark = $rf[1];
                                $elevation += $rb[5] - $rf[5];
                                $length += $rb[7] + $rf[7];
                            } else {
                                $back_benchmark = $rb[1];
                                $forward_benchmark = $rf[1];
                                $elevation = $rb[5] - $rf[5];
                                $length = $rb[7] + $rf[7];
                            }
                            if ($file_format == "xml") {
                                $output_file .= "";
                                $output_file .= "" . $back_benchmark . "";
                                $output_file .= "" . $forward_benchmark . "";
                                $output_file .= "" . $elevation . "";
                                $output_file .= "" . $length . "";
                                $output_file .= "0";
                                $output_file .= "";
                                $output_file .= "\n";
                            } else {
                                $output_file .= sprintf(
                                    "%s %s %1.4f %1.2f 0\n",
                                    $back_benchmark,
                                    $forward_benchmark,
                                    $elevation,
                                    $length
                                );
                            }
                            $elevation = 0;
                            $length = 0;
                        }
                    }
                }
                if ($file_format == "xml") {
                    $output_file .= "";
                    $output_file .= "";
                }
            }
        } else {
            echo "Възникнала е грешка при качването на Файла " .
                $old_filename .
                "";
        }
    } else {
        echo "Няма избран файл за качване!";
    }
}