#include <iostream> #include <cassert> using namespace std; ; char a[max_len]; void topK(int n, int m, int& k, int i) { || i >= m) return; ; j < ; j++) { a[i] = j + '; a[i+] = '\0'; int v = atoi(a); && v <= n && k…
class Fib(object): def __call__(self,n): a=[0,1] for i in range(n-2): an=a[-2]+a[-1] a.append(an) return a f = Fib()print f(10) class Fib(object): def __call__(self, num): a, b, L = 0, 1, [] for n in rang…
1,2,3,5,8,13..... 求第n个数 def get_num(n): if n == 1: return 1 if n == 2: return 2 return get_num(n - 1) + get_num(n - 2) def get_num(n): if n == 1: return 1 if n == 2: return 2 a = 1 b = 2 for i in range(n - 2): a, b = b, a + b return b分为递归和循环两个求法…
后一个分数的分子=前一个分数的分子+分母,后一个分数的分母=前一个分数的分子,循环个20次就有结果.注意,假设分子为a,分母为b,虽然 a = a + b, 但此时a已经变成 a+b 了,所以再给b重新赋值的时候,得是 (a+b)-b 才能等于原分母b,所以重新赋值时就得写成 a-b 方法一 from fractions import Fraction def fibonacci(n): a, b = 1, 2 res = [1] i = 1 while i < n: a, b = b, a+b…
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2719 Accepted Submission(s): 1386 Problem Description Easy question! Calculate how many primes between [1...n]! Input Each line…
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha…
public class Fibonacii{ public int m1(int n){ if(n == 1||n == 2){ return 1; } return m1(n-1) + m1(n-2); } public int m2(int n){ if(n == 1||n == 2){ return 1; } int i = 1; int j = 1; int result = 0; for(int count=3;count<=n;count++){ result = i + j; i…
* 用数组 function getTopN(a, n) { function _cloneArray(aa) { var n = aa.length, a = new Array(n); for (var i = 0; i < n; i++) { a[i] = aa[i]; } return a; } function _getMaxElem(a) { if (a.length === 0) throw "empty array"; if (a.length === 1) re…