{
ans=ans+dep*(s1[x]-'A'+1); if(lefts[x]>=0)calc(lefts[x],dep+1); if(rights[x]>=0)calc(rights[x],dep+1); }
void check(int x) {
if(lefts[x]>=0)check(lefts[x]); s3=s3+s1[x];
if(rights[x]>=0)check(rights[x]); }
void dfs(int x,int th) {
if(th==n) { s3=\check(0); if(s3==s2) { ans=0; calc(0,1);
cout< if(lefts[x]==-1&&rights[x]==-1) { lefts[x]=th; father[th]=x; dfs(th,th+1); father[th]=-1; lefts[x]=-1; } if(rights[x]==-1) { rights[x]=th; father[th]=x; dfs(th,th+1); father[th]=-1; rights[x]=-1; } if(father[x]>=0) dfs(father } int main() { cin>>s1; cin>>s2; n=s1.size() memset(lefts, memset(rights memset(father dfs(0,1); } 输入: ABCDEF BCAEDF 输出:_________ 五、完善程序(第1题第2空3分,其余每空2.5分,共计28分) 1.(排列数)输入两个正整数n,m(1≤n≤20,1≤m≤n),在1~n中任取m个数,按字典序从小到大输出所有这样的排列。例如 输入:3 2 输出:1 2 1 3 2 1 2 3 3 1 3 2 #include Const int SIZE=25; bool used[SIZE]; int data[SIZE]; int n,m,i,j,k; bool flag; int main() { cin>>n>>m; memset(used,false,sizeof(used)); for(i=1;i<=m;i++) { data[i]=i; used[i]=true; } flag=true; while(flag) { for(i=1;i<=m-1;i++)cout<=1;i--) { ② ; for(j=data[i]+1;j<=n;j++)if(!used[j]) { used[j]=true; data[i]=③ ; flag=true; break; } if(flag) { for(k=i+1;k<=m;k++) for(j=1;j<=④ ;j++)if(!used[j]) { data[k]=j; used[j]=true; break; } ⑤ ; } }
相关推荐: