zoukankan      html  css  js  c++  java
  • 循环赛日程表问题(分治法)

    问题描述及代码参考:http://blog.csdn.net/wly_2014/article/details/51388263

    代码部分

    //2018_02_25
    //循环赛日程问题
    //==========================================
    #include <iostream>
    #include <vector>
    
    using namespace std;
    //实现函数
    void Table(vector<vector<int>> &vec)
    {
        if (vec.size() == 0)
            return;
        size_t s = vec.size();  //size_t的range一般大于int
        int k = 0;
        while (s = s >> 1)
        {
            k++;
        }
        vec[0][0] = 1;
        vec[0][1] = 2;
        vec[1][0] = 2;
        vec[1][1] = 1;
    
        for (int i = 2; i <= k; i++)
        {
            int length = 1 << i;
            int half = length >> 1;
            //左下角的子表中项为左上角子表对应项加half = 2^(i-1)
            for (int row = 0; row < half; row++)
            {
                for (int col = 0; col < half; col++)
                {
                    vec[row + half][col] = vec[row][col] + half;
                }
            }
            //右上角的子表等于左下角子表
            for (int row = 0; row < half; row++)
            {
                for (int col = 0; col < half; col++)
                {
                    vec[row][col + half] = vec[row + half][col];
                }
            }
            //右小角子表等于左上角子表
            for (int row = 0; row < half; row++)
            {
                for (int col = 0; col < half; col++)
                {
                    vec[row + half][col + half] = vec[row][col];
                }
            }
        }
    }
    //主函数
    int main(void)
    {
        cout << "共有2 ^ k 个选手参加比赛 , 输入k(k > 0):" << endl;
        int k;
        do
        {
            cin >> k;
        } while ((k < 0) || (k > 31));
        int s = 1 << k;  //总共s个选手
        vector <vector <int> > vec(s, vector<int>(s, 0));  //初始化表格,初始值为0
    
        Table(vec);
    
        for (size_t i = 0; i < vec.size(); i++)
        {
            for (size_t j = 0; j < vec[i].size(); j++)
                cout << vec[i][j] << " ";
            cout << endl;
        }
        system("pause");
        return 0;
    }

    结果如下:

  • 相关阅读:
    七大排序的个人总结2
    如何创建自定义的文件类型关联
    ajax原理,验证码生成原理
    读取同一文件夹下多个txt文件中的特定内容并做统计
    为已定图片加水印
    egrep -v "^#|^$"
    QSplashScreen无法背景透明的解决办法(强制StyleSheet生效)
    c++调用python
    Qt对话框_模态/非模态
    雷军:曾日写300个高质量帖 做互联网需7字诀
  • 原文地址:https://www.cnblogs.com/zf-blog/p/8470524.html
Copyright © 2011-2022 走看看