杭电 2034

这道题让开始我用的字符串。不过很麻烦。也没Ac。
后来参考别人的算法,用了bool类型。bool类型和int,float ,double 类似,只是占一个字节,表示yes 或no。

#include<stdio.h>
#include<iostream>
using namespace std;
#include<memory.h>
#include<algorithm>
int main()
{
    int a[105],b[105];
    bool c[105];
    int n,m;
    while(scanf("%d%d",&n,&m) != EOF)
    {
        if(n == 0 && m == 0)
            break;
        int i;
        for(i = 0;i < n; i++)
            cin>>a[i];
        for(i = 0;i < m; i++)
            cin>>b[i];
        sort(a,a+n);
        sort(b,b+m);
        memset(c,true,sizeof(c));
        int j,num = 0;
        for(i = 0;i < n; i++)
            for(j = 0;j < m; j++)
            {
                if(a[i] == b[j])
                {
                    num ++;
                    c[i] = false;
                }
            }
        if(num == n)
        {
            printf("NULL\n");
            continue;
        }
        for(i = 0;i < n; i++)
            if(c[i] == true)
                printf("%d ",a[i]);
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(算法,杭电)