dongtuo6562 2019-07-13 03: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 05: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 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
  • ¥15 IEC61850 客户端和服务端的通讯机制
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部