zoukankan      html  css  js  c++  java
  • 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).
     
    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
    题解:打表找规律
    感受:将for循环里面的1000换成maxn,会wrong,想不通。将for换成while也会wrong,懵逼!!!!!!
     1 #pragma warning(disable:4996)
     2 #include<map>
     3 #include<string>
     4 #include<cstdio>
     5 #include<bitset>
     6 #include<cstring>
     7 #include<iostream>
     8 #include<algorithm>
     9 using namespace std;
    10 typedef long long ll;
    11 
    12 const int maxn = 1005;
    13 
    14 int a, b, n;
    15 int F[maxn];
    16 
    17 int main()
    18 {
    19     F[1] = F[2] = 1;
    20     while (scanf("%d%d%d", &a, &b, &n) != EOF) {
    21         if (!a && !b && !n) break;
    22         int k;
    23         for (k = 3; k < 1000; k++) {
    24             F[k] = (a*F[k - 1] + b * F[k - 2]) % 7;
    25             if (F[k] == 1 && F[k - 1] == 1) break;
    26         }
    27         n = n % (k - 2);
    28         F[0] = F[k - 2];
    29         printf("%d
    ", F[n]);
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    软件工程导论P53,习题2.4
    视图和数据表的区别
    无法从“object”转换为“string”
    Oracle 密码重置
    Struts2 上传下载
    Spring 事务管理
    JSP 指令和动作
    JS 禁用回车、后退事件、form 表单不可编辑
    关于 in 和 exist 的理解
    Oracle clob 操作函数
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/8620724.html
Copyright © 2011-2022 走看看