private static int CompareWreckageResultByTotalNum(WreckageResult r1, WreckageResult r2)
 {
     if (r1.GetTotalNumOfWreckage() < r2.GetTotalNumOfWreckage())
     {
         return(-1);
     }
     else if (r1.GetTotalNumOfWreckage() > r2.GetTotalNumOfWreckage())
     {
         return(1);
     }
     else
     {
         if (r1.Value < r2.Value)
         {
             return(-1);
         }
         else if (r1.Value > r2.Value)
         {
             return(1);
         }
         else
         {
             return(0);
         }
     }
 }
 private static int CompareWreckageResultByBodyPoint(WreckageResult r1, WreckageResult r2)
 {
     if (r1.EstimateBodyPoint < r2.EstimateBodyPoint)
     {
         return(-1);
     }
     else if (r1.EstimateBodyPoint > r2.EstimateBodyPoint)
     {
         return(1);
     }
     else
     {
         if (r1.Value < r2.Value)
         {
             return(-1);
         }
         else if (r1.Value > r2.Value)
         {
             return(1);
         }
         else
         {
             return(0);
         }
     }
 }
Esempio n. 3
0
        private void AddResultRow(WreckageResult result, Wreckage[] wreckages, int target)
        {
            ListViewItem lvi = new ListViewItem();

            lvi.SubItems[0].Text = (listViewResult.Items.Count + 1).ToString();
            lvi.SubItems.Add((result.Value - target).ToString());
            lvi.SubItems.Add(result.GetTotalNumOfWreckage().ToString());
            lvi.SubItems.Add(result.EstimateBodyPoint.ToString());
            lvi.SubItems.Add(WreckageCalculator.GetResultString(result, wreckages, target));
            listViewResult.Items.Add(lvi);
        }
        public static String GetResultString(WreckageResult result, Wreckage[] wreckages, int target)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < wreckages.Length; i++)
            {
                if (result.Nums[i] == 1)
                {
                    //sb.Append(wreckages[i].Value + " + ");
                    sb.Append(String.Format("{0:D}*{1:D} + ", wreckages[i].Value, result.Nums[i]));
                }
                else if (result.Nums[i] >= 2)
                {
                    sb.Append(String.Format("{0:D}*{1:D} + ", wreckages[i].Value, result.Nums[i]));
                }
            }

            sb.Remove(sb.Length - 3, 3);
            return(sb.ToString());
        }
        public static List <WreckageResult> DoCalc(Wreckage[] wreckages, int target, int allowedError)
        {
            int calValue = 0;
            List <WreckageResult> results = new List <WreckageResult>();
            int upLimit = target + allowedError;
            int temp;

            for (int i = 0; i < wreckages.Length; i++)
            {
                wreckages[i].CurrNum = 0;

                temp = upLimit / wreckages[i].Value;
                if (wreckages[i].MaxNum > temp)
                {
                    wreckages[i].MaxNum = temp;
                }
            }

            while (true)
            {
next_round:
                if (wreckages[wreckages.Length - 1].CurrNum > wreckages[wreckages.Length - 1].MaxNum) //finished all scan
                {
                    return(results);
                }

                while (wreckages[0].CurrNum <= wreckages[0].MaxNum)
                {
                    calValue = 0;
                    for (int x = 0; x < wreckages.Length; x++)
                    {
                        calValue += (wreckages[x].CurrNum * wreckages[x].Value);
                    }

                    if (calValue >= target && calValue < target + allowedError)
                    {
                        int[] nums = new int[wreckages.Length];
                        for (int y = 0; y < wreckages.Length; y++)
                        {
                            nums[y] = wreckages[y].CurrNum;
                        }
                        WreckageResult r = new WreckageResult(nums, calValue);
                        r.UpdateBodayPoint(wreckages);
                        results.Add(r);
                    }

                    wreckages[0].CurrNum++;
                }

                if (wreckages.Length == 1)
                {
                    return(results);
                }

                wreckages[0].CurrNum = 0;

                for (int i = 1; i < wreckages.Length; i++)
                {
                    if (++wreckages[i].CurrNum > wreckages[i].MaxNum)
                    {
                        //shift right one
                        if (i != (wreckages.Length - 1))
                        {
                            wreckages[i].CurrNum = 0;
                        }

                        for (int j = i + 1; j < wreckages.Length; j++)
                        {
                            if (++wreckages[j].CurrNum <= wreckages[j].MaxNum)
                            {
                                goto next_round;
                            }
                            else
                            {
                                if (j != (wreckages.Length - 1))
                                {
                                    wreckages[j].CurrNum = 0;
                                }
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }