解释一下这段代码?

时间:2018-02-09 03:17:50

标签: c++ fibonacci

我是编程新手。我无法找出输出为什么的原因 awk -v start=3 -v end=5 ' ##Mentioning variables named start and end where start is denoting the starting line and end is denoting end line which we want to print. FNR>=start && FNR<=end{ ##Checking condition here if variable FNR(awk out of the box variable) value is greater than or equal to variable start AND FNR value is less than or equal to end variable. If condition is TRUE then do following: for(;NF>0;NF--){ ##Initiating a for loop which starts from value of NF(Number of fields, which is out of the box variable of awk) and it runs till NF is 0. printf("%s%s",$NF,NF==1?RS:FS)} ##Printing value of NF(value of field) and other string will be either space of new line(by checking when field is first then print new line as print space). } ' Input_file ##Mentioning Input_file name here.

不应该是0 1 1 2 3 5 8 13 21 34 55等吗?

首先,0 1 1 3 5 7因此打印i=0,第二0 (because return 0)打印i=1,第三1 (because return 1)打印i=2,第四1 because (2-1)+(2-2)=1所以打印i=3等..

原谅我这个非常基本的问题。也许编程不适合我

3 because (3-1)+(3-2)=3

2 个答案:

答案 0 :(得分:1)

显然,

fib(0) = 0,
fib(1) = 1,
fib(2) = fib(1) + fib(0)  = 1,
fib(3) = fib(2) + fib(1)
       = fib(1) + fib(0) + fib(1)
       = 1 + 0 + 1
       = 2
etc...

每个人都会犯错,特别是开始新的错误,你需要耐心等待,你会发现兴趣

答案 1 :(得分:0)

您的代码有效,fib(0)为0,fib(1)也为0,其他所有内容都会添加前两个值。这意味着要打印0 0 1 1 2 3 5 8等。如果你对于为什么用fib(2),fib(3)等绘制树(二叉树)与fib(n-1)和fib(n-2)作为子节点fib(n)的节点继续运行直到你遇到基本情况,然后添加父节点的子节点的值并将其值分配给该父节点并继续向上直到你到达根。希望这会有所帮助。