Advertisement
LexManos

Armor Sorting.

Feb 13th, 2012
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.09 KB | None | 0 0
  1. import java.lang.*;
  2. import java.util.*;
  3.  
  4. public class SortTest
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         (new SortTest()).test();
  9.     }
  10.     private void test()
  11.     {
  12.         ArrayList<Element> tm = new ArrayList<Element>();
  13.         tm.add(new Element(4, 0.50, 10));
  14.         tm.add(new Element(4, 0.60, 10));
  15.         tm.add(new Element(4, 0.70, 10));
  16.         tm.add(new Element(4, 0.80, 10));
  17.         tm.add(new Element(4, 0.90, 10));
  18.         tm.add(new Element(3, 0.90, 10));
  19.         tm.add(new Element(3, 0.90, 10));
  20.         tm.add(new Element(3, 0.90, 10));
  21.         tm.add(new Element(3, 0.90, 10));
  22.         tm.add(new Element(2, 0.90, 10));
  23.         tm.add(new Element(2, 0.90, 10));
  24.         tm.add(new Element(2, 0.90, 10));
  25.         tm.add(new Element(2, 0.90, 10));
  26.         tm.add(new Element(1, 0.90, 10));
  27.         Element[] tmp = tm.toArray(new Element[0]);
  28.         Arrays.sort(tmp);
  29.        
  30.         for (Element e : tmp)
  31.             System.out.println(e);
  32.         System.out.println("======================================================");
  33.        
  34.         int   damage = 100;
  35.         int    start = 0;
  36.         double total = 0;
  37.         int    level = tmp[0].level;
  38.         int levelstart = 0;
  39.         boolean flag1 = false;
  40.         boolean flag2 = false;
  41.        
  42.         for (int x = 0; x < tmp.length; x++)
  43.         {
  44.             total += tmp[x].ratio;
  45.             if (x == tmp.length - 1 || tmp[x].level != level)
  46.             {
  47.                 if (tmp[x].level != level)
  48.                 {
  49.                     total -= tmp[x].ratio;
  50.                     x--;
  51.                     flag1 = true;
  52.                 }
  53.                 if (total > 1)
  54.                 {
  55.                     for (int y = start; y <= x; y++)
  56.                     {
  57.                         double t = tmp[y].ratio / total;
  58.                         if (t * damage > tmp[y].max)
  59.                         {
  60.                             tmp[y].ratio = (double)tmp[y].max / (double)damage;
  61.                             total = 0;
  62.                             for (int z = levelstart; z <= y; z++)
  63.                             {
  64.                                 total += tmp[z].ratio;
  65.                             }
  66.                             start = y + 1;
  67.                             x = y;
  68.                             break;
  69.                         }
  70.                         else
  71.                         {
  72.                             tmp[y].ratio = t;
  73.                             flag2 = true;
  74.                         }                        
  75.                     }
  76.                     if (flag1 && flag2)
  77.                     {
  78.                         damage -= (damage * total);
  79.                         total = 0;
  80.                         start = x + 1;
  81.                         level = tmp[start].level;
  82.                         levelstart = start;
  83.                         flag1 = false;
  84.                         flag2 = false;
  85.                         System.out.println("Going to level " + level);
  86.                         if (damage <= 0)
  87.                         {
  88.                             for (int y = x + 1; y < tmp.length; y++)
  89.                             {
  90.                                 tmp[y].ratio = 0;
  91.                             }
  92.                             break;
  93.                         }
  94.                     }
  95.                 }
  96.                 else
  97.                 {
  98.                     for (int y = start; y <= x; y++)
  99.                     {
  100.                         total -= tmp[y].ratio;
  101.                         if (damage * tmp[y].ratio > tmp[y].max)
  102.                         {
  103.                             tmp[y].ratio = (double)tmp[y].max / (double)damage;
  104.                         }
  105.                         total += tmp[y].ratio;
  106.                     }
  107.                     damage -= (damage * total);
  108.                     total = 0;
  109.                     start = x + 1;
  110.                     level = tmp[start].level;
  111.                     levelstart = start;
  112.                     flag1 = false;
  113.                     System.out.println("Going to level " + level);
  114.                     if (damage <= 0)
  115.                     {
  116.                         for (int y = x + 1; y < tmp.length; y++)
  117.                         {
  118.                             tmp[y].ratio = 0;
  119.                         }
  120.                         break;
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.         System.out.println("======================================================");
  126.         for (Element e : tmp)
  127.             System.out.println(e);
  128.     }
  129.    
  130.     private class Element implements Comparable<Element>
  131.     {
  132.         public double ratio;
  133.         public int max;
  134.         public int level;
  135.         public Element(int c, double a, int b)
  136.         {
  137.             ratio = a; max = b; level = c;
  138.         }
  139.         public int compareTo(Element o)
  140.         {
  141.             if (o.level != level)
  142.                 return o.level - level;
  143.                
  144.             return (int)((max * 100.0D / ratio) - (o.max * 100.0D / o.ratio));
  145.         }
  146.        
  147.         public String toString()
  148.         {
  149.             return String.format("%d, %d, %f, %d", level, max, ratio, (ratio == 0 ? 0 : (int)(max * 100.0D / ratio)));
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement