> 其他 >
杭电ACM1005 Number Sequence
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
人气:485 ℃ 时间:2020-06-16 02:25:29
解答
/*1 1 321 2 1050 0 0Press any key to continue*/#include<stdio.h>int main(void) {\x09int a1 = 1,a2 = 1,an;\x09int A,B,n,i;\x09while(1) {\x09\x09scan...似乎不优化一下会超时

// 简单修改了下,再超时的话,我就没有个别更好的方法了。

#include<stdio.h>
 
int main(void) {
int A,B,n,i,an_1,an_2,an;
while(scanf("%d%d%d",&A,&B,&n) == 3 && n) {
an_1 = 1;
an_2 = 1;
for(i = 3; i <= n; ++i) {
an = (A * an_2 + B * an_1) % 7;
an_1 = an_2;
an_2 = an;
}
printf("%d\n",an);
}
    return 0;
}
推荐
猜你喜欢
© 2024 79432.Com All Rights Reserved.
电脑版|手机版