TA的每日心情 | 开心 2021-12-13 21:45 |
---|
签到天数: 15 天 [LV.4]偶尔看看III
|
直接上代码
- [code]void AString_m(char *str,char*pBegin)
- {//扩展以后对数组也适用。也可以写成这种形式AString(char *str,int i,int n);
- if(!str || !pBegin) return;
- if(*pBegin=="\0") cout<<str<<endl;
- else
- { for(char *temp=pBegin;*temp!="\0";temp++)
- { char t=*pBegin;*pBegin=*temp;*temp=t;
- AString_m(str,pBegin+1);
- t=*pBegin;*pBegin=*temp;*temp=t;
- }
- }
- }
- void AString( char*str)//提供的公共接口函数
- {AString_m(str,str); }
- void CString_m(char * pStr,int m,vector<char> & result)
- {//从一个字符串中选取m个字符
- if(pStr==NULL)//字符串为空情形
- return ;
- if(*pStr=="\0" && m!=0)//字符串中不够m个字符啦
- return ;
- if(m==0)
- { for(int i=0;i<result.size();i++)
- cout<<result[i];
- cout<<endl;
- return;
- }
- result.push_back(*pStr);
- CString_m(pStr+1,m-1,result);
- result.pop_back();
- CString_m(pStr+1,m,result);
- }
- void CString(char *pStr)
- {//求一个字符串中字符的全排列
- if(pStr==NULL || *pStr=="\0") return;
- int len=strlen(pStr); vector<char> re;
- for(int i=1;i<=len;i++)
- { re.clear();
- CString_m(pStr,i,re);
- }
- }
- //排列组合类似实际问题:运动员打靶十次,问共打中90环有多少种可能
- void ShootProblem_m(int number,int sum,vector<int> & result,int *total)
- {//打靶number次,共打中sum环
- if(number<=0 || number*10<sum ) return ;
- if(number==1)
- { if(sum<=10 && sum>=0)
- { for(int i=0;i<result.size();i++)
- cout<<result[i]<<" ";
- cout<<sum<<endl;
- (*total)++;
- return;
- }
- else
- return;
- }
- for(int i=0;i<=10;i++)
- {
- result.push_back(i);
- ShootProblem_m(number-1,sum-i,result,total);
- result.pop_back();
- }
- }
- void ShootProblem(int number,int sum)
- {//提供的公共接口函数
- vector<int >re;
- int a=0;
- ShootProblem_m(number,sum,re,&a);
- cout<<"总共可能性有"<<a<<"种"<<endl;
- }
复制代码 [/code]
|
|