作业:

[total_score, user => [username, realname], scores[]] $standings = []; foreach ($usernames as $username) { $user = queryUser($username); $row = ['total_score' => 0]; $scores = []; $row['user'] = [ 'username' => $user['username'], 'realname' => $user['realname'], ]; $cond = "submitter = '{$user['username']}' AND unix_timestamp(submit_time) <= " . $submission_end_time->getTimestamp(); foreach ($problem_ids as $problem_id) { $submission = DB::selectFirst("SELECT id, score FROM submissions WHERE problem_id = $problem_id AND $cond ORDER BY score DESC, id DESC"); if ($submission) { $row['scores'][] = [ 'submission_id' => $submission['id'], 'score' => intval($submission['score']), ]; $row['total_score'] += $submission['score']; } else { $row['scores'][] = null; } } $standings[] = $row; } usort($standings, function($lhs, $rhs) { if ($lhs['total_score'] != $rhs['total_score']) { return $rhs['total_score'] - $lhs['total_score']; } return strcmp($lhs['user']['username'], $rhs['user']['username']); }); ?>