solution-p9635

· · 题解

本题难度不大,只需要找到规律即可答题.

我给了一组样例:

我们发现:

当 n=3 时,输出为 -1 。

当 n\mod 4=0 时:数列为 \{1,2,3,\cdots,n-1,n\} 。

当 n\mod 4=1 时:数列为 \{1,3,\cdots,n,n+1\} 。

当 n\mod 4=2 时:数列为 \{1,2,3,\cdots,n-1,n+1\} 。

当 n\mod 4=3 时:数列为 \{1,3,4,\cdots,n,n+1\} 。

这些结论有了,就可以写代码了。

标程:

#include<bits/stdc++.h>
using namespace std;
inline void op(int x){
    if(x==3){
        cout<<-1;
        return ;
    }
    if(x%4==0){
        for(int i=1;i<=x;++i)printf("%d ",i);
        return;
    }else if(x%4==2){
        for(int i=1;i<x;++i)printf("%d ",i);
        printf("%d",x+1);
        return;
    }else if(x%4==3){
        for(int i=2;i<=x-1;++i)printf("%d ",i);
        printf("%d %d",x+1,x+2);
    }else{
        printf("1 ");
        for(int i=3;i<=x+1;++i){
            printf("%d ",i);
        }
        return ;
    }
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        op(n);
        cout<<endl;
    }
    return 0;
}