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 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码