Trushnikov.Ivan
BAN USERusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication49
{
class Program
{
static void Main(string[] args)
{
// input file, for example:
// 4
// 1 1 0 0;
// 0 0 1 0;
// 0 0 0 1;
// 1 1 0 0.
// first line - it is number of nodes.
string[] input_data = File.ReadAllText(@"G:\input.txt").Split(new char[] {' ','\r','\n','\t'},StringSplitOptions.RemoveEmptyEntries);
int j = 0;
int i = 0;
int n = int.Parse(input_data[0]);
int[,] matrix = new int[n,n];
// get matrix with graph
for (int k = 1; k < input_data.Length; k++)
{
if (input_data[k] == ";" || input_data[k] == ".")
{
i = 0;
j++;
}
else
{
matrix[j,i] = int.Parse(input_data[k]);
i++;
}
}
// count number of cycle
var num_cycle = GetNumberCycle(matrix);
Console.WriteLine(num_cycle);
Console.ReadKey();
}
static int GetNumberCycle(int[,] matrix)
{
int cycle = 0;
int n = (int)Math.Sqrt(matrix.Length);
int[] d = new int[n]; // it is array of nodes which was visited
for (int j = 0; j < n; j++)
{
d[j] = 1;
for (int i = 0; i < n; i++)
{
if (matrix[j, i] == 1)
{
if(d[i] == 1) cycle++;
else d[i] = 1;
}
}
}
return cycle;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace reverse180
{
class Program
{
static void Main(string[] args)
{
// File contains any number. For example: 916
string[] input_data = File.ReadAllText(@"G:\input.txt").Split(new char[] { '\r'}, StringSplitOptions.RemoveEmptyEntries);
// Checking whether a number is strange :)
var is_strange_number = Turn180(int.Parse(input_data[0]));
Console.WriteLine(is_strange_number);
Console.ReadKey();
}
static bool Turn180(int strange_number)
{
int x=0; // it is result of replace and reverse
int x0 = strange_number; // store input number
while (strange_number > 0) // in each step strange_number is decremented in 10 times
{
x = x * 10;
var temp = strange_number % 10; // Get the youngest position of number
if (temp != 0)
{
if (temp == 6) temp = 9; // reverse
else if (temp == 9) temp = 6; // reverse
else if (temp != 1 && temp != 8 && temp != 0) return false; // if number contains un-reverseable item, breake method.
x =x + temp;
strange_number = strange_number / 10;
}
else {
strange_number = strange_number / 10;
}
}
if (x != x0) return false;
return true;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication47
{
class Program
{
static void Main(string[] args)
{
//Input file:
// name begin end
// Bob1 1 3
// Bob2 2 4
// Bob3 1 4
// Bob1 5 6
// Bob2 4 7
// Bob3 5 6
string[] input_data = File.ReadAllText(@"G:\input.txt").Split(new char[] {'\n','\r','\t' }, StringSplitOptions.RemoveEmptyEntries);
Employee[] calendar = new Employee[input_data.Length*2];
int j = 0;
// convert input file to the list. for each line in file, we make two items: for begin and for end
for (int i = 0; i < input_data.Length; i++)
{
string[] employee = input_data[i].Split(new char[] {' ', '\r'}, StringSplitOptions.RemoveEmptyEntries);
Employee man = new Employee(employee[0], int.Parse(employee[1]), true); // begin
calendar[j] = man;
j++;
man = new Employee(employee[0],int.Parse(employee[2]), false); // end
calendar[j] = man;
j++;
}
// quicksorting
calendar = SortingCalendar(calendar, 0, calendar.Length-1);
int count = 0;
int t_min=0;
// finding when all employees are not work
for (int i = 0; i < calendar.Length; i++)
{
if (calendar[i].is_begin) count++;
else count--;
if (count == 0)
{
t_min = calendar[i + 1].time - calendar[i].time; // this line needs to be checked for i+1 < calendar.length. But i suppose, that shedule is looped
break;
}
}
Console.WriteLine(t_min);
Console.ReadKey();
}
static Employee[] SortingCalendar(Employee[] calendar, int begin, int end)
{
int i = begin;
int j = end;
int m = (begin + end) / 2;
int median = calendar[m].time;
while (i <= j)
{
while (calendar[i].time < median)
{
i++;
}
while (calendar[j].time > median)
{
j--;
}
if (i <= j)
{
// This check is necessary for the situation where the beginning of the next cycle coincides with the end of the previous
if (calendar[i].time != calendar[j].time)
{
Employee temp = calendar[i];
calendar[i] = calendar[j];
calendar[j] = temp;
}
i++;
j--;
}
}
if (j > begin) calendar = SortingCalendar(calendar, begin, j);
if (i < end) calendar = SortingCalendar(calendar, i, end);
return calendar;
}
}
class Employee
{
public string name;
public int time;
public bool is_begin;
public Employee()
{ }
public Employee(string in_name, int in_time, bool in_is_begin)
{
name = in_name;
time = in_time;
is_begin = in_is_begin;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication46
{
class Program
{
static void Main(string[] args)
{
// get hand
string[] input_data = File.ReadAllText(@"G:\input.txt").Split(new char[] { ' ','\n','\r','\t'},StringSplitOptions.RemoveEmptyEntries);
Dictionary<int, int> result = new Dictionary<int, int>();
// count the same cards
for (int i = 0; i<input_data.Length;i++)
{
var key = int.Parse(input_data[i]);
if (result.ContainsKey(key))
{
result[key]++;
}
else
{
result.Add(key,1);
}
}
// begin from the most expensive combination
if (result.ContainsValue(4))
{
Console.WriteLine("four of a kind");
}
else if (result.ContainsValue(3) && result.ContainsValue(2))
{
Console.WriteLine("full house");
}
else if (result.ContainsValue(3))
{
Console.WriteLine("three of a kind");
}
else if(result.ContainsValue(2))
{
int count_pair=0;
foreach (KeyValuePair<int, int> item in result)
{
if (item.Value == 2)
{
count_pair++;
}
}
if (count_pair == 2)
{
Console.WriteLine("dable pair");
}
else
{
Console.WriteLine("pair");
}
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication45
{
class Program
{
static void Main(string[] args)
{
// data for creating tree
// This file contains a string of numbers separated by spaces
string[] input_data = File.ReadAllText(@"G:\input.txt").Split(new char[] { ' ','\n','\r','\t'}, StringSplitOptions.RemoveEmptyEntries);
if (input_data.Length == 0)
{
return;
}
Tree node = new Tree(); // creating of root
bool is_head = true;
// creating of tree
for (int i =0; i< input_data.Length; i++)
{
// this case is needed to fill the root
if (is_head)
{
node.data = int.Parse(input_data[i]);
is_head = false;
}
else
{
node = GrowingTree(node, int.Parse(input_data[i]));
}
}
// Now we have a tree for test
// Get node for test
int test_counter =2;
while (node != null && test_counter > 0)
{
node = node.right;
test_counter--;
}
node = GetLeftMost(node);
}
static Tree GetLeftMost(Tree item)
{
int level = 0;
if (item == null)
{
return null;
}
// Get root
while (item.parent !=null)
{
item = item.parent;
level++;
}
// Get left most
while (level > 0)
{
// Left sub tree is not null
if (item.left != null)
{
item = item.left;
level--;
}
// Get right sub tree
else {
item = item.right;
level--;
}
}
return item;
}
static Tree GrowingTree(Tree node, int data)
{
if (node.data > data)
{
if (node.left != null)
{
GrowingTree(node.left, data);
}
else
{
node.left = new Tree(node, data);
}
}
else
{
if (node.right != null)
{
GrowingTree(node.right, data);
}
else
{
node.right = new Tree(node, data);
}
}
return node;
}
}
class Tree
{
public Tree parent;
public Tree left;
public Tree right;
public int data;
// Constructors
public Tree()
{ }
public Tree(Tree in_parent, int in_data)
{
data = in_data;
parent = in_parent;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace palindrome_string
{
class Program
{
static void Main(string[] args)
{
SinglyLinkedList list = new SinglyLinkedList("aba", new SinglyLinkedList("cd"));
var item = list.next;
item.next = new SinglyLinkedList("efe");
item = item.next;
item.next = new SinglyLinkedList("d");
item = item.next;
item.next = new SinglyLinkedList("caba");
var length = GetListLength(list);
var is_palindrom = ProcessList(list, length);
}
static bool ProcessList(SinglyLinkedList backward, int length)
{
bool is_palindrom = false;
if (backward == null)
{
return false;
}
SinglyLinkedList forward = backward.next;
backward.next = null;
int median = length / 2;
int i = 0;
SinglyLinkedList temp;
bool is_odd = false;
// median is difrent for even and odd
if (length % 2 == 0)
{
is_odd = false;
median = length / 2 -1;
}
else
{
is_odd = true;
median = length / 2;
}
// Get two singly linked list with forward link, and with reverse link
while (i < median)
{
temp = backward;
backward = forward;
forward = forward.next;
backward.next = temp;
i++;
}
// In this method backward and forward will compare
is_palindrom = CheckPalindrom(backward, forward, is_odd);
return is_palindrom;
}
static bool CheckPalindrom(SinglyLinkedList back, SinglyLinkedList forw, bool is_odd)
{
string data_back = back.data;
string data_forw = forw.data;
int i = data_back.Length - 1;
int j = 0;
// If quantity of item in input data is odd, we have backward longer than forward.
if (is_odd)
{
data_forw = data_back;
for (int k = 0; k< data_back.Length;k++)
{
int p = data_back.Length - k - 1;
if (data_back[k] != data_forw[p])
{
return false;
}
}
back = back.next;
data_back = back.data;
data_forw = forw.data;
i = data_back.Length - 1;
}
// Compare backward and forward
while (back != null && forw !=null)
{
while (i>=0 && j < data_forw.Length)
{
if (data_back[i] != data_forw[j])
{
return false;
}
else
{
i--;
j++;
}
}
// If current item of back sequence is ended, get next
if (i < 0 && back.next !=null)
{
back = back.next;
if (back != null)
{
data_back = back.data;
i = data_back.Length - 1;
}
}
// If current item of forward sequence is ended, get next
if (j >= data_forw.Length )
{
forw = forw.next;
if (forw != null)
{
data_forw = forw.data;
j = 0;
}
}
}
return true;
}
static int GetListLength(SinglyLinkedList list)
{
int length = 0;
while (list != null)
{
length++;
list = list.next;
}
return length;
}
}
class SinglyLinkedList
{
public string data;
public SinglyLinkedList next;
// constructors
public SinglyLinkedList(string input_data, SinglyLinkedList input_next)
{
data = input_data;
next = input_next;
}
public SinglyLinkedList(string input_data)
{
data = input_data;
}
public SinglyLinkedList()
{
}
}
}
Is it a good practice, when end of array is processed by "try...catch"?
- Trushnikov.Ivan December 12, 2015using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication44
{
class Program
{
static void Main(string[] args)
{
string[] input_data = File.ReadAllText(@"G:\input.txt").Split(new char[] { ' ','\r'},StringSplitOptions.RemoveEmptyEntries);
Stack<int> stack = new Stack<int>();
int begin = 0;
int end = 0;
for (int i = 0; i < input_data.Length; i++)
{
int item = int.Parse(input_data[i]);
if (item < 0 )
{
end = i-1;
Reverse(input_data, begin, end, stack);
begin = i + 1;
}
else
{
stack.Push(item);
}
if (i == input_data.Length - 1 && stack.Count>0)
{
end = i;
Reverse(input_data,begin,end,stack);
}
}
for (int i = 0; i < input_data.Length; i++)
{
Console.Write(input_data[i]);
}
}
static void Reverse(string[] arr, int begin, int end, Stack<int> stack)
{
for (int i = begin; i<= end;i++)
{
arr[i] = stack.Pop().ToString();
}
stack.Clear();
}
}
}
May be, this problems about "find X in 2d array using method binary search".
- Trushnikov.Ivan December 12, 2015using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Collections;
//C#
namespace ConsoleApplication16
{
class Program
{
static void Main(string[] args)
{
int Index = 0;
int IndexBegin = 0;
int[] InputData = new int[] { -1,100, 200, -2, 1, 2, 3, 4, -6, -5, 10, 20, -2,0 };
ArrayList OutputData = new ArrayList();
foreach (int item in InputData)
{
if (item < 0)
{
for (int i =Index-1; i>=IndexBegin;i--)
{
OutputData.Add(InputData[i]);
}
OutputData.Add(InputData[Index]);
IndexBegin = Index+1;
}
Index++;
}
if(IndexBegin != Index + 1)
for (int i = Index - 1; i >= IndexBegin; i--)
{
OutputData.Add(InputData[i]);
}
foreach (int item in OutputData) Console.Write(item);
Console.ReadKey();
}
}
}
RepNitaniyaGrown, Animator at ASAPInfosystemsPvtLtd
I am a talented video editor with an eye for compelling stories and 3 years of experience. I committed to ...
Searching neighbors by DFS
- Trushnikov.Ivan December 23, 2015