dongtuo6562 2019-07-13 11:02
浏览 64
已采纳

如何使用PHP解决此问题语句?

Suppose a number is given which is of positive integer type, e.g: 312. Please help me to write a program in PHP which convert that given number to a new number having same number of digits and all the digits of the new number must be equal to any of the digits of the given number (e.g: 333, 111, 222 by either decrementing or incrementing each digit by 1 at a time only). But print only that sequence of digits which takes lesser number of steps to generate the sequence and also print the number of steps taken to generate that sequence.

Explanation: Input: A positive integer N (e.g: 312)

converting the number(312) to the sequence of 3

3 2 2

3 3 2

3 3 3

here number of steps = 3

Now, converting the number(312) to the sequence of 1

2 1 2

1 1 2

1 1 1

here number of steps = 3

and finally converting the number(312) to the sequence of 2

2 1 2

2 2 2

here number of steps = 2

So, Output: 222

Number of steps: 2

Here's what I tried but lost

<?php

$num = 312;
$arr_num = array_map('intval', str_split($num));

//steps taken for each sequence will be stored in this array
$steps = array();

//printing number
for($i = 0; $i < count($arr_num); $i++)
    echo $arr_num[$i];


//calculation
for($i = 0; $i < count($arr_num); $i++) {
    $count = 0;

    for($j = 0; $j < count($arr_num); $j++) {

        if($arr_num[$i] == $arr_num[$j])
            ++$j;

        elseif($arr_num[$i] > $arr_num[$j]) {

            while($arr_num[$j] != $arr[$i]) {
                $arr_num[$j] += 1;
                $count++;
            }
        }

        else {
            while($arr_num[$j] != $arr_num[$i]) {
                $arr_num[$j] -= 1;
                $count++;
            }
        }
    }
    //pushing the count to steps array for each sequence
    array_push($steps, $count);

}
//I am stuck here...can't find the further solution
?>
  • 写回答

4条回答 默认 最新

  • douzhuolong9886 2019-07-13 13:04
    关注

    This works (according to my very quick testing)):

     $intIn = 312;
    
    # function changeDigits( $intIn ) { // uncomment for function
      $digits = str_split( $intIn ); // convert to array of digits
      $numerOfDigits = count($digits);
      $numberOfSteps = array();
    
     # check each digit in number
     for ($i=0; $i < $numerOfDigits; $i++) {
        $numberOfSteps[$i] = 0;
        $currentDigit = $digits[$i];
    
        # count the number of inc/decrements to change the other digits to this digit
        foreach($digits as $otherDigit) {
         if ($currentDigit > $otherDigit) $numberOfSteps[$i] += $currentDigit - $otherDigit;
         if ($currentDigit < $otherDigit) $numberOfSteps[$i] += $otherDigit - $currentDigit;
        }
      }
      $digitKey = array_search( min($numberOfSteps), $numberOfSteps );
      echo 'Number of Steps: ' . $numberOfSteps[$digitKey] . PHP_EOL;  // (or '<br>')
      echo 'New number = ' . str_repeat( $digits[$digitKey], $numerOfDigits );
     #}
    
    # changeDigits(312);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。