hdu 1005 Number Sequence
Problem Description
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).
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, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.
Output
For each test case, print the value of f(n) on a single line.
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2
5
题目要求 就是f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) %7问你给你a,b,输出f(n);
因为n的范围不能直接递归,这是一个循环的解Ps题目测试数据有问题,真正意义上的ac代码不能过;
ac代码
1 #include<stdio.h> 2 #include<string.h> 3 4 int a,b; 5 int fun(int n) 6 { 7 if(n==1) 8 return 1; 9 else if(n==2) 10 return 1; 11 else 12 return (a*fun(n-1)+b*fun(n-2))%7; 13 } 14 15 int main() 16 { 17 int n; 18 while(scanf("%d %d %d",&a,&b,&n)!=EOF&&(a!=0||b!=0||n!=0)) 19 { 20 21 printf("%d\n",fun(n%49)); 22 } 23 }
实际情况的测试代码
#include<iostream> #include<cstring> using namespace std; int x[100]; int main() { int a,b,n,i,k; while(cin>>a>>b>>n,a||b||n) { memset(x,0,sizeof(x)); x[1]=1,x[2]=1; for(i=3;i<60;i++) { x[i]=(a*x[i-1]+b*x[i-2])%7; } for(i=1;i<60;i++) cout<<i<<' '<<x[i]<<endl; for(i=3;i<60;i++) if(x[i]==1&&x[i+1]==1) break; k=i-1; if(n%k==0) cout<<x[k]<<endl; else cout<<x[n%k]<<endl; cout<<k<<endl; } return 0; }
ac不带表你程序怎么样只代表这道题目不混过去了,只有写出真正解决实际问题的代码才是好代码
ps只是给你现实应该出现的数据

浙公网安备 33010602011771号