【2021寒假集训营第一场】I-限制不互素对的排列

限制不互素对的排列

https://ac.nowcoder.com/acm/contest/9981/I

图片说明

代码:

#include<bits/stdc++.h>

using namespace std;

int main(){
    int n, k;
    cin >> n>>k;
    if (n < 6 && k == n / 2)
    {
        cout << -1;
        return 0;

    }
    if (k < n/2)
    {
        int i = 2;
        for (int j = 1; j <= k+1 &&i <= n;  j++, i+= 2)
        cout << i<<' ';
        i-=2;
        for (int j = i+1; j <= n; j++)
            cout << j<<' ';
        for (int j = 1; j <= i; j+=2)
            cout << j<<' ';
    }
    else {
        int i = 8;
        for (int j = 1; j <= k-2 &&i <= n;  j++, i+= 2)
            cout << i <<' ';
        cout << 2 <<' '<< 4 <<' '<< 6 <<' ';
        cout <<3 <<' '<<1 <<' ';
        for (int i = 5; i <= n; i+= 2)
            cout <<i <<' ';
    }
    return 0;
}
全部评论
老师,你讲的扩展求最长的那个程序怎么写?
点赞 回复 分享
发布于 2021-02-05 21:41

相关推荐

秋盈丶:后续:我在宿舍群里和大学同学分享了这事儿,我好兄弟气不过把他挂到某脉上了,10w+阅读量几百条评论,直接干成精品贴子,爽
点赞 评论 收藏
分享
评论
10
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务