數學 × 程式編寫比賽 (第四回)之我也试一下[请指正]Mathematics × Programming Competition #4 I've try [please correct!]

@kenchung/question-mathematics-programming-competition-4
@kenchung 发起的
对我来说挺难的,但我小试了一下。结果肯定是对的,就是不知道我这个效率怎么样。弄了一夜也没弄明白面向对象怎么用,我怀疑PHP类的方法不支持递归的调用。索性就还是面向对向吧。

It's very difficult for me, but I've try it. The answer is correct, just don't know what about the efficiency of the code. I study the method of OO for the whole night but I failed, I doubt the class methods of PHP don't support recursive invocation. The code is PO. Please correct!

<?php
function go($from_x=0, $from_y=0, $target_x, $target_y)
{
global $cnt;
if($from_x>$target_x || $from_y>$target_y)
{
return; //如果起点超过终点,则返回
}
if($from_x$target_x && $from_y$target_y)
{
$cnt;
return;
}
go($from_x+1,$from_y,$target_x,$target_y); //向东移动,递归调用
go($from_x,$from_y+1,$target_x,$target_y); //向北移动,递归调用
return $cnt;
}
global $cnt;
$cnt=0;
$atob=go(0,0,3,4);
$cnt=0;
$btoc=go(3,4,8,9);
echo $atob*$btoc;
?>
111.png
@justyy 的帮助下,修改了一下:with the help of @justyy I correct:
<?php
function go($from_x=0, $from_y=0, $target_x, $target_y)
{
global $cnt;
if($from_x$target_x || $from_y$target_y)
{
$cnt;
return;
}
go($from_x+1,$from_y,$target_x,$target_y); //向东移动,递归调用
go($from_x,$from_y+1,$target_x,$target_y); //向北移动,递归调用
return $cnt;
}
global $cnt;
$cnt=0;
$atob=go(0,0,3,4);
$cnt=0;
$btoc=go(3,4,8,9);
echo $atob*$btoc;
?>

反正只要横坐标到3或纵坐标到4,就只剩下死路一条了哈哈哈,不需要非得走到B点了,我果然是浪费了效率哈哈。至于更复杂的我暂时还没有整明白。。。至于我这个坐标的问题,多两个变量吧,这年月多两个变量应该不是事,实 际操作我觉得还是明白点比较好,为以后修改代码和维护做好准备。。习惯整得好分辩一些吼吼。
突然发下我编辑器里的缩进都没了。。我忍!不想翻译了,看代码即可懂。。。

@kenchung 给出了用数学归纳的方法来解答,有图有真相:
@jubi/kenchung
@justyy 给出了数学排列组合的方法和三种编程思想,一种比一种更高效:
@justyy/software-engineer-interview-question-how-many-ways-from-a-to-c-via-b-a-b-c
steemit_tvb_logo.png

近期文章Recent Blog

Software Engineer Interview Question - How Many Ways from A to C via B? 从A经B到C共有多少条路径 程序解决数学问题之我也试一下
藏头诗:七绝-精彩瞬间(中华新韵)Acrostic Poem of Wonderful Moment
Software Engineer Interview Question - How Many Ways from A to C via B? 从A经B到C共有多少条路径 程序解决数学问题之我也试一下
写给36岁的女性伪程序员的我 To 36-years-myself as a female quasi-programmer
20170809习联:对联是一种中国古典语言艺术形式 Couplet is an artistic form of Chinese classical language
第一次绝恋--最后的初恋 The first but also last love
填词一首:桂殿秋《俺是猪》博君一笑 Write a poem call "I am a pig"

H2
H3
H4
3 columns
2 columns
1 column
9 Comments