百度2014校园招聘算法——给出一组数据A=[a_0, a_1, a-2, ... a_n](当中n可变),打印出该数值元素的全部组合。
VC++
void StringTest(CString source, CStringArray& dest) { if(source.IsEmpty()) { } else { CString temp = source.Left(1); CStringArray arr; source = source.Mid(1); StringTest(source,dest); arr.Append(dest); for(int i=0;i<dest.GetSize();i++) { CString data = dest.GetAt(i); dest.SetAt(i,temp+data); } dest.Append(arr); dest.Add(temp); } }
測试结果:
void CTestDlg::OnOK() { // TODO: Add extra validation here CStringArray result; CString str = "abc"; StringTest(str,result); CString msg; for(int j=0;j<result.GetSize();j++) { msg +=result.GetAt(j) + ","; } MessageBox("初始数据: "+ str + "\n全部组合: " +msg.Mid(0,msg.GetLength() - 1),"提示",MB_ICONINFORMATION|MB_OK); CDialog::OnOK(); }