基础编程题之牛客网星际密码
文章目錄
- 題目
- 解題思路
- 代碼
題目
???br />
解題思路
本題的基本意思就是給你給定一個矩陣:(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?),然后一個數(shù)n,n表示矩陣(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?)的n次方,這個n代表一個數(shù)也就是解密的結(jié)果,即為(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?)n的結(jié)果的(也是一個矩陣)的左上角數(shù)字。
如果該數(shù)字小于4就用0補充,如果大于4就只輸出最后4位
同時矩陣的乘法公式如下:
所以先代入前幾個數(shù),可以發(fā)現(xiàn)如下規(guī)律
當(dāng)n=1時,左上角值=1
(1110)\begin{pmatrix} 1 & 1 \\ 1 & 0 \end{pmatrix}(11?10?)
當(dāng)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?)
當(dāng)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?)
當(dāng)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?)
當(dāng)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?)
當(dāng)n=6時,左上角的值=?
····
····
····
可以發(fā)現(xiàn)是很明顯的斐波那契數(shù)列
代碼
// 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;//數(shù)據(jù)個數(shù)vector<int> fib={1,1};//斐波那契數(shù)列Fib(fib);//初始化while(cin >> number){int temp;while(number--){cin >> temp;printf("%04d",fib[temp]);}cout<<endl;}return 0; }總結(jié)
以上是生活随笔為你收集整理的基础编程题之牛客网星际密码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: easyui from 缓存问题处理
- 下一篇: 翻转二叉树: