自己写的时候,利用deque<int>按照模拟的思路去做,结果超时了。看了解析,原来是找规律的问题。
#include <iostream> #include <deque> using namespace std; int main(){ // 用找规律来解题 int n = 0; cin >> n; if (n <= 2) cout << -1 << endl; // 前两行没有偶数 else if (n % 2 == 1) cout << 2 << endl; // 奇数行的偶数都在第二个位置 else if (n % 4 == 2) cout << 4 << endl; // 偶数行,如果该行除4余数为2,则偶数位于第四个位置 else if (n % 4 == 0) cout << 3 << endl; // 偶数行,如果该行除4能整数,则偶数位于第三个位置 }