Skip to main content
Code Review

Return to Revisions

3 of 4
edited title
200_success
  • 145.5k
  • 22
  • 190
  • 478

Counting distinct triangles that have integer-length sides

Problem:

We are interested in triangles that have integer length sides, all of which are between minLength and maxLength, inclusive. How many such triangles are there? Two triangles differ if they have a different collection of side lengths, ignoring order. Triangles with side lengths {2,3,4} and {4,3,5} differ, but {2,3,4} and {4,2,3} do not. We are only interested in proper triangles; the sum of the two smallest sides of a proper triangle must be strictly greater than the length of the biggest side. Create a class TriCount that contains a method count that is given ints minLength and maxLength and returns the number of different proper triangles whose sides all have lengths between minLength and maxLength, inclusive. If there are more than 1,000,000,000 return -1.

My solution:

enter image description here

<?php
/**
 *Class Form
 */
class Form{
 /**
 *@var array données utilisées par le formulaire
 */
 protected $data;
 /**
 *@var string tag qui entoure les champs
 */
 public $surroud ='p';
 /**
 *@param array $data
 *@return string
 */
 public function __construct($data = array()){
 $this->data = $data;
 }
 /**
 *@param $html string
 *@return string
 */
 protected function surroud(string $html){
 return "<{$this->surroud}>".$html."</{$this->surroud}>";
 }
 /**
 *@param $index string
 *@return string
 */
 protected function getValue(string $index){
 return isset($this->data[$index]) ? $this->data[$index] : null;
 }
 /**
 *@param $name string
 *@return string
 */
 public function input(string $name){
 return $this->surroud("<label for='".$name."'>".$name.": </label><input type='text' name='".$name."' value='".$this->getValue($name)."'>");
 }
 /**
 *@return string
 */
 public function submit(){
 return $this->surroud("<button type='submit'>Envoyer</button>");
 }
}

<?php
class FormController{
 /**
 *@return objet
 */
 public function registerI()
 {
 return new TriCount();
 }
 /**
 *@param $params array
 *@return integer
 */
 public function register(array $params)
 {
 //les champs sont remplis d'entier
 if(intval($params['min']) && intval($params['max'])){
 //instancier la classe pour le calcul des probabilités
 $inst = new TriCount();
 //appel de la methode qui calcul les probabilités
 $nbre = $inst->count($params['min'], $params['max']);
 return $nbre;
 }else{
 $message_erreur = "Vous devez remplir avec des entiers superieur à 0!";
 return $message_erreur;
 }
 }
}

<?php
/**
 *Class TriCount
 */
class TriCount{
 /**
 *@var integer minimum du tableau
 */
 private $minLength;
 /**
 *@var integer maximum du tableau
 */
 private $maxLength;
 /**
 *@var integer nombre de triangle possible
 */
 private $count;
 /**
 *@param $minLength integer
 *@param $maxLength integer
 *@return integer
 */
 public function count(int $minLength , int $maxLength ){
 //initialiser le compteur
 $count = 0;
 //3 boucles qui font varier le (i,j,k)
 // le script s'arrete si la condition n'est pas vérifiée
 for ($i = $minLength; $i <= $maxLength; $i++){
 for($j = $i ; $j <= $maxLength; $j++){
 for($k = $j ; $k <= $maxLength; $k++){
 //condition: la somme des deux petits cotés du triangle superieur au troisieme coté
 if( ($i + $j ) > $k ) {
 $count++;
 }else{
 break;
 }
 }
 }
 }
 //si le nombre de possibilité dépasse 1000000000
 if ($count <= 1000000000 ){
 return $count;
 }else {
 return -1;
 }
 }
}
k.am
  • 53
  • 3
lang-php

AltStyle によって変換されたページ (->オリジナル) /