View Code of Problem 22

#include <iostream>
#include <string.h>
using namespace std;

int main(){
    char a[1001],b[1001],ans[1001];
    int len_a,len_b,s;
    while(gets(a)!=NULL){
        gets(b);
        len_a = (int)strlen(a);
        len_b = (int)strlen(b);
        s = 0;
        for(int j=0;j<len_a;j++){
            for(int k=0;k<len_b;k++){
                if(a[j]==b[k]){
                    ans[s++] = a[j];
                    b[k]='0';   //防止重复
                    break;
                }
            }
        }
      	ans[s] = '\0';
        //排序
        for(int j=0;j<s;j++){
            for(int k=0;k<s-1-j;k++){
                if(ans[k]>ans[k+1]){
                    char tmp = ans[k];
                    ans[k] = ans[k+1];
                    ans[k+1] = tmp;
                }
            }
        }
        
        printf("%s\n",ans);
    }
    return 0;
}

Double click to view unformatted code.


Back to problem 22