mohamadreza.shakouri
BAN USERusing System;
public class Program
{
public static void Main()
{
int n= 10 ;
int m = 100;
Console.WriteLine(DisplaySteppingNumber(n ,m));
}
public static int DisplaySteppingNumber(int n ,int m)
{
int count = 0;
for (int i = 1 ; i <= 9 ; i++)
{
findSteppingNum( n , m , i , ref count);
}
return count;
}
public static void findSteppingNum (int n , int m , int stepnum , ref int count)
{
if ( stepnum > m || stepnum == 0 )
{
return ;
}
if (stepnum > n )
{
count ++;
}
int lastDigit = stepnum % 10;
// There can be 2 cases either digit
// to be appended is lastDigit + 1 or
// lastDigit - 1
int stepnumA = stepnum*10 + (lastDigit-1);
int stepnumB = stepnum*10 + (lastDigit+1);
// If lastDigit is 0 then only possible
// digit after 0 can be 1 for a Stepping
// Number
if (lastDigit == 0)
findSteppingNum(n, m, stepnumB , ref count);
// If lastDigit is 9 then only possible
// digit after 9 can be 8 for a Stepping
// Number
else if(lastDigit == 9)
findSteppingNum(n, m, stepnumA , ref count);
else
{
findSteppingNum(n, m, stepnumA , ref count);
findSteppingNum(n, m, stepnumB , ref count);
}
}
}
Pretty much the same algorithm as previous answer but with c# not python , I am sure this can be done with less memory complexity.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
int num_of_disks = 3;
Stack<int> Src = new Stack<int>();
Stack<int> Aux = new Stack<int>();
Stack<int> Dest = new Stack<int>();
for (int i = num_of_disks; i >= 1; i--)
Src.Push(i);
Stack<int> Result = HanoiProblem (Src , Aux , Dest);
while (Result.Count > 0)
{
Console.WriteLine(Result.Pop());
}
}
public static Stack<int> HanoiProblem (Stack<int> Src , Stack<int> Aux , Stack<int> Dest)
{
int number_of_moves = (int) Math.Pow(2 , Src.Count) -1;
int number_of_disck = Src.Count;
for (int i = 1 ; i <= number_of_moves ; i++ )
{
if (i%3 == 1)
{
MakeLegalMove(Src , Dest);
Console.WriteLine("move Source and Dest");
}
else if (i % 3 == 2)
{
MakeLegalMove(Src , Aux);
Console.WriteLine("move Source and Aux");
}
else
{
MakeLegalMove(Aux , Dest);
Console.WriteLine("move Aux and Dest");
}
}
if (number_of_disck%2 == 0)
{
return Aux;
}
else
{
return Dest;
}
}
public static void MakeLegalMove (Stack<int> A , Stack<int> B)
{
if (A.Count == 0 )
{
A.Push(B.Pop());
}
else if (B.Count == 0 || A.Peek() < B.Peek())
{
B.Push(A.Pop());
}
else if (A.Peek() > B.Peek())
{
A.Push(B.Pop());
}
else
{
B.Push(A.Pop());
}
}
}
you probably can find tons of answers everywhere. They key is to use binary search to find the index of that smallest element and also make sure to ask the elements are unique.
using System;
public class Program
{
public static void Main()
{
int[] testArray = {7, 9, 11, 12, 15};
Console.WriteLine(BinarySearch(testArray , 0 , testArray.Length-1));
}
public static int BinarySearch (int[] rotatedArray , int min , int max)
{
if (min > max)
{
return 0;
}
else if (max == min)
{
return min;
}
else
{
int mid = (min+max)/2;
if (mid-1 > 0)
{
if (rotatedArray[mid-1] > rotatedArray[mid])
{
return mid;
}
}
if (mid+1 < max)
{
if (rotatedArray[mid] > rotatedArray[mid+1] )
{
return mid+1;
}
}
if (rotatedArray[mid] < rotatedArray[max])
return BinarySearch (rotatedArray , min , mid-1);
else
return BinarySearch (rotatedArray , mid+1 , max);
}
}
}
using System;
public class Program
{
public static void Main()
{
Node n1 = new Node(9);
Node n2 = new Node(9);
Node n3 = new Node(9);
n1.next = n2;
n2.next = n3;
Node n4 = new Node(1);
Node n5 = new Node(2);
Node n6 = new Node(3);
n4.next = n5;
n5.next = n6;
Node nr = MultiPly (n1 , n4);
Node cursor = nr;
while(cursor != null)
{
Console.Write(cursor.data);
cursor = cursor.next;
}
}
public static Node MultiPly (Node head1 , Node head2)
{
Node number1 = Reverse(head1);
Node number2 = Reverse(head2);
int ret;
int carryout = 0;
Node result = new Node(0);
Node reultHead = result;
Node resultCur = result;
while(number2 != null)
{
Node marker = number1;
result = resultCur;
carryout = 0;
while(marker != null)
{
//Console.WriteLine(carryout);
ret = (marker.data * number2.data);
result.data = ret + result.data + carryout;
if (result.data > 9)
{
carryout = result.data/10;
result.data = result.data%10;
}
if ( result.next == null)
{
Node resultnext = new Node(0);
result.next = resultnext;
}
//Console.WriteLine(marker.data + " * " + number2.data + "=" + result.data ) ;
result = result.next;
marker = marker.next;
}
if (carryout > 0)
{
result.data = carryout;
}
number2 = number2.next;
resultCur = resultCur.next;
}
return Reverse(reultHead);
}
public static Node Reverse (Node head)
{
Node cur = head;
Node prev = null;
Node next = null;
while (cur != null)
{
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
public class Node{
public Node prev;
public Node next;
public int data ;
public Node (int d)
{
this.data = d;
}
}
}
RepIn 2008 I was licensing race cars in Los Angeles, CA. What gets me going now is promoting husband vashikaran ...
using System;
- mohamadreza.shakouri April 11, 2017public class Program
{
public static void Main()
{
Node n1 = new Node(1);
Node n2 = new Node(7);
//Node n3 = new Node(1);
//n1.next = n2;
//n2.next = n3;
Node n4 = new Node(8);
Node n5 = new Node(7);
Node n6 = new Node(0);
n4.next = n5;
n5.next = n6;
Node cursor = Addition(n1 , n4);
while(cursor != null)
{
Console.Write(cursor.data);
cursor = cursor.next;
}
}
public static Node Addition (Node number1 , Node number2)
{
Node num1 = Reverse(number1);
Node num2 = Reverse(number2);
Node Head = new Node(0);
Node result = Head;
int temp = 0;
int carry = 0 ;
while (num1 != null || num2 !=null)
{
if (num1 == null)
{
num1 = new Node(0);
}
if (num2 == null)
{
num2 = new Node(0);
}
temp = num1.data + num2.data + carry;
carry = 0;
if (temp > 9)
{
result.data = temp % 10 ;
carry = temp / 10 ;
}
else
{
result.data = temp;
}
if (result.next == null)
{
result.next = new Node(0);
}
result = result.next;
num1 = num1.next;
num2 = num2.next;
}
if (carry > 0)
{
result.data = carry ;
}
return Reverse(Head);
}
public static Node Reverse (Node head)
{
Node cur = head;
Node prev = null;
Node next = null;
while (cur != null)
{
next = cur.next;
cur.next = prev;
prev = cur;
cur = next;
}
return prev;
}
public class Node{
public Node prev;
public Node next;
public int data ;
public Node (int d)
{
this.data = d;
}
}
}