题目限制1S.你写递归一般就会超时,并且你的递归程序参数都不对.
倚天照海花无数,流水高山心自知。
#include <stdio.h>
int a, b;
int main(void)
{
long n;
long sum( long n);
while(scanf("%d%d%ld", &a, &b, &n) == 3&&(a != 0||b != 0||n != 0))
{
printf("%ld\n", sum(n));
}
return 0;
}
long sum( long n)
{
if(n == 1||n == 2)
return 1;
else
return (a*sum(n - 1) + b*sum(n - 2))%7;
}
别的方法还没有想到啊!
这个肯定会超时.
今天星期六,我把自己写的贴上来.
#include<stdio.h>
#define N 100
long count[N];
long is_in(long a[],long n)
{
long j;
for(j=n-1;j>=2;j--)
if(a[j]==a[n]&&a[j-1]==a[n-1])
return(n-j);
return(0);
}
int main()
{
#ifdef ONLINE_JUDGE
freopen ("Number Sequence.txt","r",stdin);
#endif
long n;
long a,b,i,t,d;
while(EOF!=(scanf("%ld%ld%ld",&a,&b,&n))&&!(a==0&&b==0&&n==0))
{
i=3;
count[1]=1;
count[2]=1;
while(1)
{
count[i]=(a*count[i-1]+b*count[i-2])%7;
t=is_in(count,i);
if(t!=0)
break;
i++;
}
i=i-t-1;
if(i>n)
printf("%ld\n",count[n]);
else
{
d=(n-i)%t;
if(d==0)
d=d+t;
d=d+i;
printf("%ld\n",count[d]);
}
}
return(0);
}
steps
#include<stdio.h>
int main()
{
#ifndef ONLINE_JUDGE
freopen ("steps.txt","r",stdin);
#endif
long n1,n2,sum;
int i,j,flag;
while(EOF!=(scanf("%ld%ld",&n1,&n2)))
{
i=1;
sum=0;
flag=0;
j=0;
while((n2-n1)>sum)
{
sum=sum+i;
flag++;
if(flag==2)
{
i++;
flag=0;
}
j++;
}
printf("%d\n",j);
}
return(0);
}