牛客网——C列一列
鏈接:https://www.nowcoder.net/acm/contest/71/C
來源:牛客網
題目描述
小W在計算一個數列{An},其中A1=1,A2=2,An+2=An+1+An。盡管他計算非常精準,但很快他就弄混了自己的草稿紙,他找出了一些他計算的結果,但他忘記了這些都是數列中的第幾項。輸入描述:
每行包括數列中的一項Ak(k<=100000)。
總行數T<=30。
輸出描述:
對于每一項Ak,輸出一行包括一個正整數k表示輸入中數是數列的第幾項。
示例1輸入
2 3 5 8 13輸出
2 3 4 5 6 #include<bits/stdc++.h> using namespace std; #define ull unsigned long long typedef long long ll;const int inf = 0x3f3f3f3f; const int moder = 1e9 + 7; const int MAXN=1000010; ull a[MAXN]; int main() {a[1] = 1;a[2] = 2;for(int i=3;i < MAXN;i++) a[i] = a[i-1] + a[i-2];char str[MAXN];while(~scanf("%s",str)){ll len = strlen(str);ull B = 0;for(int i=0;i < len;i++) B = B*10 + (str[i]-'0');for(int i=1;i < MAXN;i++){if(a[i] == B){cout << i << endl;break;}}}return 0; }用map方便查找
#include<bits/stdc++.h> using namespace std; #define ull unsigned long long typedef long long ll;const int inf = 0x3f3f3f3f; const int moder = 1e9 + 7; const int MAXN=1000010; ll a[MAXN]; map<ll,int> mm; int main() {a[1] = 1,a[2] = 2;mm[1] = 1,mm[2] = 2;for( int i=3; i<=MAXN; i++ ) //打表 {a[i] = a[i-1] + a[i-2];mm[a[i]] = i; //用map使值和對應的位置形成鍵值對方便查找 }string s;while( cin >> s ) {ll sum = 0;for( int i=0; i<s.length(); i++ ) {sum = sum * 10 + s[i] - '0';}cout << mm[sum] << endl;}return 0; }?
轉載于:https://www.cnblogs.com/cunyusup/p/8409882.html
總結
- 上一篇: 大二寒假 之 丢失的13天
- 下一篇: CSS,让100%的宽度,自动减10,让