题目
题目链接:
https://www.nowcoder.com/practice/6a3dfb5be4544381908529dc678ca6dd
思路
斐波那契数列
参考答案Java
import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param first int整型* @param second int整型* @param n int整型* @return int整型*/public int findNthValue (int first, int second, int n) {//就是斐波那契数列if (n == 1) return first;if (n == 2) return second;int idx = 3;while (idx <= n) {int sum = first + second;first = second;second = sum;idx++;}return second;}
}
参考答案Go
package main/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param first int整型* @param second int整型* @param n int整型* @return int整型*/
func findNthValue(first int, second int, n int) int {//就是斐波那契数列if n == 1 {return first}if n == 2 {return second}idx := 3for idx <= n {sum := first + secondfirst = secondsecond = sumidx++}return second
}
参考答案PHP
<?php/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param first int整型 * @param second int整型 * @param n int整型 * @return int整型*/
function findNthValue( $first , $second , $n )
{//就是斐波那契数列if($n ==1) return $first;if($n ==2) return $second;$idx = 3;while ($idx<=$n){$sum = $first+$second;$first=$second;$second=$sum;$idx++;}return $second;// write code here
}