PHP Bonus - Online Judge Problem Solving Using PHP

PHP Bonus - Online Judge Problem Solving Using PHP

Problem Solving

Todays, lecture is totally different from PHP programming language aspects. This is for both PHP and all general level programming languages. Problem Solving is a very important parts to becoming a very good Developer. In our practical life, we’ll face lots of different and new problems every day when we starts our real project. If we have proper problem solving skills, then we can easily solve any problem easily. Let’s try solving some of the problems in popular online judge programming platform.

Popular online problem solving platforms -

  1. BeeCrowd - https://www.beecrowd.com.br/judge/en
  2. UVA Online Judge - https://onlinejudge.org
  3. HackerRank Online Judge - https://www.hackerrank.com
  4. CodeForce Online Judge - https://codeforces.com
  5. LightOJ - https://lightoj.com

And there is so many online programming problem solving websites out there. We’ll start using the easiest one BeeCrowd or previously URI online judge.

BeeCrowd Online Judge

BeeCrowd is one of the most popular online judge problem solving platform. It’s suitable for both beginner, intermediate and advanced level users.

How we’ll solve the problems ?

We’ll read the problems very carefully and solve those problems using PHP language. You can try with other language also if you’re comfortable. We’ll solve some Beginner Level problems - https://www.beecrowd.com.br/judge/en/problems/index/1

Test in Online PHP Code -

https://onecompiler.com/php/3y5ak79p9

Problem 1: Hello World

https://www.beecrowd.com.br/judge/en/problems/view/1000

Problem Details

beecrowd | 1000

Hello World!

Jean Bez, beecrowd Brasil

Timelimit: 1

Welcome to beecrowd!

Your first program in any programming language is usually "Hello World!". In this first problem all you have to do is print this message on the screen.

Input

This problem has no input.

Output

You must print the message Hello World! and then the endline as shown below.

Input Sample Output Sample

Hello World!

Solution in PHP

<?php

echo "Hello World!\n";

?>

Problem 2: Extremely Basic

https://www.beecrowd.com.br/judge/en/problems/view/1001

Solution in PHP

<?php

$input1 = intval(fgets(STDIN));
$input2 = intval(fgets(STDIN));
$summation = $input1 + $input2;

echo "X = $summation\n";

?>

Problem 3 - Multiples

https://www.beecrowd.com.br/judge/en/problems/view/1044

Solution in PHP

<?php

$numbers = fgets(STDIN);

$a = intval(explode(" ", $numbers)[0]);
$b = intval(explode(" ", $numbers)[1]);

if (($b % $a == 0) || ($a % $b == 0)) {
    echo "Sao Multiplos\n";
} else {
    echo "Nao sao Multiplos\n";
}

?>

Problem 4 - Upto Odd Numbers

https://www.beecrowd.com.br/judge/en/problems/view/1067

Solution in PHP

<?php

$x = intval(fgets(STDIN));

for($i = 1; $i <= $x; $i += 2) {
    echo $i . "\n";
}

?>
Previous
PHP Array Part 2 - Array Built in PHP Functions and Some More Examples
Next
PHP - Functions in PHP - How to create and how to use