题解 | 小红的树构造

小红的树构造

https://www.nowcoder.com/practice/5229e7a68b5a4a2381341ffa4f832273

#include <bits/stdc++.h>
#define int long long
using namespace std;
#define endl '\n'
void work() 
{
    int n , k ; cin >> n >> k ;
    if(k > n - 1)
    {
        cout << -1 << endl ; 
        return ;
    }
    else if(k == 1)
    {
        if(n == 1)
        {
            cout << -1 << endl ; 
        }
        else if(n == 2)
        {
            cout << 1 << " " << 2 << endl ;  
        }
        else
        {
            cout << -1 << endl ; 
        }
        return ; 
    }
    else 
    {
        for(int i = 1 ; i <= k ; i++)
        {
            cout << i << " " << i + 1 << endl ; 
        }
        if(n > k + 2)
        {
            for(int j = k + 2 ; j <= n ; j++)
            {
                cout << 2 << " " << j << endl ;
            }
        }
    }


}
signed main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while (t--) 
    {
        work();
    }
    return 0;
}

在这里补充一篇c++的题解

全部评论

相关推荐

12-23 18:51
中南大学 Java
唉又萌混过关:是不是那种收钱盖实习章的机构?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务