[华为oj]iNOC产品部-杨辉三角的变形

             1ios

         1  1  1spa

      1  2  3  2  1code

   1  3  6  7  6  3  1blog

1  4  10 16 19  16 10  4  1ci

以上三角形的数阵,第一行只有一个数1,如下每行的每一个数,是刚好是它上面的数,左上角数到右上角的数,3个数之和(若是不存在某个数,认为该数就是0)。get

求第n行第一个偶数出现的位置。若是没有偶数,则输出-1。例如输入3,则输出2,输入4则输出3。io

 

 1 #include <iostream>
 2 #include <deque>
 3 
 4 using namespace std;
 5 
 6 static deque<int> deInt;
 7 
 8 void getOddPos(int count)
 9 {
10     count=count-1;
11 
12     deInt.push_front(0);
13     deInt.push_front(0);
14     deInt.push_back(0);
15     deInt.push_back(0);
16 
17     deque<int> temp;
18     for(int i=1;i<static_cast<int>(deInt.size())-1;i++)
19     {
20         int t=deInt.at(i-1)+deInt.at(i)+deInt.at(i+1);
21         if(count<=0)
22         {
23             if(t%2==0)
24             {
25                 cout<<i<<endl;
26                 return;
27             }
28         }
29         temp.push_back(t);
30     }
31     if(count<=0)
32     {
33         cout<<-1<<endl;
34         return;
35     }
36 
37     deInt.clear();
38     deInt.insert(deInt.begin(),temp.begin(),temp.end());
39     temp.clear();
40     getOddPos(count);
41 }
42 
43 int main()
44 {
45     deInt.push_back(1);
46     int r;
47     cin>>r;
48     if(r==1)
49         cout<<-1;
50     else
51     {
52         --r;
53         getOddPos(r);
54     }
55 }
相关文章
相关标签/搜索