zoukankan      html  css  js  c++  java
  • 5.20每日一题题解

    Special Permutation

    涉及知识点:

    • 思维

    solution:

    • (因为一共有1~n个数,所以要满足条件我们只需要把数分成奇偶两组(n要大于3,3及其以下是不成立的))
    • (比如n = 8 那么我们1 3 5 7 4 8 6 2,是可以满足条件的。比如 n = 7 , 那么我们1 3 5 7 4 6 2)
    • (那么如何输出结果呢)
    • (奇数部分不用考虑直接从小到大输出就行)
    • (偶数部分先要找到一个用来保证和前面奇数的最后一个数字在【2,4】之间的数字)
    • (根据上面我写的例子我们可以看出这个数字是前面的数字-3)
    • (然后剩下的数字就是降序输出即可)

    std:

    #include<bits/stdc++.h>
    using namespace std;
    typedef  long long LL;
    LL n , m , k;
    
    int main(){
            cin >>n;
            while(n--){
               cin >> m;
               if(m<=3)cout<<-1<<endl;// 不成立
               else if(m==4)cout<<3<<" "<<1<<" "<<4 << " "<<2<<endl;//特判4
               else {
                    if(m%2&1){//m是奇数的情况
                        for(int i=1;i<=m;i+=2)cout<<i<<" ";
                        cout <<m-3<<" ";
                        for(int i=m-1;i>m-3;i-=2)cout<<i<<" ";
                        for(int i=m-5;i>=2;i-=2)cout<<i<<" ";
                        cout<<endl;
                    }
                    else {//m是偶数的情况
                        for(int i=1;i<=m-1;i+=2)cout<<i<<" ";
                        cout <<m-4<<" ";
                        for(int i=m;i>=max(m-2,1LL);i-=2)cout<<i<<" ";
                        for(int i=m-6;i>=2;i-=2)cout<<i<<" ";
                        cout<<endl;
                    }
    
               }
            }
            return 0;
    }
    
  • 相关阅读:
    TeX中的引号
    竖式问题
    蛇形填数
    开灯问题
    排列
    分数化小数
    子序列的和
    cookie
    post请求
    get请求
  • 原文地址:https://www.cnblogs.com/QFNU-ACM/p/12922847.html
Copyright © 2011-2022 走看看