hdu1005(规律)

来源:互联网 发布:在淘宝上怎样代理1688 编辑:程序博客网 时间:2024/04/28 12:37

Runtime Error(INTEGER_DIVIDE_BY_ZERO)代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

int a,b,n,c;
int s[50005];

int main()
{
    while(~scanf("%d%d%d",&a,&b,&n)&&(a||b||n)){
        memset(s,0,sizeof(s));
        s[1]=s[2]=1;
        for(int i = 3; i < 10000; i++){
            s[i]=(a*s[i-1]+b*s[i-2])%7;
            if(s[i]==1&&s[i-1]==1){
                c=i-2;
                break;
            }
        }
        n%=c;
        if(n==0)
            printf("%d\n",s[c]);
        else
            printf("%d\n",s[n]);
    }
    return 0;
}


AC代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>

using namespace std;

int a,b,n,c;
int s[50005];

int main(){
    while(~scanf("%d%d%d",&a,&b,&n)&&(a||b||n)){
        memset(s,0,sizeof(s));
        s[1]=s[2]=1;
        int i;
        for(i = 3; i < 10000; i++){
            s[i]=(a*s[i-1]+b*s[i-2])%7;
            if(s[i]==1&&s[i-1]==1){
                break;
            }
        }
        c=i-2;
        n%=c;
        if(n==0)
            printf("%d\n",s[c]);
        else
            printf("%d\n",s[n]);
    }
    return 0;
}

0 0
原创粉丝点击