基础编程题之牛客网星际密码
文章目錄
- 題目
- 解題思路
- 代碼
題目
牛客
解題思路
本題的基本意思就是給你給定一個矩陣:(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?),然后一個數n,n表示矩陣(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?)的n次方,這個n代表一個數也就是解密的結果,即為(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?)n的結果的(也是一個矩陣)的左上角數字。
如果該數字小于4就用0補充,如果大于4就只輸出最后4位
同時矩陣的乘法公式如下:
所以先代入前幾個數,可以發現如下規律
當n=1時,左上角值=1
(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?)
當n=2時,左上角值=2
(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?) * (1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?) =(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?)
當n=3時,左上角值=3
(2111)\begin{pmatrix} 2 & 1 \\ 1 & 1 \end{pmatrix}(21?11?) * (1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?) = (3221)\begin{pmatrix} 3 & 2 \\ 2 & 1 \end{pmatrix}(32?21?)
當n=4時,左上角值=4
(3221)\begin{pmatrix} 3 & 2 \\ 2 & 1 \end{pmatrix}(32?21?) * (1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?) = (5332)\begin{pmatrix} 5 & 3 \\ 3 & 2 \end{pmatrix}(53?32?)
當n=5時,左上角的值=8
(5332)\begin{pmatrix} 5 & 3 \\ 3 & 2 \end{pmatrix}(53?32?) * (1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?) = (8553)\begin{pmatrix} 8 & 5 \\ 5 & 3 \end{pmatrix}(85?53?)
當n=6時,左上角的值=?
····
····
····
可以發現是很明顯的斐波那契數列
代碼
// write your code here cpp #include <iostream> #include <vector> #include <cstdio> using namespace std;void Fib(vector<int>& fib) {for(int i=2 ;i < 10001;i++){fib.push_back((fib[i-1]+fib[i-2])%10000);} } int main() {int number=0;//數據個數vector<int> fib={1,1};//斐波那契數列Fib(fib);//初始化while(cin >> number){int temp;while(number--){cin >> temp;printf("%04d",fib[temp]);}cout<<endl;}return 0; }總結
以上是生活随笔為你收集整理的基础编程题之牛客网星际密码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: easyui from 缓存问题处理
- 下一篇: 翻转二叉树: