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)!=EOF){
        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;
}

/*
Main.cc: In function 'int main()':
Main.cc:8:11: warning: 'char* gets(char*)' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
     while(gets(a)!=EOF){
           ^
Main.cc:8:17: warning: 'char* gets(char*)' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
     while(gets(a)!=EOF){
                 ^
In file included from /usr/include/stdio.h:74:0,
                 from /usr/include/c++/4.9/cstdio:42,
                 from /usr/include/c++/4.9/ext/string_conversions.h:43,
                 from /usr/include/c++/4.9/bits/basic_string.h:2850,
                 from /usr/include/c++/4.9/string:52,
                 from /usr/include/c++/4.9/bits/locale_classes.h:40,
                 from /usr/include/c++/4.9/bits/ios_base.h:41,
                 from /usr/include/c++/4.9/ios:42,
                 from /usr/include/c++/4.9/ostream:38,
                 from /usr/include/c++/4.9/iostream:39,
                 from Main.cc:1:
Main.cc:8:20: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     while(gets(a)!=EOF){
                    ^
Main.cc:9:9: warning: 'char* gets(char*)' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
         gets(b);
         ^
Main.cc:9:15: warning: 'char* gets(char*)' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
         gets(b);
               ^
*/

Double click to view unformatted code.


Back to problem 22