zoukankan      html  css  js  c++  java
  • 一天一道算法题--6.17--组合问题

    感谢微信平台:一天一道算法题---每天多一点进步----

    problem:

      从M个不同的字符中任取N个字符的所有组合

    analyse:

      微信这边 没有给出一些思路  而且我也觉得它提供的代码 可读性不高... 而且好像 实现出来并没有达到预计效果

      我准备 自己看下 能不能写出来 再贴上去 可能要等明天晚上吧 =-=

      时隔好多天的代码 贴上 =-=

      分别是 考虑 排序的时候元素是否考虑有序的情况与不考虑的情况  只有稍许区别

       

     1 // 从7个的字符串选取3个 任意组合
     2 #include <iostream>
     3 #include <algorithm>
     4 using namespace std;
     5 
     6 char str[20] = {'a','b','C','B','2','1' };
     7 char temp[20];
     8 
     9 void printStr( char* temp , int m )
    10 {
    11     for( int i = 0 ; i<m ; i++ )
    12     {
    13         cout<<temp[i];
    14     }
    15     cout<<endl;
    16 }
    17 
    18 bool judge(    int k , char var )
    19 {
    20     for( int i = 0 ; i<k ; i++ )
    21     {
    22         //if( temp[i]==var ) // 考虑顺序排序 如1 2 3  与 2 1 3都输出
    23         //    return false;
    24         if( temp[i]!=var ) // 不考虑 排序 1 2 3与 2 1 3 只输出 1 2 3一种情况
    25             return false;
    26     }
    27     return true;
    28 }
    29 
    30 void multi(  int t , int n , int m )
    31 {
    32     if( t==m )
    33     {
    34         temp[t] = '';
    35         printStr( temp , m );
    36     }
    37     else
    38     {
    39         for( int i = 0 ; i<n ; i++ )
    40         {
    41             temp[t] = str[i];
    42             if( judge( t , str[i] ) )
    43             {
    44                  multi( t+1 , n , m );
    45             }
    46         }
    47     }
    48 }
    49 
    50 int main()
    51 {
    52     sort(str,str+6);
    53     multi( 0 , 6 , 3 );
    54     getchar();
    55     return 0;
    56 }
    View Code

    today:

      好多事情总是后来才看清楚
      然而我已经找不到来时的路  
      好多事情当时一点也不觉得苦
      就算是苦我想我也不会在乎

    just follow your heart
  • 相关阅读:
    ehcarts绘制一个可以拖动的两条曲线的效果
    bootstrap-table如何根据不同传值进行渲染
    语音播报功能
    webpack命令监测文件变化
    webpacck打包完react后引入到html文件中报错:Target container is not a DOM element...
    react app相关知识
    redux-devtools安装
    react-devtools超级简单安装教程
    react-router v3和v4区别
    foreach循环的跳出
  • 原文地址:https://www.cnblogs.com/radical/p/3795685.html
Copyright © 2011-2022 走看看