Trova la risposta con aiuto Algoritmo dell'albero binario:
using System;
namespace MinimumStack
{
public class Node
{
public static long[] denominations = { 1L, 5L, 10L, 25L, 50, 100L, 500L, 1000L, 5000L, 10000L, 25000L, 50000L, 100000L, 500000L, 1000000L, 5000000L, 10000000L, 250000000L, 500000000L, 100000000L, 500000000L, 1000000000L };
public long Data { get; set; }
public long Denom { get; set; }
public long Denom1 { get; set; }
public long Denom2 { get; set; }
public int denomChip1 { get; set; }
public int denomChip2 { get; set; }
public long ChipCount { get; set; }
public Node Left { get; set; }
public Node Right { get; set; }
public Node()
{
}
public Node(long data,int index,long chipCount1)
{
this.Data = data;
this.ChipCount = chipCount1;
if(index > 0)
{
this.Denom = denominations[index+1];
this.Denom1 = denominations[index];
if(data >= denominations[index] && data >0)
{
long chipcount1 = data / denominations[index];
long newdata = data - denominations[index] * chipcount1;
//if(newdata>0)
Left = new Node(newdata, index - 1, chipcount1);
}
}
if( index-1 > 0)
{
this.Denom2 = denominations[index - 1];
if(data>= denominations[index-1] && data > 0)
{
long chipcount1 = data / denominations[index-1];
Console.WriteLine(data+ " data ======== " +denominations[index-1]+" = "+chipcount1);
long newdata = data - denominations[index-1] * chipcount1;
//if(newdata>0)
Right = new Node(newdata, index - 2, chipcount1);
}
}
if(index == 0)
{
//this.ChipCount = data;
this.Denom = denominations[index+1];
this.Denom1 = denominations[index];
Left = new Node(0, index - 1, data);
}
if(index-1 == 0)
{
//this.ChipCount = data;
this.Denom2 = denominations[index-1];
Right = new Node(0, index - 2, data);
}
//Console.WriteLine("===========================*************=====================================");
// Console.WriteLine("===============================******************=================================");
}
/*
private void InsertRec(Node root, Node newNode)
{
if (root == null)
root = newNode;
if (newNode.Data < root.Data)
{
if (root.Left == null)
root.Left = newNode;
else
InsertRec(root.Left, newNode);
}
else
{
if (root.Right == null)
root.Right = newNode;
else
InsertRec(root.Right, newNode);
}
}
*/
}
public class Program
{
private Node _root;
public static void Main(string[] args)
{//Your code goes here
Program p = new Program();
long amount = 14;//15L;
long[] str1 = { 1L, 5L, 10L, 25L, 50L, 100L, 500L, 1000L, 5000L, 10000L, 25000L, 50000L, 100000L, 500000L, 1000000L, 5000000L, 10000000L, 250000000L, 500000000L, 100000000L, 500000000L, 1000000000L };
//Console.WriteLine("Hello, world!" + p.GetMaxDenomination(44440L));
for (int j = str1.Length - 1; j >= 0; j--)
{
if (amount >= str1[j] && str1[j] > 1)
{
Console.WriteLine((4/1)+"Hello, world! " +j);
p._root = new Node(amount, j, 0L);
break;
}
}
p.DisplayTree();
Console.WriteLine("Do something test "+ 44440L/10L);
Console.WriteLine("================================================================");
// p.DoSomething();
}
private void DisplayTree(Node root,long count)
{
if (root == null)
{
// Console.WriteLine("ChipCount "+count);
return;
}
//Console.WriteLine("Denom " +root.Denom +" ChipCount " +(root.ChipCount));
if(root.Data == 0)
Console.WriteLine("ChipCount "+(count));
// Console.WriteLine("================================================================");
if (root.Left != null)
DisplayTree(root.Left,root.Left.ChipCount+count);
/* System.Console.WriteLine(root.Data + "Root "+root.Denom1+ " : "+root.Denom2+" chipcount "+root.ChipCount+ "Chip Denomination: "+root.Denom);
if(root.Left != null)
System.Console.WriteLine(root.Left.Data + " left " +root.Left.ChipCount);
if (root.Right != null)
System.Console.WriteLine(root.Right.Data + " right "+root.Right.ChipCount);
*/
if (root.Right != null)
DisplayTree(root.Right,root.Right.ChipCount+count);
// Console.WriteLine("================================================================");
}
public void DisplayTree()
{
DisplayTree(_root,_root.ChipCount);
}
public long GetMaxDenomination(long value)
{
long[] denominations = {1L,5L,10L,25L,50,100L, 500L, 1000L, 5000L, 10000L, 25000L, 50000L,100000L, 500000L, 1000000L, 5000000L, 10000000L,250000000L,500000000L, 100000000L, 500000000L, 1000000000L };
long maxDenomination = denominations[0];
for (int i = 1; i < denominations.Length; i++)
{
float val = value/denominations[i];
Console.WriteLine("value "+ val);
if (val > 0)
{
maxDenomination = denominations[i];
}
}
return maxDenomination;
}
public void DoSomething()
{//{1L,5L,10L,25L,50,100L, 500L, 1000L, 5000L, 10000L, 25000L, 50000L,100000L, 500000L, 1000000L, 5000000L, 10000000L,250000000L,500000000L, 100000000L, 500000000L, 1000000000L };
long[] str1 = {1L,5L,10L,25L,50,100L, 500L, 1000L, 5000L, 10000L, 25000L, 50000L,100000L, 500000L, 1000000L, 5000000L, 10000000L,250000000L,500000000L, 100000000L, 500000000L, 1000000000L };
for(long i = 44440L;i<=44440L; i++)//1000000000L
{
// print("\(i)")
//if(i%5 == 0)
{
var amount = i;
var chipcount = 0L;
for( int j = str1.Length-1;j>=0;j--)//.reversed()
{// print(j)
if(amount/str1[j] > 0 && str1[j]>1)
{
//Console.WriteLine(str1[j]+" reach "+ amount +" here "+amount/str1[j] );
chipcount += amount/str1[j];
var k = amount/str1[j];
amount = amount - str1[j]*k;
}
else if(str1[j]==1)
{
Console.WriteLine(str1[j]+" reach "+ amount +" here "+amount );
chipcount+=amount;amount =0;
}
if(amount == 0)
break;
}
if(chipcount > 11)
{
Console.WriteLine(i+" amount will have chip "+ chipcount);
// print("\(i) amount will have chip \(chipcount)")
}
}
}
}
}
}