用于查找最大和第二大数字而不使用数组的php代码

时间:2018-01-03 09:54:11

标签: php html

<form id="form1" name="form1" method="post" action="">
   <input name="txt1" type="text" /><br />
   <input name="txt2" type="text" /><br />
   <input name="txt3" type="text" /><br />
   <input name="s" type="submit" value="Find No"/>
   </form> 

这是我的HTML代码。

    <?php

    $a = 10;
    $b = 25;
    $c = 20;

    if($a > $b)
    {
  if($a > $c)
 {
 echo "a is biggest number";
 }
else
 {
 echo "c is biggest number";
 }
}
if($b > $c)
{
echo "b is biggest number";
}

?>

`这是寻找

的代码 三个号码中最大的一个。我希望得到最大和第二大的没有

3 个答案:

答案 0 :(得分:1)

给定从输入字段获得的数字字符串数组,例如,一个反映原始值的字符串:

$a = array('1', '5', '19', '200', '999');

您可以使用以下方法将其转换为真正的数值数组:

$a = array_map('intval', $a);

然后完全按照之前的步骤进行:

$last = max($a);
$second = max(array_diff($a,[$last]));

小提琴here

答案 1 :(得分:1)

你必须使用输入框和一个非常简单的基本工作模型的提交按钮。

HTML:

<?php
    if(isset($_REQUEST['arr']) && !empty($_REQUEST['arr'])){
        $a = explode(' ', $_REQUEST['arr']);
        $last = max($a);
        $second = max(array_diff($a,[$last]));
        echo $second;
        echo $last;
?>

//现在用户可以在输入框中提供输入空间,例如9 11 13 15 16 29等。

PHP

storeDossie: (req, res) => {
var dossieStamp = req.params.dossie;
var where = req.params.where;
var dbname = req.params.dbname;

  var dossie = request.query("select * from ts where tsstamp ='"+dossieStamp+"'");
  console.log("RETURNS THE ERROR HERE");
  Promise.all([dossie]).then(function(results)
  {

    var today = new Date();
    var date = today.getFullYear()+'-'+(today.getMonth()+1)+'-'+today.getDate();
    var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();

    var addDossie = request.query("INSERT INTO u_options (area, type, sub_type, dossie_stamp, dossie_name, ndos, date, hora)"+
    " VALUES ('"+area+"', '"+type+"', '"+subtype+"', '"+results[0][0].tsstamp+"' ,'"+results[0][0].nmdos+"', "+results[0][0].ndos+", '"+date+"', '"+time+"')");

    Promise.all([addDossie]).then(function(result1)
    {
      if(where == 'submitReceptionSourceDossie')
      {
        var info = request.query("select id, ndos, dossie_name from u_options where area = 'Reception' and sub_type = 'source' order by ndos asc");
      }
      else if(where == 'submitReceptionDestinyDossie')
      {
        var info = request.query("select id, ndos, dossie_name from u_options where area='Reception' and sub_type = 'destiny' order by ndos asc");
      }
      else if(where == 'submitExpeditionSourceDossie')
      {
        var info = request.query("select id, ndos, dossie_name from u_options where area='Expedition' and sub_type = 'source' order by ndos asc");
      }
      else if(where == 'submitExpeditionDestinyDossie')
      {
        var info = request.query("select id, ndos, dossie_name from u_options where area='Expedition' and sub_type = 'destiny' order by ndos asc");
      }

      Promise.all([info]).then(function(result2)
      {
        res.render(view, {records: result2[0]});
      }).catch(function(err2)
      {
        console.log('Error storing dossies render');
        console.log(err2);
      });

    }).catch(function(err1)
    {
      console.log('Error storing dossies render');
      console.log(err1);
    });

  }).catch(function(err)
  {
    console.log("Error storing dossies");
    console.log(err);
  });

我认为这会解决您的问题。

答案 2 :(得分:0)

这里我们使用最大和第二大数字的反向数组排序

$result = array(1,5,19,200,999);
$count = count($result);
for($i=0;$i<$count;$i++){   
   for($j=$i+1;$j<$count;$j++){   
      if($result[$i]<$result[$j]){
        $m = $result[$i];
        $result[$i] = $result[$j];
        $result[$j] = $m;
      }
   }
 }
echo "Largest Number : ".(!empty($result[0])?$result[0]:FALSE)." Largest Second Number : ".(!empty($result[1])?$result[1]:FALSE);