c# 最小重量机器设计

来源:互联网 发布:大数据的现状与展望 编辑:程序博客网 时间:2024/06/08 18:44

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _4
{
    /*class BL
    {
        public double Hight(int i, int cij)
        {
            return i + cij;
        }

        public double Gys(int sn,int cj,int bj,int jg)
        {
            int[] jg2 = new int[sn];  //价格
            int[] zl = new int[sn];  //重量          
            for (int i = 0; i <= sn; i++)
            {
                jg2[i] = (i + 1) * bj;
                zl[i] = (i + 1) * bj;
            }

            return Hight(jg2[cj], zl[cj]);
        }
    }*/


    class MinWmechine
    {
        int n; //部件个数
        int m; //供应商个数
        int COST; //题目中的C
        int cw; //当前的重量
        int cc; //当前花费
        int bestw; //当前最小重量
        public int[] bestx=new int[50];
        public int[] savex=new int[50];
        public int[][] w=new int[50][];
        public int[][] c=new int[50][];
 
        public MinWmechine()
        {
            cw=0; //当前的重量
            cc=0; //当前花费
            bestw=-1; //当前最小重量
            for (int i = 0; i <50; i++)
            {
                w[i] = new int[50];
                c[i] = new int[50];
            }

            Console.WriteLine("请输入部件个数:");
            n=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入供应商个数:");
            m=Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入总价格不超过:");
            COST=Convert.ToInt32(Console.ReadLine());

            for(int j=0;j<m;j++)
            {
               for(int i=0;i<n;i++)
               {
                    Console.WriteLine("请输入第 "+(j+1)+" 个供应商的第 "+(i+1)+" 个部件的重量:");
                    w[i][j]=Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("请输入第 "+(j+1)+" 个供应商的第 "+(i+1)+" 个部件的价格:");
                    c[i][j]=Convert.ToInt32(Console.ReadLine());

                    if(w[i][j]<0 || c[i][j]<0)
                    {
                        Console.WriteLine("重量或价钱不能为负数!");
                      i=i-1;
                    }
               }
            }
        }
        public void machine_plan(int i)
        {
            if(i>=n)
            {
               if(cw <bestw || bestw==-1)
               {
                bestw=cw;
                for(int j=0;j<n; j++) //把当前搜过的路径记下来
                 savex[j]=bestx[j];
               }
               return;
            }
            for(int j=0; j<m; j++) //依次递归尝试每个供应商
            {
               if(cc+c[i][j]<COST)
               {
                    cc+=c[i][j];
                    cw+=w[i][j];
                    bestx[i]=j;

                    machine_plan(i+1);

                    bestx[i]=-1;
                    cc-=c[i][j];
                    cw-=w[i][j];
               }
            }
        }
        public void prinout()
        {
            int i,j,ccc=0;

            for(j=0;j<m;j++)
            {
               for(i=0;i<n;i++)
               {
                Console.WriteLine("第 "+(j+1)+" 供应商的第 "+(i+1)+" 部件重量:"+w[i][j]+" 价格:"+c[i][j]);
               }
            }
            for(j=0; j<n; j++)
            {
               bestx[j]=-1;
            }          
            machine_plan(0);

            Console.WriteLine("最小重量机器的重量是: "+bestw);
            for(int k=0; k<n; k++)
            {
               Console.WriteLine(" 第 "+(k+1)+" 部件来自供应商 "+(savex[k]+1));
               ccc+=c[k][savex[k]];
            }
            Console.WriteLine("该机器的总价钱是: "+ccc);
            Console.WriteLine();

        }

    }


    class Program
    {
        static void Main(string[] args)
        {
            /*int c,bj,cj,jg;   //bj 部件数量 //cj 厂家数量 //jg 价格
           
            c = Convert.ToInt32(Console.ReadLine());
            bj = Convert.ToInt32(Console.ReadLine());
            cj = Convert.ToInt32(Console.ReadLine());
            jg = Convert.ToInt32(Console.ReadLine());
            BL b=new BL();
            double[][][] x = new double[cj][][];
            double[][] s=new double[bj][];
            for (int i = 0; i <= cj; i++)
            {
                for (int j = 0; j <= bj; j++)
                {
                    for(int z=0;z<=jg;z++)
                    {
                        x[i][j][z] = b.Gys(cj,i,j,z);
                        s[j][z] += x[i][j][z];
                    }
                }
               
            }

           
            for (int j = 0; j <= cj; j++)
            {
                for(int z=0;z<=jg;z++)
                    if (c < s[j][z])
                        Console.WriteLine("最小价格分别为" + s[j][z]+"厂家为"+j+"号 "+"价格为 "+z);
            }*/
            MinWmechine y=new MinWmechine();
            y.prinout();
            Console.ReadLine();
           
        }

    }
}

原创粉丝点击