Fibonacci Again

Problem Description
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
 

 

Input
Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
 

 

Output
Print the word "yes" if 3 divide evenly into F(n).

Print the word "no" if not.
 

 

Sample Input
  
  
0 1 2 3 4 5
 

 

Sample Output
  
  
no no yes no no no
 

 

Author
Leojay
 

 

Recommend
JGShining
答案:
找规律。。。

int main()
{
 int n;
 while (scanf("%d", &n) != EOF)
 {
  if (n < 0)
   break;
  if (n == 0 || n == 1)
   printf("no/n");
  else
  {
   if ((n-2)%4 == 0)
    printf("yes/n");
   else
    printf("no/n");  
  }
 }
 
 return 0;
}
RV321并不是一种常见的处理器架构名称,可能是您想说的是RISC-V 32位指令集架构下的某个实现或芯片型号。在这里我们假设您是指基于 RISC-V 架构的处理器,并讨论如何在其上编写斐波那契数列。 **在RISC-V汇编语言中实现斐波那契函数:** 下面是一个简单的RISC-V 汇编程序示例,用于计算第 n 项斐波那契数值: ```asm # Fibonacci in RISCV Assembly .text main: li a0, 5 # Set n = 5 (example input) jal ra, fib # Call the fibonacci function with parameter in a0 and return value also stored to a0. mv t0, a0 # Move result from a0 into temp register for printing later. addi sp,sp,-4 # Reserve space on stack. sw t0, 0(sp) # Store our fibonacci number at address pointed by sp. li a7, 9 # System call code for write string - not used here but shown as example of syscalls. ecall # Make system call lw a0, 0(sp) # Load int back again before print syscall can use it. li a7, 1 # Syscall to 'print integer'. ecall # Print out that answer! exit: li a7, 10 # Exit program gracefully via ECALL ecall fib: bgtz a0, L1 # if(a0 > 0) goto L1 else return directly. xor v0,v0,v0 # If zero then set final output=zero. This is first element F(0). ret # Return immediately since we know this special case already computed above. L1: blt a0,2,Lbasecase # Check whether current argument equals either base cases explicitly handled below: Lrecurse: sub a1,a0,1 # Compute previous term index i.e.,a[i−1]. jal ra,fib # Recursively compute Fib(i–1) using tail recursion optimization where possible.. mv t1,v0 # Save intermediate result fib(n-1). sub a0,a0,2 # Decrease counter twice now because both terms needed were just found recursively. jal ra,fib # Now calculate second last required member f(n – 2). add v0,t1,v0 # Add two members together to get next one finally returned up chain. jr ra # Jump back once done adding everything up properly. Lbasecase: beq a0,1,F_one_base_case # Directly compare against expected single match only allowed within these bounds here... F_zero_base_case: li v0,0 # Zero itself represents Base Case Number One Explicit Value Known A Priori So Just Assign It Immediately Then Done! j end_of_fibonacci_function F_one_base_case: li v0,1 # First Non-Zero Term Defined As Exactly Equal To Unit Quantity Also Trivial Therefore Hardcoded Same Way Here Too Simpler Code Wins Again Hooray For Us All Right?? end_of_fibonacci_function: ``` 上述代码展示了递归版本的斐波那契算法,在实际应用时可以考虑优化成非递归形式以提高效率并减少栈空间消耗。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值