Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.*;
- import java.util.*;
- public class SortTest
- {
- public static void main(String[] args)
- {
- (new SortTest()).test();
- }
- private void test()
- {
- ArrayList<Element> tm = new ArrayList<Element>();
- tm.add(new Element(4, 0.50, 10));
- tm.add(new Element(4, 0.60, 10));
- tm.add(new Element(4, 0.70, 10));
- tm.add(new Element(4, 0.80, 10));
- tm.add(new Element(4, 0.90, 10));
- tm.add(new Element(3, 0.90, 10));
- tm.add(new Element(3, 0.90, 10));
- tm.add(new Element(3, 0.90, 10));
- tm.add(new Element(3, 0.90, 10));
- tm.add(new Element(2, 0.90, 10));
- tm.add(new Element(2, 0.90, 10));
- tm.add(new Element(2, 0.90, 10));
- tm.add(new Element(2, 0.90, 10));
- tm.add(new Element(1, 0.90, 10));
- Element[] tmp = tm.toArray(new Element[0]);
- Arrays.sort(tmp);
- for (Element e : tmp)
- System.out.println(e);
- System.out.println("======================================================");
- int damage = 100;
- int start = 0;
- double total = 0;
- int level = tmp[0].level;
- int levelstart = 0;
- boolean flag1 = false;
- boolean flag2 = false;
- for (int x = 0; x < tmp.length; x++)
- {
- total += tmp[x].ratio;
- if (x == tmp.length - 1 || tmp[x].level != level)
- {
- if (tmp[x].level != level)
- {
- total -= tmp[x].ratio;
- x--;
- flag1 = true;
- }
- if (total > 1)
- {
- for (int y = start; y <= x; y++)
- {
- double t = tmp[y].ratio / total;
- if (t * damage > tmp[y].max)
- {
- tmp[y].ratio = (double)tmp[y].max / (double)damage;
- total = 0;
- for (int z = levelstart; z <= y; z++)
- {
- total += tmp[z].ratio;
- }
- start = y + 1;
- x = y;
- break;
- }
- else
- {
- tmp[y].ratio = t;
- flag2 = true;
- }
- }
- if (flag1 && flag2)
- {
- damage -= (damage * total);
- total = 0;
- start = x + 1;
- level = tmp[start].level;
- levelstart = start;
- flag1 = false;
- flag2 = false;
- System.out.println("Going to level " + level);
- if (damage <= 0)
- {
- for (int y = x + 1; y < tmp.length; y++)
- {
- tmp[y].ratio = 0;
- }
- break;
- }
- }
- }
- else
- {
- for (int y = start; y <= x; y++)
- {
- total -= tmp[y].ratio;
- if (damage * tmp[y].ratio > tmp[y].max)
- {
- tmp[y].ratio = (double)tmp[y].max / (double)damage;
- }
- total += tmp[y].ratio;
- }
- damage -= (damage * total);
- total = 0;
- start = x + 1;
- level = tmp[start].level;
- levelstart = start;
- flag1 = false;
- System.out.println("Going to level " + level);
- if (damage <= 0)
- {
- for (int y = x + 1; y < tmp.length; y++)
- {
- tmp[y].ratio = 0;
- }
- break;
- }
- }
- }
- }
- System.out.println("======================================================");
- for (Element e : tmp)
- System.out.println(e);
- }
- private class Element implements Comparable<Element>
- {
- public double ratio;
- public int max;
- public int level;
- public Element(int c, double a, int b)
- {
- ratio = a; max = b; level = c;
- }
- public int compareTo(Element o)
- {
- if (o.level != level)
- return o.level - level;
- return (int)((max * 100.0D / ratio) - (o.max * 100.0D / o.ratio));
- }
- public String toString()
- {
- return String.format("%d, %d, %f, %d", level, max, ratio, (ratio == 0 ? 0 : (int)(max * 100.0D / ratio)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement