既然102位就会出现循环节,所以精确的话就开到102大小了
回复 31楼 czz5242199
恩,谢谢啊
程序代码:#include <stdio.h>
int num[30] = {0};
int pre, mod;
void construct(int y[], int a, int b);
int main(void)
{
int a, b, n, T, m;
scanf("%d", &T);
int i = 1;
while (i <= T)
{
scanf("%d%d%d", &a, &b, &n);
if (a ==0 && b == 0)
{
printf("Case #%d: %d\n", i, 0);
}
else
{
construct(num, a, b);
if (mod)
{
if (n <= pre)
{
printf("Case #%d: %d\n", i, num[n - 1]);
}
else
{
m = (n - pre) % 10 ? (n - pre) % 10 : 10;
printf("Case #%d: %d\n", i, num[m + pre - 1]);
}
}
else
{
if (n <= pre)
{
printf("Case #%d: %d\n", i, num[n - 1]);
}
else
{
m = (n - pre) % 4 ? (n - pre) % 4 : 4;
printf("Case #%d: %d\n", i, num[m + pre - 1]);
}
}
}
i++;
}
return 0;
}
void construct(int y[], int a, int b)
{
int i = 0, c, d;
y[0] = a;
y[1] = b;
while (1)
{
if (i >= 28) break;
c = (y[i] + y[i+1]) / 10;
d = (y[i] + y[i+1]) % 10;
if (c == 0)
{
y[i+2] = d;
i++;
}
else
{
y[i+2] = c;
y[i+3] = d;
i += 2;
}
}
for (i = 0; i < 30; i++)
{
if (y[i] == 1 && y[i+1] == 1)
{
pre = i;
mod = 1;
break;
}
if (y[i] == 1 && y[i+1] == 4)
{
pre = i;
mod = 0;
break;
}
}
}
在少数量的测试还是能通过 但是现在提交过期了 没办法验证啊。
程序代码:#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100
int Value[MaxSize]={0};
int getValue(int a, int b, int n)
{
int i,j,sum;
i=0;j=1;
if(a==0&&b==0) return 0;
else
{
Value[0]=a;Value[1]=b;
while(j<n)
{
if((sum=Value[i]+Value[j])>9)
{
i+=2;j+=2;
Value[i]=1;
Value[j]=sum%10;
}
else
{
i+=1;j+=1;
Value[j]=sum;
}
}
}
return (j<=MaxSize)?Value[j]:-1;
}
int main()
{
int x,a,b,n;
scanf("%d",&x);
while(x--)
{
scanf("%d%d%d",&a,&b,&n);
if(a>9||b>9) exit(-1);
printf("Case #%d: %d\n",x+1,getValue(a,b,n-1));
}
return 0;
}//////////这只是n很小的时候使用,题中n<10^9... = =!这个很难啊。
///////////考虑归纳法:getValue(a,b,n)=gteValue(a,b,n-1)+getValue(a,b,n-2)[sum<10] | getValue(a,b,n)=1[sum>=10];[n>=2]
//////////直接写递归代码肯定不行,效率太差了;在数学上可以把 递归式 变成 通项公式,可是 我不会变啊!(可以参考 斐波那契一般式的推导)= =!拙计了。。。