Skip to main content
Code Review

Return to Question

Tweeted twitter.com/StackCodeReview/status/1026665139855077376
Blockquote and markdown the problem statement, assuming it's a direct quote
Source Link
Toby Speight
  • 87.2k
  • 14
  • 104
  • 322

Problem: ##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.

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##My solution:

[![enter image description here][1]][1] side lengths {2,3,4} and {4,3,5} differ, but {2,3,4} and[1]: {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//i. 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, inclusivesstatic. If there are more than 1,000,000,000 return -1net/b9kq3.

My solution:

enter image description here png

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

##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][1]][1] [1]: //i.sstatic.net/b9kq3.png

edited title
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

Algorithm problem and php organisation Counting distinct triangles that have integer-length sides

added 618 characters in body
Source Link
Ludisposed
  • 11.8k
  • 2
  • 41
  • 91
<?php

/** *Class Form */

class Form{

/**
 *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>");
}
}

}


count($params['min'], $params['max']);return $nbre;}else{$message_erreur = "Vous devez remplir avec des entiers superieur à 0!";return $message_erreur;}}}$k){$count++;}else{break;}}}}//si le nombre de possibilité dépasse 1000000000if($count
<?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;
 }
 }
}
<?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>");
}

}

count($params['min'], $params['max']);return $nbre;}else{$message_erreur = "Vous devez remplir avec des entiers superieur à 0!";return $message_erreur;}}}$k){$count++;}else{break;}}}}//si le nombre de possibilité dépasse 1000000000if($count
<?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;
 }
 }
}
Source Link
k.am
  • 53
  • 3
Loading
lang-php

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