* Given a Binary Search Tree, Find the distance between 2 nodes
* Count zeros in a row wise and column wise sorted matrix
* so hoan hao (perfect number)
* in so va chuoi theo chieu nguoc lai
=================================================
=================================================
A Palindrome is a word, phrase or sequence which reads the same in both directions.
/// <summary>
/// Find string is Palindrome.
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static bool IsPalindrome(string value)
{
int min = 0;
int max = value.Length - 1;
while (true)
{
if (min > max)
{
return true;
}
char a = value[min];
char b = value[max];
// Scan forward for a while invalid.
while (!char.IsLetterOrDigit(a))
{
min++;
if (min > max)
{
return true;
}
a = value[min];
}
// Scan backward for b while invalid.
while (!char.IsLetterOrDigit(b))
{
max--;
if (min > max)
{
return true;
}
b = value[max];
}
if (char.ToLower(a) != char.ToLower(b))
{
return false;
}
min++;
max--;
}
}
class Program
{
/// <summary>
/// Find Fibonacci number.
/// </summary>
/// <param name="n">(int) number</param>
/// <returns>(int) Fibonacci number<</returns>
public static int Fibonacci(int n)
{
int a = 0;
int b = 1;
// In N steps compute Fibonacci sequence iteratively.
for (int i = 0; i < n; i++)
{
int temp = a;
a = b;
b = temp + b;
}
return a;
}
static void Main(string[] args)
{
for (int i = 0; i < 15; i++)
{
Console.WriteLine(Fibonacci(i) );
}
Console.Read();
}
}
****************************** ******************************
Fibonacci recursive
public class Program
{
public static void Main(string[] args)
{
int number = 11;
for(int counter=0;counter<number; counter++)
Console.WriteLine(" \n" + Fibonacci(counter) );
}
public static int Fibonacci(int number)
{
if (number == 0)
return 0;
else if(number ==1)
return 1;
else
{
return Fibonacci(number - 2) + Fibonacci(number - 1);
}
}
/// <summary>
/// Find number is even or odd.
/// </summary>
/// <param name="x">(int) number</param>
/// <returns>(int) return 0 if number is even else 1.</returns>
public static int EvenOddNumber(int x)
{
//bitwise operation
if ((x & 1) == 0)
return 0;//even number
else
return 1;//odd number
}
/// <summary>
/// Find the Nth common element form an array.
/// </summary>
/// <param name="items">items array</param>
/// <param name="p">Nth element</param>
/// <returns>element</returns>
public static int GetNthCommonItem(int[] items, int p)
{
Dictionary<int, int> ranks = new Dictionary<int, int>();
foreach (int no in items)
{
if (ranks.ContainsKey(no))
{
ranks[no] += 1;
}
else
{
ranks[no] = 1;
}
}
//store keys in decending order
int[] sorted = ranks.Keys.OrderByDescending(x => ranks[x]).ToArray();
if (p <= sorted.Length)
{
return sorted[p - 1];
}
else
{
return -1;
}
}
Nhập số 12345
In ra số 54321
using System;
namespace VietJackCsharp
{
class TestCsharp
{
public static void Main()
{
int num, r, sum = 0, t;
Console.Write("\n");
Console.Write("In so theo chieu dao nguoc trong C#:\n");
Console.Write("--------------- --------------------");
Console.Write("\n\n");
Console.Write("Nhap mot so bat ky: ");
num = Convert.ToInt32(Console. ReadLine());
for (t = num; t != 0; t = t / 10)
{
r = t % 10;
sum = sum * 10 + r;
}
Console.Write("So theo chieu dao nguoc cua so da cho la: {0} \n", sum);
Console.ReadKey();
}
}
}
****************************** ************
in chuoi dao nguoc
Nhập chuỗi: VietJack
In chuỗi theo chiều đảo ngược: kcaJteiV
using System;
namespace VietJackCsharp
{
class TestCsharp
{
public static void Main()
{
string str, str1 = "";
int i, l;
Console.Write("\n");
Console.Write("In chuoi theo chieu dao nguoc trong C#:\n");
Console.Write("--------------- ----------------------");
Console.Write("\n\n");
Console.Write("Nhap mot chuoi: ");
str = Console.ReadLine();
l = str.Length - 1;
for (i = l; i >= 0; i--)
{
str1 = str1 + str[i];
}
Console.WriteLine("Chuoi dao nguoc cua chuoi ban dau la: {0}", str1);
Console.Write("\n");
Console.ReadKey();
}
}
}
ve tam giac so nguoc
using System;
namespace VietJackCsharp
{
class TestCsharp
{
public static void Main()
{
Console.Write("Nhap mot so bat ky: ");
int num = Convert.ToInt32(Console. ReadLine());
Console.Write("Nhap do rong cua tam giac: ");
int width = Convert.ToInt32(Console. ReadLine());
int height = width;
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
Console.Write(num);
}
Console.WriteLine();
width--;
}
Console.ReadKey();
}
}
}
****************************** *********************
ve tam giac so
using System;
namespace VietJackCsharp
{
class TestCsharp
{
public static void Main()
{
int i, j, so_hang;
Console.Write("\n");
Console.Write("Ve tam giac sao trong C#:\n");
Console.Write("--------------- ----------");
Console.Write("\n\n");
Console.Write("Nhap so hang: ");
so_hang = Convert.ToInt32(Console. ReadLine());
for (i = 1; i <= so_hang; i++)
{
for (j = 1; j <= i; j++)
Console.Write("*");
Console.Write("\n");
}
Console.ReadKey();
}
}
}
****************************** ***********************
tam giac so dang
1
12
123
1234
12345
using System;
namespace VietJackCsharp
{
class TestCsharp
{
public static void Main()
{
int i, j, so_hang;
Console.Write("\n");
Console.Write("Ve tam giac so trong C#:\n");
Console.Write("--------------- -----------");
Console.Write("\n\n");
Console.Write("Nhap so hang: ");
so_hang = Convert.ToInt32(Console. ReadLine());
for (i = 1; i <= so_hang; i++)
{
for (j = 1; j <= i; j++)
Console.Write("{0}", j);
Console.Write("\n");
}
Console.ReadKey();
}
}
}
****************************** ****************************** ***
tam giac so dang
1
2 3
4 5 6
7 8 9 10
using System;
namespace VietJackCsharp
{
class TestCsharp
{
public static void Main()
{
int i, j, bien_dem, so_hang, k, t = 1;
Console.Write("\n");
Console.Write("Ve tam giac so can trong C# - cac so trong tam giac co thu tu tang dan:\n");
Console.Write("--------------- ------------------------------ --------------------------");
Console.Write("\n\n");
Console.Write("Nhap so hang: ");
so_hang = Convert.ToInt32(Console. ReadLine());
bien_dem = so_hang + 4 - 1;
for (i = 1; i <= so_hang; i++)
{
for (k = bien_dem; k >= 1; k--)
{
Console.Write(" ");
}
for (j = 1; j <= i; j++)
Console.Write("{0} ", t++);
Console.Write("\n");
bien_dem--;
}
Console.ReadKey();
}
}
}
****************************** ****************
tam giac so dang
1
121
12321
1234321
1234543221
using System;
namespace VietJackCsharp
{
class TestCsharp
{
public static void Main()
{
int i, j, n;
Console.Write("\n");
Console.Write("Ve tam giac so trong C#:\n");
Console.Write("--------------- --------");
Console.Write("\n\n");
Console.Write("Nhap so hang: ");
n = Convert.ToInt32(Console. ReadLine());
for (i = 0; i <= n; i++)
{
/* vong lap nay de in khoang trang */
for (j = 1; j <= n - i; j++)
Console.Write(" ");
/* Hien thi cac so theo thu tu tang dan tu dau hang cho den giua hang*/
for (j = 1; j <= i; j++)
Console.Write("{0}", j);
/* Hien thi so theo thu tu giam dan tu giua hang cho den cuoi hang */
for (j = i - 1; j >= 1; j--)
Console.Write("{0}", j);
Console.Write("\n");
}
Console.ReadKey();
}
}
}
/// <summary>
/// Reverse string words.
/// </summary>
/// <param name="input">(string)(i.e. Hello hai)</param>
/// <returns>(string)(i.e. hai Hello)</returns>
public static string ReverseStringWords(string input)
{
if (input.Length <= 0) return "";//return empty string.
//first complement of string length.
int len = input.Length >> 1;
//Reverse string output characters array
char[] output = new char[input.Length];
for (int i = 0; i < len; i++)
{
output[i] = input[input.Length - i - 1];
output[input.Length - i - 1] = input[i];
}
ReverseStringWords(output);
return new string(output);
}
private static void ReverseStringWords(char[] c)
{
int wordStart = 0;
for (int i = 0; i < c.Length; i++)
{
//check space between words.
if (c[i] == ' ' ||c[i]=='\0')
{
//swap words
ReverseCharacters(c, wordStart, i - 1);
wordStart = i + 1;
}
else if (i == c.Length - 1)
{
ReverseCharacters(c, wordStart, i);
}
}
}
private static void ReverseCharacters(char[] c, int i, int j)
{
if (i >= j)
return;
for (int k = i; k <= (i + j) / 2; k++)
{
char temp = c[k];
c[k] = c[i + j - k];
c[i + j - k] = temp;
}
}
/// <summary>
/// Reverse string
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static string ReverseString(string input)
{
if (input.Length <= 0) return "";//return empty string.
//first complement of string length.
int len = input.Length >> 1;
//output characters array
char[] output = new char[input.Length];
for (int i = 0; i < len; i++)
{
output[i] = input[input.Length - i - 1];
output[input.Length - i - 1] = input[i];
}
return new string(output);
}
/// <summary>
/// Reverse an Array elements
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
public static int[] ReverseArray(int[] arr)
{
if (arr.Length <= 0) return new int[] { };//return empty array.
//first complement of array length.
int len = arr.Length >> 1;
//temporary variable.
int temp;
for (int i = 0; i < len; i++)
{
temp = arr[i];
arr[i] = arr[arr.Length - i - 1];
arr[arr.Length - i - 1] = temp;
}
return arr;
}
/// <summary>
/// Find minimum an array element value.
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
public static int MIN(int[] arr)
{
if (arr.Length <= 0) return 0;
int len = arr.Length >> 1;
//temporary variables.
int min = arr[0];
for (int i = 0; i <= len; i++)
{
if (arr[i] < min)
min = arr[i];
if (arr[arr.Length - i - 1] < min)
min = arr[arr.Length - i - 1];
}
return min;
}
****************************** ****************************** *********
/// <summary>
/// Find maximum an array element value.
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
public static int MAX(int[] arr)
{
if (arr.Length <= 0) return 0;
int len = arr.Length >> 1;
//temporary variables.
int max = 0;
for (int i = 0; i <= len; i++)
{
if (arr[i] > max)
max = arr[i];
if (arr[arr.Length - i - 1] > max)
max = arr[arr.Length - i - 1];
}
return max;
}
****************************** ****************************** ************
/// <summary>
/// Find average an array elements
/// </summary>
/// <param name="arr"></param>
/// <returns></returns>
public static int AVG(int[] arr)
{
if (arr.Length <= 0) return 0;
int len = arr.Length >> 1;
//temporary variables.
int sum = 0;
int rem = 0;
for (int i = 0; i <= len; i++)
{
sum += arr[i] / arr.Length;
rem += arr[i] % arr.Length;
if (i == len)
break;
sum += arr[arr.Length - i - 1] / arr.Length;
rem += arr[arr.Length - i - 1] % arr.Length;
}
return sum + rem / arr.Length;
}
User interface contains two types of user input controls: TextInput, which accepts all characters and NumericInput,
which accepts only digits.
Implement the class TextInput that contains:
Public method void Add(char c) - adds the given character to the current value
Public method string GetValue() - returns the current value
Implement the class NumericInput that:
Inherits TextInput
Overrides the Add method so that each non-numeric character is ignored
public class TextInput
{
public IList<char> list = new List<char>();
public virtual void Add(char c)
{
list.Add(c);
}
public string GetValue()
{
string r = "";
foreach (char l in list)
{
r = r + l;
}
return r;
}
}
public class NumericInput : TextInput
{
public override void Add(char c)
{
if (c < '0' || c > '9') { }
else
list.Add(c);
}
}
public class UserInput
{
public static void Main(string[] args)
{
TextInput input = new NumericInput();
input.Add('1');
input.Add('a');
input.Add('0');
Console.WriteLine(input. GetValue());
}
}
public class ComputePi1
{
public static void main(String[] args)
{
int i;
int nThrows = 0;
int nSuccess = 0;
double x, y;
for (i = 0; i < 1000000 ; i++)
{
x = Math.random(); // Throw a dart
y = Math.random();
nThrows++;
if ( x*x + y*y <= 1 )
nSuccess++;
}
System.WriteLine("Pi/4 = " + (double)nSuccess/(double) nThrows );
System.WriteLine("Pi = " + 4*(double)nSuccess/(double) nThrows );
}
}
using System;
class Program {
static double MonteCarloPi(int n) {
int inside = 0;
Random r = new Random();
for (int i = 0; i < n; i++) {
if (Math.Pow(r.NextDouble(), 2)+ Math.Pow(r.NextDouble(), 2) <= 1) {
inside++;
}
}
return 4.0 * inside / n;
}
static void Main(string[] args) {
int value = 1000;
for (int n = 0; n < 5; n++) {
value *= 10;
Console.WriteLine("{0}:{1}", value.ToString("#,###"). PadLeft(11, ' '), MonteCarloPi(value));
}
}
}
int minint = array[0];
int maxint = array[0];
foreach (int value in array) {
if (value < minint) minint = value;
if (value > maxint) maxint = value;
}
bool IsDigitsOnly(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
public static double Factorial(int number)
{
if (number == 0)
return 1;
double factorial = 1;
for (int i = number; i >= 1;i-- )
{
factorial = factorial * i;
}
return factorial;
}
public static int Tinh(string input)
{
return input.Length == 0 ? 0 : input.GroupBy(c => c).Max(group => group.Count());
}
hoac
var maxCount = 0;
foreach (var c in input)
{
var count = 0;
foreach (var d in input)
{
if (c == d)
{
count++;
}
}
maxCount = Math.Max(count, maxCount);
}
return maxCount;
public static List<int> Tinh(int input)
{
var res = new List<List<int>>();
int x,y,z;
z = (int)input/10;
var input1 = input % 10;
y = (int)input1/5;
var input2 = input1 % 5;
x = (int) input2/2;
var i = new List<int>();i.Add(x) ;i.Add(y);i.Add(z);res.Add(i);
var minc = res.FirstOrDefault().Sum();
foreach (var k in res)
{
if(k.Sum() < minc) minc = k.Sum();
}
var cccc = res.FirstOrDefault(x=>x.Sum() == minc);
return cccc;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text. RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
float[] input = new float[7]{1,2,3,4,5,6,7};
var c = Average(input,3);
//Your code goes here
foreach (var t in c){
if(t>0)
Console.WriteLine(t);
}
Console.ReadLine();
}
public static float[] Average(float[] input, int w)
{
float[] result = new float[7];
if(input.Length == 0) return result;
for(int i = 0; i <=input.Length - w; i++ )
{
float sum = 0;
float ave = 0;
for(int j= 0; j < w; j++)
{
sum += input[i+j];
ave = sum / w;
result[i]= ave;
}
}
return result;
}
}
}
Cho biết ‘()’ , ‘[ ]' là chuỗi chuẩn. A, B là chuỗi chuẩn thì (A) và [A] và AB là chuỗi chuẩn.
Vd : ‘(()[ ]([ ]))’ là chuẩn , ‘( ( ] )’ là ko chuẩn.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
class Answer
{
/// Checks that the given string is correct
static public bool Check(string str)
{
if(str == null || str =="")
{
return true;
}
var res = IsCorrect(str);
return res;
}
public static bool IsCorrect (string input)
{
input = input.Replace("[]","");
input = input.Replace("()","");
var count = input.Where(x=>x == '(').Count();
var count2 = input.Where(x=>x == '[').Count();
while(count > 0 || count2 > 0)
{
input = input.Replace("[]","");
input = input.Replace("()","");
input = input.Replace("[]","");
input = input.Replace("()","");
count = count -1;
count2 = count2 -1;
}
if(string.IsNullOrEmpty(input)) return true;
return false;
}
}
1 người đứng trên trục số nguyên … -4 -3 -2 -1 0 1 2 3 4…. Ban đầu ở vị trí 0
step 1: bước s1 = 1 bước => vị trí 1.
step 2 : bước s2 = -2 bước => vị trí -1
step 3 : bước s3 = (-2) - (1) = -3 => vị trí -4
step n : số bước s = số bước ở step n-1 trừ số bước ở step (n-2) , sn = s(n-1) -s(n-2)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text. RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
int number = 5;
Console.WriteLine("step:" + Step(number) );
Console.WriteLine("pos:" + Pos(number) );
}
public static int Step(int number)
{
if (number == 0)
return 0;
else if(number ==1)
return 1;
else if (number == 2)
return -2;
else
{
return Step(number - 1) - Step(number - 2);
}
}
public static int Pos(int number)
{
if (number == 0)
return 0;
else if(number ==1)
return 1;
else if (number == 2)
return -1;
else
{
return Step(number) + Pos(number - 1);
}
}
}
}
//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text. RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
int n, i, j;
n = 9;
int[,] a = new int[n + 1, n+1 ];
a[0, 0] = 1;
for (i = 1; i < n + 1; i++)
{
for (j = 0; j < i ; j++)
{
if (j == 0 || j==i+1)
{
a[i, j] = 1;
}
if (j != 0)
{
a[i, j] = a[i - 1, j] + a[i - 1, j - 1];
}
}
a[i, j] = 1;
}
Console.WriteLine();
for (i = 0; i < n + 1; i++)
{
for (j = 0; j < n + 1; j++)
{
if (a[i, j] != 0)
{
Console.Write(a[i, j] + " ");
}
}
Console.WriteLine();
}
}
}
}
using
System;
class
GFG
{
public
static
int
N = 5;
// Function to count number of
// 0s in the given row-wise and
// column-wise sorted binary matrix.
static
int
countZeroes(
int
[,] mat)
{
// start from bottom-left
// corner of the matrix
int
row = N - 1, col = 0;
// stores number of zeroes
// in the matrix
int
count = 0;
while
(col < N)
{
// move up until you find a 0
while
(mat[row,col] > 0)
// if zero is not found in
// current column,
// we are done
if
(--row < 0)
return
count;
// add 0s present in current
// column to result
count += (row + 1);
// move right to next column
col++;
}
return
count;
}
// Driver Code
public
static
void
Main ()
{
int
[,] mat = { { 0, 0, 0, 0, 1 },
{ 0, 0, 0, 1, 1 },
{ 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1 } };
Console.WriteLine( countZeroes(mat));
}
}
public static bool IsBalanced(Node node)
{
var mm = new DepthMinMax();
mm.Min = int.MaxValue;
mm.Max = int.MinValue;
FindMinMax(mm, node, 0);
return (mm.Max - mm.Min <= 1) ? true : false;
}
private static void FindMinMax(DepthMinMax mm, Node node, int depth)
{
if (node == null) return;
FindMinMax(mm, node.L, depth + 1);
FindMinMax(mm, node.R, depth + 1);
// At a terminating node
if (node.L == null || node.R == null)
{
if (mm.Min > depth) mm.Min = depth;
if (mm.Max < depth) mm.Max = depth;
}
}
public class Node
{
public Node L { get; set; }
public Node R { get; set; }
}
public class DepthMinMax
{
public int Min { get; set; }
public int Max { get; set; }
}
Given a Binary Search Tree, Find the distance between 2 nodes
class MyTreeNode
{
public int Data { get; set; }
public MyTreeNode Left { get; set; }
public MyTreeNode Right { get; set; }
public MyTreeNode(int data)
{
this.Data = data;
}
}
class QTwoNodeDis
{
public static int Distance(MyTreeNode root, MyTreeNode node1, MyTreeNode node2)
{
var node = FindLCA(root, node1, node2);
int distLCA = FindLevel(root, node);
int dist1 = FindLevel(root, node1);
int dist2 = FindLevel(root, node2);
return dist1 + dist2 - 2 * distLCA;
}
private static MyTreeNode FindLCA(MyTreeNode root, MyTreeNode node1, MyTreeNode node2)
{
if (root == null) return null;
if (root.Data == node1.Data || root.Data== node2.Data)
return root;
MyTreeNode left_lca = FindLCA(root.Left, node1, node2);
MyTreeNode right_lca = FindLCA(root.Right, node1, node2);
if (left_lca != null && right_lca != null)
return root;
return left_lca != null ? left_lca : right_lca;
}
private static int FindLevel(MyTreeNode root, MyTreeNode node)
{
if (root == null)
return -1;
if(root.Data == node.Data)
return 0;
int level = FindLevel(root.Left, node);
if (level == -1)
level = FindLevel(root.Right, node);
if(level != -1)
return level + 1;
return -1;
}
}
FV Lot de 100 Carte Francaise constitué de 50 VMAX + 50 Carte V. Nos Cartes sont Rare, idéal pour Les Jeu et Les Cadeaux.630 Pochettes pour Cartes Pokemon Double Face, 35 Pages, en Polychlorure de vinyle (PVC), Vierges Trading Card SleevesPokèmon - 50 Cartes Assorties [Jouet]Lot de 50 Cartes Pokemon Ne Se Répéteront Pas + 3 Cartes Brillantes Cadeau - French Version
Aucun commentaire:
Enregistrer un commentaire