Several recursive algorithm implementations

Source: Internet
Author: User

1. Personal Income (C #)

Using system;

Using system. Collections. Generic;

Using system. LINQ;

Using system. text;

 

namespace Tax{    class Program    {        static double tax_fee = 0;        const double minus_money = 2000.0;        static void Main(string[] args)        {            Console.WriteLine(fun(8000 - minus_money, 1));            Console.ReadKey();        }                static double fun(double money, int i)        {            double[] num ={0,500,2000, 5000, 20000, 40000, 60000, 80000, 100000,double.MaxValue};            double[] tax_num = {0.05,0.10,0.15,0.20,0.25,0.30,0.35,0.40,0.45};            if (i > num.Length || money < 0)                return tax_fee;            tax_fee += Math.Min(num[i] - num[i - 1], Math.Max(money - num[i], money)) * tax_num[i - 1];            fun(money - num[i] + num[i - 1], ++i);                     return tax_fee;        }    }}

 

 

2. Data Structure-recursively traversing the path lengths and node sequences between two points in an undirected graph through the adjacent matrix (Java)

 

 

Import java. util. arraylist;

 

Public class graph

{

Public final int max_vertex = 9;

Private vertex [] vertexlist = new vertex [max_vertex];

Private int matrix [] [] = new int [max_vertex] [max_vertex];

Private Static int vertexnum = 0;

Private Static int sum = 0;

Private arraylist <integer> shortestvalue = new arraylist <integer> ();

Private arraylist <string> queue = new arraylist <string> ();

Public arraylist <string> shortestway = new arraylist <string> ();

Private int min = 0;

 

Public graph ()

{

For (INT I = 0; I <max_vertex; I ++)

{

For (Int J = 0; j <max_vertex; j ++)

{

Matrix [I] [J] = 0;

}

}

}

 

Class Vertex

{

Public char name;

Public Boolean visited;

Public vertex (char name)

{

This. Name = Name;

Visited = false;

}

}

 

Public void addvertex (char name)

{

Vertexlist [vertexnum ++] = new vertex (name );

}

 

Public void addedge (char start, char end, int value)

{

Int startnum = find (start );

Int endnum = find (end );

If (startnum! =-1 & endnum! =-1)

{

Matrix [startnum] [endnum] = value;

Matrix [endnum] [startnum] = value;

}

}

 

Public int find (char name)

{

For (INT I = 0; I <vertexlist. length; I ++)

{

If (vertexlist [I]. Name = Name)

Return I;

}

Return-1;

}

 

Public void printmatrix ()

{

For (INT I = 0; I <max_vertex; I ++)

{

For (Int J = 0; j <max_vertex; j ++)

{

System. Out. Print (Matrix [I] [J] + "");

}

System. Out. println ();

}

}

 

Public int findminway (char startvertex, char endvertex)

{

Int value;

Int start = find (startvertex );

Int end = find (endvertex );

Sum = 0;

Min = 99999;

Shortestvalue. Clear ();

Queue. Clear ();

Shortestway. Clear ();

Findminway (START, end );

Value = findmin (shortestvalue );

Return value;

}

 

@ Suppresswarnings ("unchecked ")

Public void findminway (INT start, int end)

{

Vertexlist [start]. Visited = true;

Queue. Add (vertexlist [start]. Name + "");

If (START = end)

{

For (INT I = 0; I <queue. Size (); I ++)

System. Out. Print (queue. Get (I) + "");

System. Out. println (SUM );

Shortestvalue. Add (SUM );

Vertexlist [start]. Visited = false;

If (shortestvalue. Get (shortestvalue. Size ()-1) <min)

{

Min = shortestvalue. Get (shortestvalue. Size ()-1 );

Shortestway = (arraylist <string>) queue. Clone ();

}

Queue. Remove (queue. Size ()-1 );

Return;

}

For (INT I = 0; I <vertexlist. length; I ++)

{

If (Matrix [start] [I]! = 0 & vertexlist [I]. Visited = false)

{

Sum + = matrix [start] [I];

Findminway (I, end );

Sum-= matrix [start] [I];

}

}

Vertexlist [start]. Visited = false;

Queue. Remove (queue. Size ()-1 );

Return;

}

 

Private int findmin (arraylist <integer> value)

{

Int min = 9999999;

For (INT I = 0; I <value. Size (); I ++)

{

If (value. Get (I)> = 0 & value. Get (I) <min)

{

Min = value. Get (I );

}

}

Return min;

}

}

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.