关于递归的几个算法实现

来源:互联网 发布:中国制造业pmi指数数据 编辑:程序博客网 时间:2024/04/29 12:09

1.个人所得稅(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.数据结构-通过邻接矩阵递归遍历无向图两点间所有的路径长度及节点序列(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;

    }

}

 

 

 

 

原创粉丝点击