| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 631 人关注过本帖
标题:函数递归调用里的问题
取消只看楼主 加入收藏
shuimu10
Rank: 2
等 级:论坛游民
帖 子:53
专家分:48
注 册:2010-11-19
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:1 
函数递归调用里的问题
函数递归调用时 出现比如 f(n)=Af(n-1)+Bf(n-2)的时候 其中A 和B是主函数里输入的参数  要怎么在定义f(n)函数时把A B的值引入
2010-12-23 22:18
shuimu10
Rank: 2
等 级:论坛游民
帖 子:53
专家分:48
注 册:2010-11-19
收藏
得分:0 
Problem Description
A number sequence is defined as follows:

f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

Given A, B, and n, you are to calculate the value of f(n).

 

Input
The input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

 

Output
For each test case, print the value of f(n) on a single line.

 

Sample Input
1 1 3
1 2 10
0 0 0
 

Sample Output
2
5
 
这个是原题
#include <stdio.h>
int main()
{
    int A,B,s;
    long n;
    int f(int r,int t,long w);
    while(scanf("%d%d%ld",&A,&B,&n)!=EOF)
    {  if(A==0&&B==0&&n==0)
          break;
       else
       {
           s=f(A,B,n);
              printf("%ld\n",s);
       }
    }
    return 0;
}
int f(int r,int t,long w)
{
    int e;
    if(w==1||w==2)
        e=1;
    else
        e=(r*f(r,t,w-1)+t*f(r,t,w-2))%7;

    return(e);
}
这个是我写的代码  运行结果是 运行时间超时  什么栈溢出的  表示不理解
2010-12-25 21:54
快速回复:函数递归调用里的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016469 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved