C + + Write a permutation combination applet

Source: Internet
Author: User

Today, a problem suddenly comes to mind, and sometimes there are multiple responses to the same event, especially in game AI, where the most appropriate scenario needs to be taken, which is best suited to the outcome or impact of each scenario, so that you can choose the most appropriate one. The most basic is a permutation combination method, the various scenarios are combined out. So wrote a basic n number permutation combination applet!

Development tools: Visual Studio 2012

ctestpermutation::~ctestpermutation () {cout<<">>>>>>>>>>>>>>>>>>>>>>>> Ctestpermutation::~ctestpermutation ()"<<Endl;}voidctestpermutation::D otest () {cout<<"-------The number of n is arranged in combination-------"<<endl<<Endl; Vector<int>vecnums,vecpermutated; cout<<"Sample Data:";  for(inti =1; I <5; i++) {vecnums.push_back (i); cout<<i<<" "; } cout<<Endl; cout<<"permutation combination Result:"<<Endl; Permutation (vecpermutated,vecnums);}/****************************************************** @ functionnmae:permutation @ Function: Arrange the n number in combination @ vecpermutated: already arranged series @ Vecwaitpermuta: number to be arranged ***************************************** ***************/voidCtestpermutation::P ermutation (vector<int> vecpermutated,vector<int>Vecwaitpermuta) {    if(Vecwaitpermuta.size () >0 )    {         for(vector<int>::iterator itnum = Vecwaitpermuta.begin (); Itnum! = Vecwaitpermuta.end (); + +itnum) {Vector<int> vecpermutatedtmp =vecpermutated; Vecpermutatedtmp.push_back (*itnum); Vector<int> vecwaitpermutatmp =Vecwaitpermuta; //Delete elements that are currently sortedvector<int>::iterator Retfind = Find (Vecwaitpermutatmp.begin (), Vecwaitpermutatmp.end (), *itnum); if(Vecwaitpermutatmp.end ()! =retfind)            {vecwaitpermutatmp.erase (retfind); }            //continuation of recursive call permutation algorithmpermutation (vecpermutatedtmp,vecwaitpermutatmp); }    }    Else //a set of arrangements is complete    {        //Print Arrangement Resultscout<<"\ t";  for(inti =0; I < vecpermutated.size (); + +i) {cout<<vecpermutated.at (i) <<" "; } cout<<Endl; }}

Execution Result:

C + + Write a permutation combination applet

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.