排列算法的实现?有难度哦!

!!排列算法的实现?有难度哦!

在n个数取出M个数,输出它们可能的排列所有情况
具体点啊 假如有3, 4, 5 三个数
我对他的3个数 进行排列的话 就是
3 4 5、3 5 4、 4 3 5、 4 5 3、 5 3 4、 5 4 3
六组数
对他2个数排列的话
34,43,35,53,45,54,六组数



$a=array(1,2,3,4);
//m=3
        for($i1=0;$i1<count($a);$i1++){
                for($i2=$i1+1;$i2<count($a);$i2++){
                        for($i3=$i2+1;$i3<count($a);$i3++){
                                echo $a[$i1].$a[$i2].$a[$i3].'<br>';
                        }
                }
        }













经典c程序100例   
一.

题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。组成所有的排列后再去
      掉不满足条件的排列。
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
  int i,j,k;
  printf("/n");
  for(i=1;i<5;i++) /*以下为三重循环*/
    for(j=1;j<5;j++)
      for (k=1;k<5;k++)
      {
        if (i!=k&&i!=j&&j!=k) /*确保i、j、k三位互不相同*/
        printf("%d,%d,%d/n",i,j,k);
      }
  getch();
}

可以参考一下改到php来.. 当然如果不是连续的数字也很容易啦..放array()里啦












我只是让你参考而已.. 你拓展着自己写一下不行阿!!
还把我跟6楼那么不负责人写出来的代码说一样的.. 气死我了阿!!!


<?php
        class Xxx
        {
                private $temp = array();         
                private $result = array();        //返回的结果
                private $chuanjinlai = array();   
                private $geshu ;                  
                private $count ;
               
                /*
                *  @param  array  $chuanjinlai   ,传个array进来.. 也就是你所说的n
                *  @param  int      $geshu           个数,也就是你所说的m
                *  @return    array
                */
                public function getResultArray($chuanjinlai , $geshu)
                {
                        $this->chuanjinlai = $chuanjinlai;
                        $this->geshu = $geshu;
                        $this->count = count($chuanjinlai);
                        if($geshu > $this->count)
                        {
                                return false;
                        }
                        $this->digui($geshu);
                        return $this->result;
                }
               
                private function digui($cishu)
                {
                        if($cishu == 0)
                        {
                                return;
                        }
                        //$this->digui(--$cishu);
                        for($i = 0 ; $i < ($this->count) ; $i++)
                        {
                                $this->temp[] = $this->chuanjinlai[$i];
                                $tt = $cishu -1;
                                $this->digui($tt);
                                if(count(array_count_values($this->temp)) == $this->geshu)
                                {
                                        $this->result[] = join($this->temp,",");
                                       
                                }
                                array_pop($this->temp);
                        }
                }
        }



//  测试
$tt = new Xxx();
$aa = array(11,22,33,44,55);
//$aa = array('a','b','c','d','e');
//$aa = array(-1,-2,-3,0);
print_r($tt->getResultArray($aa,3));


?>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值