[Improve PHP basic physical strength] Let’s solve the basic problem of competitive professional (AOJ-ICPC) with PHP!

Aug 30, 2020 PHP For beginners Competitive programming AOJ-ICPC

#Audience for this article

Purpose of writing this article

It is quite a hurdle for a beginner who has learned a programming language with books and online learning materials such as dot installation to suddenly create a website.

What I would like to recommend to those beginners is to practice and solve simple programming problems. Through this practice, you can “practice your coding skills purely without devoting your power to service planning.”

This should become a fundamental force that will definitely help you in developing Web services later.

This is also the recommended method for those who want to change their careers from inexperienced to software development. For example, in a service that allows you to do job hunting by taking the coding test paiza, you can interview a considerable number of companies “without an entry sheet” if you can get up to about A rank. .. In addition, it was very popular to inform the companies interviewed with media other than paiza that they have A rank in paiza.

However, it is basically forbidden to explain the paiza problem externally, so this time we will use the site AOJ-ICPCLevel100problem(IthinkthereisaBrankclassinpaizaevenatthebottom) I will try to solve it while carefully explaining!

*Well, the conclusion is very interesting and it will lead to an increase in coding strength, and at the same time (because you can get the rank in paiza), it will be advantageous for you to get a job, so Wai and AOJ-ICPC Yarouz!

#Preparation

“Carefully prepare paiza in Vim step by step”

Understand the problem

This time, I will discuss the problem of ICPC score counting software.

First of all, before solving the problem (especially for beginners), please read the question sentence with _reading and make sure you understand it well.

Did you read it? If you can afford it, solve it by yourself before reading below. (In that case, let’s try while thinking about the logic to solve with one hand of notepad!)

First, consider what kind of data will be given

From the above, you can see that the data is given in the following format.


//input is given in the following format.
Number of judges n
s_1
s_2
s_3
.
.
.
s_n-1
s_n
Number of judges m
s_1
s_2
.
.
.
s_m
0 -> Input here End!

For the performance score, the maximum and minimum values are subtracted from the sum of the scores given by the judges. After that, divide the number of judges by the number (2) minus the maximum and minimum judges. The score should be “The average score may have a fraction, but it is rounded down and the final score is an integer.”

First try coding the solid writing process

Fgets(STDIN) to get standard input. Standard cast is obtained as string type (character), so type cast. (int) becomes int type.

$judge_num = (int) fgets(STDIN);
$score_list = [];
for($i=0;$i<$judge_num;$i++){
    $score_list[] = (int)fgets(STDIN);
}

To get the sum, maximum and minimum of the array, array_sum functionandmaxfunction,minfunction. Use the floor function because the problem statement says that the average value should be truncated.

$max_num = max($score_list);
$min_num = min($score_list);
$score_sum = array_sum($score_list)-$max_num-$min_num;
$adjusted_ave_score = floor($score_sum / ($judge_num-2));

You can now calculate the points for one audit.

However, this time I want to give points until the number of judges reaches 0. I don’t know how many times it will be processed. .. .. In that case, use a while statement instead of a for loop.

while(True){
  $judge_num = (int) fgets(STDIN);
  //When the number of judges is 0, the while loop is broken.
  if ($judge_num === 0){
      break;
      }
      //processing
      .
      .
      .
  }

The following is the finished product.

<?php
while(True){
        // Get the number of judges
        $judge_num = (int) fgets(STDIN);
        if ($judge_num === 0){
                break;
        }
        // put scores into an array
        $score_list = [];
        for($i=0;$i<$judge_num;$i++){
                $score_list[] = (int)fgets(STDIN);
        }
        //Get maximum and minimum
        $max_num = max($score_list);
        $min_num = min($score_list);
        //Calculate the adjusted average
        $score_sum = array_sum($score_list)-$max_num-$min_num;
        $adjusted_ave_score = floor($score_sum / ($judge_num-2));
        echo $adjusted_ave_score .PHP_EOL;
}

Try to functionalize

According to Masato Oya, a full-fledged introduction to PHP [upper], “a function is a collection of a series of program processing that makes sense to humans, so that it can be handled as a single instruction with an original name.”

The advantage of using functions is

(1) It becomes DRY code (you do not need to write program processing in many places with copy and paste) (2) Readability improvement (If you write back the function name and comment firmly when you read it back later, you can understand what you are doing without having to read the contents of the process)

There are two.

This time, I will show you the finished product.

<?php

function get_input_num(){
        $input_num = (int) fgets(STDIN);
        return $input_num;
}

function make_score_list($judge_num){
        $score_list = [];
        for($i=0;$i<$judge_num;$i++){
                $score_list[] = get_input_num();
        }
        return $score_list;
}

function calculate_adjusted_ave_score($judge_num, $score_list){
        $max_num = max($score_list);
        //echo $max_num;
        $min_num = min($score_list);
        //echo $min_num;
        $score_sum = array_sum($score_list)-$max_num-$min_num;
        $adjusted_ave_score = $score_sum / ($judge_num-2);
        return $adjusted_ave_score;
}

while(True){
        // Get the number of judges
        $judge_num = get_input_num();
        //End the input when the judge's number is 0 and exit from while
        if ($judge_num === 0){
                break;
        }
        //Let's code while checking the numbers with echo! Comment out when you no longer need
        //echo $judge_num;

        // put the score in an array
        $score_list = make_score_list($judge_num);
        //Check the array with var_export!
        //var_export($score_list);
        
        // Get the adjusted average excluding the maximum and minimum values
        $adjusted_ave_score = calculate_adjusted_ave_score($judge_num, $score_list);
        // round off the decimal point$adjusted_ave_score = floor($adjusted_ave_score);
         echo $adjusted_ave_score .PHP_EOL;
 }

Let’s write it yourself! (Think other logic)

For example, from the logic of getting the score as an array and using the max and min functions. .. ..

 max_num = -1
 min_num = 1001

Set and set these variables and score each time they are obtained from standard input, and compare and update.

Let’s think about what other logic can be used to write!