有人可以解释这个java代码

时间:2013-07-03 22:32:01

标签: java

这是Zyflair的topcoder SRM 569 DIV 2 problem 250解决方案

我无法理解代码或它是如何工作的,有人可以解释它或简化它。

public int countSupervisors(int[] students, int Y, int J){
  for(int i = 0,Y2 = Y + (Y = 0),J2=J+(J=0) ;i<students.length; i++,Y +=students [i -1 ] ){
   J =Math.max(J,-(Math.max(0, students[i]-Y2)+J2-1)/J2+(students[i] = (students[i]+J2-1)/J2));
        }
  return Y-J;
}

1 个答案:

答案 0 :(得分:1)

我现在没有时间研究这个问题,但这里有点简化:

// Y2 = Y + (Y = 0)
int Y2 = Y;
Y = 0;

// J2 = J + (J = 0)
int J2 = J;
J = 0;

// Note that, since we've just set Y and J to 0,
//   we may as well have used different variables

for (int i = 0; i < students.length; i++)
{
   int temp = (students[i] + J2 - 1) / J2;
   J = Math.max(J, -(Math.max(0, students[i] - Y2) + J2 - 1) / J2 + temp);
   Y += temp;
}
return Y-J;