zoukankan      html  css  js  c++  java
  • 数组全排列 手动&自动

    /*
    手动:交换 再 交换
    自动:next_permutation
    */

    #include <iostream> #include <string> #include <sstream> #include <algorithm> #include <cstring> using namespace std ; int ans = 0; int check(int a[]){ if(a[0]-a[4]==-1||a[0]-a[4]==1) return 0; if(a[3]-a[4]==-1||a[3]-a[4]==1) return 0; if(a[5]-a[4]==-1||a[5]-a[4]==1) return 0; if(a[7]-a[4]==-1||a[7]-a[4]==1) return 0; if(a[8]-a[4]==-1||a[8]-a[4]==1) return 0; if(a[9]-a[4]==-1||a[9]-a[4]==1) return 0; if(a[1]-a[4]==-1||a[1]-a[4]==1) return 0; if(a[1]-a[5]==-1||a[1]-a[5]==1) return 0; if(a[1]-a[6]==-1||a[1]-a[6]==1) return 0; if(a[0]-a[5]==-1||a[0]-a[5]==1) return 0; if(a[2]-a[5]==-1||a[2]-a[5]==1) return 0; if(a[8]-a[5]==-1||a[8]-a[5]==1) return 0; if(a[9]-a[5]==-1||a[9]-a[5]==1) return 0; if(a[6]-a[5]==-1||a[6]-a[5]==1) return 0; if(a[6]-a[9]==-1||a[6]-a[9]==1) return 0; if(a[6]-a[2]==-1||a[6]-a[2]==1) return 0; if(a[3]-a[0]==-1||a[3]-a[0]==1) return 0; if(a[3]-a[7]==-1||a[3]-a[7]==1) return 0; if(a[8]-a[7]==-1||a[8]-a[7]==1) return 0; if(a[8]-a[3]==-1||a[8]-a[3]==1) return 0; if(a[9]-a[8]==-1||a[9]-a[8]==1) return 0; if(a[1]-a[0]==-1||a[1]-a[0]==1) return 0; if(a[1]-a[2]==-1||a[1]-a[2]==1) return 0; } //手动全排列 void f(int k ,int a[]){ //出口 if(k == 10){ if(check(a)!= 0 ){ ans++; } return ; } for(int i = k; i<10 ; ++i){ {int temp = a[k] ; a[k] = a[i] ; a[i] = temp ;} //深入 f(k+1,a); {int temp = a[k] ; a[k] = a[i] ; a[i] = temp ;} //回溯 } } void i2s(int x, string &basic_string) { stringstream ss; ss << x; ss >> basic_string; } void s2i(string &basic_string , int &x ) { stringstream ss; ss << basic_string; ss >> x; } void c2i(char &basic_string , int &x ) { stringstream ss; ss << basic_string; ss >> x; } void c2s(char c , string & s){ stringstream stream; stream << c; s = stream.str(); } //自动全排列 void f1(string s, int a[]){ do{ for(int i=0 ; i<10 ; ++i){ int temp ; // string temps ; // c2s (s[i],temps); // s2i(temps,temp); c2i(s[i],temp); a[i] = temp ; } check(a); }while(next_permutation(s.begin(),s.end())); } int main(){ //int a[10]={0,1,2,3,4,5,6,7,8,9}; //f(0,a); int a[10]; string s ="0123456789" ; f1(s,a); cout<<ans<<endl; }

      

  • 相关阅读:
    「Android」Android4.04 在线源代码查看
    「Android」Huawei U8825d new LowmemoryKiller config
    「Linux」Ubuntu12.10的Libpcap1.3安装过程
    NSString+NSMutableString+NSValue+NSAraay用法汇总
    hd2 刷android
    EXT分区教程
    how to use a Class Breaks Renderer in the ESRI ArcGIS iPhone API to display cities of varying population with different
    windows server 2008修改远程桌面连接数
    iphone开发常用代码
    手持GPS坐标系统的转换与应用
  • 原文地址:https://www.cnblogs.com/Stephen-Jixing/p/12628305.html
Copyright © 2011-2022 走看看