Below is the code of a simple auto-tournament of cats: we drive in the number of participants - we get the result of their hassles. The crux of the problem - Cats with an odd postfix are not written to the second intermediate array? Why? I would be glad if at least in general describe the sources of the problem with the indication of lines, and also send to read on specific topics to eliminate such troubles in the future)

import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; public class Solution { private static int mastercount=0; private static ArrayList<Cat> cats = new ArrayList<Cat>(); public static void main(String[] args) throws Exception{ int round = 1; int cc=0; BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); while(cc==0) { cc = Integer.parseInt(bf.readLine()); } bf.close(); String[] names = {"Котик","Киса"}; for (int i = 0; i < cc; i++) { cats.add(new Cat(names[(int)(Math.random()*2)]+(i+1),(int)((Math.random()*15)+1),(int)((Math.random()*8)+1),(int)((Math.random()*10)+1))); } while(mastercount>2 || mastercount==0){ checkMassive(cats,round); round++; } System.out.println("THE END");//Проверка окончания работы } public static void checkMassive(ArrayList<Cat> c,int r){ int mascount=0; for (int i = 0; i < c.size(); i++) { if (c.get(i).fightCount == 0) { if(i!=c.size()-1) {//если не последняя последняя ячейка if(c.get(i).fight(c.get(i+1))) { if(c.size()>2) System.out.println("Round "+r+" winner: "+ c.get(i).name); else System.out.println("!!!ABSOLUTELY WINNER!!!"+c.get(i).name+"!!!ABSOLUTELY WINNER!!!"); c.get(i).fightCount++; c.get(i+1).fightCount--; mascount+=2; } else { if(c.size()>2) System.out.println("Round "+r+" winner: "+ c.get(i+1).name); else System.out.println("!!!ABSOLUTELY WINNER!!!"+c.get(i+1).name+"!!!ABSOLUTELY WINNER!!!"); c.get(i+1).fightCount++; c.get(i).fightCount--; mascount+=2; } } else{ if(c.size()>2) System.out.println("Round "+r+" INDIVIDwinner: "+ c.get(i).name);//добавил в текст INDIVID, для простоты анализа отображения появления единственного участника в раунде else System.out.println("!!!INDIVIDABSOLUTELY WINNER!!!"+c.get(i).name+"!!!ABSOLUTELY WINNER!!!"); c.get(i).fightCount++; mascount+=1; } } } Cat[] tmp = new Cat[c.size()];//создаём временный массив для выбора победителей - пробывал ArrayList - тоже самое for (int i = 0; i < c.size(); i++) {//Не записывает победителей ТОЛЬКО с нечётными именами if(c.get(i).fightCount==1) { c.get(i).fightCount=0; tmp[i] = c.get(i); } } for (int i = 0; i < tmp.length; i++) {//проверка того - что записалось if(tmp[i]!=null) System.out.println(""+tmp[i].name); } Solution.cats.clear(); for (int i = 0; i < tmp.length; i++) { if(tmp[i]!=null){ Solution.cats.add(tmp[i]); } } Arrays.fill(tmp,null); mastercount = mascount; } public void setCats(ArrayList<Cat> a){ } public static class Cat { public static int count = 0; public int fightCount = 0; protected String name; protected int age; protected int weight; protected int strength; public Cat(String name, int age, int weight, int strength) { count++; this.name = name; this.age = age; this.weight = weight; this.strength = strength; } public boolean fight(Cat anotherCat) { fightCount++; double a,w,s,p1; if(anotherCat.age<10 && this.age<10) a = this.age - anotherCat.age; else{ p1 = this.age; if(anotherCat.age>9) anotherCat.age = anotherCat.age/(((anotherCat.age-9)/10)*4+1); if(this.age>9) p1 = this.age/(((this.age-9)/10)*4+1); a = p1 - anotherCat.age; } if(anotherCat.weight<7 && this.weight<7) w = this.weight - anotherCat.weight; else{ p1 = this.weight; if(anotherCat.weight>6) anotherCat.weight = anotherCat.weight/(((anotherCat.weight-6)/10)*3+1); if(this.weight>6) p1 = this.weight/(((this.weight-6)/10)*3+1); w = p1 - anotherCat.weight; } s = this.strength - anotherCat.strength; p1 = a+w+s; if (p1>0)return true; else return false; } } } 

    2 answers 2

    Replace this unit:

     if (c.get(i).fightCount > 0) { c.get(i).fightCount = 0; tmp[i] = c.get(i); } 

    The fact is that your seals get more than one battle points) - they cheat. Note that you change the value of the fightCount variable, including in the fight method () ...

    • Thank you very much - I'm just a blind idiot) - such a stupid mistake. Correctly I minus - just a shame. - fargusconner

    Zahardkodte variables themselves cats. As I understand this name and parameters, age, weight, strength. You can get confused in these random places.

    In the fight () method, simply compare, for example, these 3 parameters and for each give us a point on one or a second cat. if the first one has more points, return the tru.

    A rounds can be defended like this:

      System.out.println("Схватка между Барсиком и Мурзиком..."); if (cat1.fight(cat2)) System.out.println("Победу одержал Барсик"); else System.out.println("Победу одержал Мурзик");