如何使用 PHP 解决此问题陈述?

假设给出一个正整数类型的数字,例如:312。请帮我用 PHP 编写一个程序,将给定的数字转换为具有相同位数的新数字,并且新数字的所有数字必须相等到给定数字的任何数字(例如:333、111、222,每个数字一次递减或递增 1)。但只打印生成该序列所需步骤数较少的数字序列,并打印生成该序列所需的步骤数。


解释:输入:一个正整数 N(例如:312)


将数字 (312) 转换为 3 的序列


3 2 2


3 3 2


3 3 3


这里的步骤数 = 3


现在,将数字(312)转换为 1 的序列


2 1 2


1 1 2


1 1 1


这里的步骤数 = 3


最后将数字(312)转换为2的序列


2 1 2


2 2 2


这里的步骤数 = 2


所以,输出:222


步数:2


这是我尝试过但失败的方法


<?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

?>


慕容708150
浏览 128回答 3
3回答

收到一只叮咚

<?phpclass SeqSolver{&nbsp; &nbsp; public function solve($str_num)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if(!ctype_digit($str_num))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Exception('Invalid input.&nbsp; Input string must contain digits between 0 and 9 only.');&nbsp; &nbsp; &nbsp; &nbsp; $digits = str_split($str_num);&nbsp; &nbsp; &nbsp; &nbsp; $length = count($digits);&nbsp; &nbsp; &nbsp; &nbsp; foreach(array_unique($digits) as $digit)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $results[$digit] = $this->stepsToSequence($str_num, $digit);&nbsp; &nbsp; &nbsp; &nbsp; //var_export($results);&nbsp; &nbsp; &nbsp; &nbsp; $min_keys = array_keys($results, min($results));&nbsp; &nbsp; &nbsp; &nbsp; // Prepare result&nbsp; &nbsp; &nbsp; &nbsp; $result['input'] = $str_num;&nbsp; &nbsp; &nbsp; &nbsp; foreach($min_keys as $key)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $result['solutions'][] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'sequence' => str_repeat($key, $length),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'steps'&nbsp; &nbsp; => $results[$key]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; return $result;&nbsp; &nbsp; }&nbsp; &nbsp; public function stepsToSequence($str_num, $target_digit) {&nbsp; &nbsp; &nbsp; &nbsp; $digits = str_split($str_num);&nbsp; &nbsp; &nbsp; &nbsp; $steps&nbsp; = 0;&nbsp; &nbsp; &nbsp; &nbsp; foreach($digits as $digit)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $steps += abs($digit - $target_digit);&nbsp; &nbsp; &nbsp; &nbsp; return $steps;&nbsp; &nbsp; }}使用示例:$solver = new SeqSolver;foreach(['312', '334', '39'] as $input) {&nbsp; &nbsp; $result = $solver->solve($input);&nbsp; &nbsp; var_export($result);&nbsp; &nbsp; echo "\n";}输出:array (&nbsp; 'input' => '312',&nbsp; 'solutions' =>&nbsp;&nbsp; array (&nbsp; &nbsp; 0 =>&nbsp;&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; 'sequence' => '222',&nbsp; &nbsp; &nbsp; 'steps' => 2,&nbsp; &nbsp; ),&nbsp; ),)array (&nbsp; 'input' => '334',&nbsp; 'solutions' =>&nbsp;&nbsp; array (&nbsp; &nbsp; 0 =>&nbsp;&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; 'sequence' => '333',&nbsp; &nbsp; &nbsp; 'steps' => 1,&nbsp; &nbsp; ),&nbsp; ),)array (&nbsp; 'input' => '39',&nbsp; 'solutions' =>&nbsp;&nbsp; array (&nbsp; &nbsp; 0 =>&nbsp;&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; 'sequence' => '33',&nbsp; &nbsp; &nbsp; 'steps' => 6,&nbsp; &nbsp; ),&nbsp; &nbsp; 1 =>&nbsp;&nbsp; &nbsp; array (&nbsp; &nbsp; &nbsp; 'sequence' => '99',&nbsp; &nbsp; &nbsp; 'steps' => 6,&nbsp; &nbsp; ),&nbsp; ),)

慕标琳琳

这有效(根据我非常快速的测试)):&nbsp;$intIn = 312;# function changeDigits( $intIn ) { // uncomment for function&nbsp; $digits = str_split( $intIn ); // convert to array of digits&nbsp; $numerOfDigits = count($digits);&nbsp; $numberOfSteps = array();&nbsp;# check each digit in number&nbsp;for ($i=0; $i < $numerOfDigits; $i++) {&nbsp; &nbsp; $numberOfSteps[$i] = 0;&nbsp; &nbsp; $currentDigit = $digits[$i];&nbsp; &nbsp; # count the number of inc/decrements to change the other digits to this digit&nbsp; &nbsp; foreach($digits as $otherDigit) {&nbsp; &nbsp; &nbsp;if ($currentDigit > $otherDigit) $numberOfSteps[$i] += $currentDigit - $otherDigit;&nbsp; &nbsp; &nbsp;if ($currentDigit < $otherDigit) $numberOfSteps[$i] += $otherDigit - $currentDigit;&nbsp; &nbsp; }&nbsp; }&nbsp; $digitKey = array_search( min($numberOfSteps), $numberOfSteps );&nbsp; echo 'Number of Steps: ' . $numberOfSteps[$digitKey] . PHP_EOL;&nbsp; // (or '<br>')&nbsp; echo 'New number = ' . str_repeat( $digits[$digitKey], $numerOfDigits );&nbsp;#}# changeDigits(312);
打开App,查看更多内容
随时随地看视频慕课网APP