| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 736 人关注过本帖
标题:求高手解答!非常感谢!
只看楼主 加入收藏
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:0 
程序代码:
#include <iostream>
using namespace std;

const size_t MAX=6;

unsigned int array[12] =
    {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};

void _swap(unsigned int &a, unsigned int &b)
{
    unsigned int temp = a;
    a = b;
    b =  temp;
}
void _show_x(const size_t x)
{
    //输出横向
    for (size_t i=1; i<=MAX; ++i)
    {
        if (i==array[x])
        {
            cout << 1 << ' ';
        }
        else
        {
            cout << 0 << ' ';
        }
        if (!(i%3))
        {
            cout << 0 << ' ';
        }
    }

    cout << endl;
}
void _show()
{
    static size_t counter = 1;
    cout << "counter = " << counter << endl;
    counter++;

    for (size_t i=1; i<=MAX; ++i)
    {
        _show_x(i-1);
        if (!(i%3))
        {
            for (size_t j=1; j<=MAX; ++j)
            {
                if (!(j%3))
                {
                    cout << 0 << ' ';
                }
                cout << 0 << ' ';
            }
            cout << endl;
            //cout << "0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" << endl;
        }
    }
    cout << endl;
}
void _function(size_t index)
{
    if (index == MAX-1)
    {
        _show();
    }
    else
    {
        for(size_t i=index; i<MAX; ++i)
        {
            _swap(array[index], array[i]);
            _function(index+1);
            _swap(array[index], array[i]);
        }
    }
}

int main(void)
{
    freopen("text.txt", "w", stdout);
    _function(0);

    return 0;
}
2011-06-01 01:26
诸葛修勤
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:11
帖 子:549
专家分:1955
注 册:2010-10-28
收藏
得分:0 
输出的结果太多   
可以设置
const size_t MAX=6;

MAX的值来查看

结果保存到了工程文件下的text.txt中

当MAX=3
counter = 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 0

counter = 2
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 0

counter = 3
0 1 0 0
1 0 0 0
0 0 1 0
0 0 0 0

counter = 4
0 1 0 0
0 0 1 0
1 0 0 0
0 0 0 0

counter = 5
0 0 1 0
0 1 0 0
1 0 0 0
0 0 0 0

counter = 6
0 0 1 0
1 0 0 0
0 1 0 0
0 0 0 0
2011-06-01 08:52
lz1091914999
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:四川
等 级:贵宾
威 望:37
帖 子:2011
专家分:5959
注 册:2010-11-1
收藏
得分:0 
应该有12!个矩阵。

[ 本帖最后由 lz1091914999 于 2011-6-1 11:21 编辑 ]
收到的鲜花
  • 诸葛修勤2011-06-02 00:07 送鲜花  10朵   附言:恩 终于仔细啦

My life is brilliant
2011-06-01 09:31
laigaoat2005
Rank: 4
等 级:业余侠客
帖 子:388
专家分:226
注 册:2007-4-5
收藏
得分:0 
呵呵,比我聪明多了。。看来真不能小看任何一个题目,更应该仔细读题。
为自己的无知感到羞愧,为自己的眼高手低感到惋惜!
还是主席说得好,好好学习,天天向上。
收到的鲜花
  • 诸葛修勤2011-06-02 00:09 送鲜花  10朵   附言:要向你学习!!!
2011-06-01 10:12
yqb139130
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-5-31
收藏
得分:0 
谢谢11楼,非常感谢,我正在运行,好像结果是比较多,还没运行出来~
2011-06-01 10:41
yqb139130
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-5-31
收藏
得分:0 
11楼牛人,很强悍,可惜我没有积分给你,惭愧了!再次非常感谢!
2011-06-02 15:35
laigaoat2005
Rank: 4
等 级:业余侠客
帖 子:388
专家分:226
注 册:2007-4-5
收藏
得分:0 
    啊,这题没有做出来, 这两天吃不好,睡不好。
    那天,我用循环的方法做好很长时间都没有做出来,后来灵光一闪,用位运算来做应该要简单些,于是,牺牲了两天的所有休息时间,并复习了递归n次,终于有了下面的代码。
    一个人鼓捣了好几天,工作一空就又泡在这代码里,两天都没有学习新东西了。今天终于可以休息一会了!
说来还是羞愧,下面这代码,调试了很时间,有些特殊数据(代码中的if(0==(cnt++/6)%2)语句中的6,不是我推算出来的。而是我在多次测试结果中找出来的一个规律数。为什么要用6?我不知道。)
    算法不好(我不是直接排列出结果,我是排列出所有结果,再过滤出不用的结果),只是算出了结果。不过,自己鼓励自己说,总算是自己写的吧。
   
程序代码:
#include <stdio.h> 
#include <iostream>
using namespace std;
const int SIZE(4);         //结果个数为 (SIZE-2)个
int count_motrix=0;//矩阵计数器
int arr[SIZE];
void initia()//初始化数组
{
    for(int cnt=0;cnt<SIZE;cnt++)
    {
        if (0==cnt)        arr[cnt]=1;
        else    arr[cnt]=arr[cnt-1]*2;
    }
    for(int cnt=3;cnt<SIZE;cnt=cnt+4)    arr[cnt]=0;
}

void bina_out_line(int _in_num)
{
    for(int cnt=0;cnt<SIZE;cnt++)
    {
        bool tem_bt = _in_num & (1<<cnt )    ;
        cout << " " <<  tem_bt ;//put 0 or 1
    }
    cout << endl ;  //There is an "endl" at the end of a line.
}

bool test()//过滤
{
    for (int i=0;i<SIZE;i++)
    {
        if ( 0==(i+1)%4 ) //if on 4/8/12...
        {
            if( 0 != arr[i] )    return false;//如果值不为0 不打印
            else continue;        //如果值为0 可能要打印
        }
        if(0==arr[i]) return false;
    }
    return true;
}


inline void swapit(int& a, int& b)
{
        int temp = a;
        a = b;
        b = temp;    
} 

void perm(int arr[], int k, int m)
{    static int cnt=0;
    int i;
    if (k == m)
    {
        if(    test() )
        {
            if(0==(cnt++/6)%2)   
            {
                cout << "counter:" << ++count_motrix << endl;
                for (i = 0; i <= m; i++) 
                    bina_out_line(    arr[i]    );  //cout << (arr[i])<< ",";  //          
                cout <<    "\n";               
            }      
        }
    }  
    else
        for (i=k; i <= m; i++)
        {
            swapit (arr[k], arr[i]);
            perm (arr, k+1, m);
            swapit (arr[k], arr[i]);                        
        }    
}
int main()
{    freopen("result.txt", "w", stdout);
    initia();
    perm(arr, 0,SIZE-1);    
    return 0;
}    


2011-06-03 14:14
快速回复:求高手解答!非常感谢!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015665 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved