x=int(input()) a=1 b=1 for i in range(1,x-1): if i%2==0:b+=a else:a+=b print(max(a,b))
#include <stdio.h> // 计算第 n 项的斐波那契数 int fibonacci_at(int n) { /* * f1 : 比喻为指针,一开始指向第一项数 * f2 : 比喻为指针,一开始指向第二项数 * result : 由 f1 和 f2 求和而来,即为第三项的数 * * 如在这样的斐波那契数列中:1, 1, 2, 3, 5, 8 ... * f1 和 f2 的指向为: f1 f2 */ int f1 = 1, f2 = 1, result = f1 + f2; if (n < 3) { return 1; } /* * 在这样的斐波那契数列中,只在计算第4项数才开始向右移动 f1 和 f2 * * 计算第3项:1, 1, 2, 3, 5, 8 ... * f1 f2 * 计算第4项:1, 1, 2, 3, 5, 8 ... * f1 f2 * 计算第5项:1, 1, 2, 3, 5, 8 ... * f1 f2 */ while (n-- > 3) { f1 = f2; f2 = result; result = f1 + f2; } return result; } int main() { int n = 0; scanf("%d", &n); printf("%d", fibonacci_at(n)); return 0; }
#include <stdio.h> int main() { int a, b,c=1,v1=1,v2=0; scanf("%d",&a); for(b=1;b<a;b++){ c=v1+v2; v2=v1; v1=c; } printf("%d",c); }
use std::io; fn main() { let mut stdin = String::new(); io::stdin() .read_line(&mut stdin) .expect(""); let mut n = stdin .trim() .parse() .expect(""); if n == 1 || n == 2 { println!("1"); } else { let (mut a, mut b) = (1, 1); for _ in 3..= n { let mut next = a + b; a = b; b = next; } println!("{}", b); } }