首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

杭电1005求相助,路过的都看看啊

2013-04-20 
杭电1005求帮助,路过的都看看啊!#include stdio.h#include stdlib.h/*******************************

杭电1005求帮助,路过的都看看啊!

#include <stdio.h>
#include <stdlib.h>

/*************************************
Problem 1005
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).
*************************************/
int func(int a,int b,int f_n_1,int f_n_2)
{
    /* Parameter:a=A,b=B,f_n_1=f(n - 1) ,f_n_2=f(n - 2)*/
    return (a*f_n_1+b*f_n_2)%7;
}

int main()
{
    int i,j,a,b,temp;
    int n;
    int mod[51]= {1,1};

    while(scanf("%d%d%d", &a, &b, &n) != EOF && (a || b || n))
    {
        for(j=0; j<47; j++)
        {
            mod[j+2]=func(a,b,mod[j+1],mod[j]);
        }
        for(i=1; i<49; i++)
        {
            if(mod[i]==mod[0]&&mod[i+1]==mod[1])
            {
                temp=i;
                break;
            }
        }
        printf("%d\n",mod[(n-1)%temp]);
    }
    return 0;
}

Runtime Error (ACCESS_VIOLATION)错误,查了半天不知道哪出错了,结果是对的,好像是越界,但是不知道哪错了,AC不了,求帮助!!! 杭电1005 c 算法
[解决办法]
循环节可能不包括1,1。这时候你的temp未初始化,(n-1)%temp越界。

热点排行