杭电ACM1005 Number Sequence
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/09/21 16:18:37
杭电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
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
/*
1 1 3
2
1 2 10
5
0 0 0
Press any key to continue
*/
#include<stdio.h>
int main(void) {
\x09int a1 = 1,a2 = 1,an;
\x09int A,B,n,i;
\x09while(1) {
\x09\x09scanf("%d%d%d",&A,&B,&n);
\x09\x09if(A == 0 && B == 0 && n == 0) break;
\x09\x09a1 = 1;
\x09\x09a2 = 1;
\x09\x09for(i = 3; i <= n; ++i) {
\x09\x09\x09an = (A * a2 + B * a1) % 7;
\x09\x09\x09a1 = a2;
\x09\x09\x09a2 = an;
\x09\x09}
\x09\x09printf("%d\n",an);
\x09}
\x09return 0;
}
再问: 似乎不优化一下会超时
再答: // 简单修改了下,再超时的话,我就没有个别更好的方法了。#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;
}
1 1 3
2
1 2 10
5
0 0 0
Press any key to continue
*/
#include<stdio.h>
int main(void) {
\x09int a1 = 1,a2 = 1,an;
\x09int A,B,n,i;
\x09while(1) {
\x09\x09scanf("%d%d%d",&A,&B,&n);
\x09\x09if(A == 0 && B == 0 && n == 0) break;
\x09\x09a1 = 1;
\x09\x09a2 = 1;
\x09\x09for(i = 3; i <= n; ++i) {
\x09\x09\x09an = (A * a2 + B * a1) % 7;
\x09\x09\x09a1 = a2;
\x09\x09\x09a2 = an;
\x09\x09}
\x09\x09printf("%d\n",an);
\x09}
\x09return 0;
}
再问: 似乎不优化一下会超时
再答: // 简单修改了下,再超时的话,我就没有个别更好的方法了。#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;
}
杭电acm1005的算法设计,分析下周期
为什么杭电acm1005周期是49呢
Number Sequence
杭电 ACM 小弟想知道为什么循环周期是49 呢,A number sequence is defined as fol
sequence number是什么意思
number-letter sequence是什么意思
What is the next number in this sequence?
What number comes next in this sequence:_______________ 9174
如何翻译:Sequence Number of predicted TMHs:2 特别翻译出TMHs的意思.
英语翻译这句子是The n(th) term of a number sequence is given by form
a sequence of number is like this:2,5,9,14,20,27...then the
一道AMC数学题The real number c,b,a form an arithmetic sequence wi