集合 uva 496

Simply Subsets

题目大意:

判断两个集合包含等关系,输出要求结果。

原题

After graduating from the University of Notre Dame, you obtained a job at Top Shelf Software, Inc.,as an entry-level computer engineer. On the first day, your manager sits down with you and tasks you with the following job: “We want to see how well you understand computer programming and theabstract science behind it. As an evaluation for all of our new hires, we require them to write a program to determine the relationship between pairs of sets. I’m quite sure that you’ll do well; my confidence is high. Here’s a list of requirements for what the program should do. Good luck.”

Input

Your program should accept an even number of lines of text. Each pair of lines will represent two sets; the first line represents set A, the second line represents set B. Each line of text (set) will be a list of distinct integers.

Output

After each pair of lines has been read in, the sets should be compared and one of the following responses should be output:
• A is a proper subset of B
• B is a proper subset of A
• A equals B
• A and B are disjoint
• I’m confused!

Sample Input

55 27
55 27
9 24 1995
9 24
1 2 3
1 2 3 4
1 2 3
4 5 6
1 2
2 3

Sample Output

A equals B
B is a proper subset of A
A is a proper subset of B
A and B are disjoint
I’m confused!

大致思路

字符串存入数组。比较两个集合的大小,放入min和max。从小的集合那个一一找大的集合出现的数字。字符转数字。find记录出现的相等数字的次数。记录原本两个集合数字的个数。

#include <stdio.h>
#include <stdlib.h>
#include "string.h"
int main()
{
    char a[1000],b[1000],min[1000]={' '},max[1000]={' '},change;
    char *pmax,*pmin;
    int lenA,lenB,find,i,j,charmin,charmax,countmin,countmax;
    while(gets(a)!=NULL&&gets(b)!=NULL)
    {
        min[0]=' ';max[0]=' ';
        lenA=strlen(a);
        lenB=strlen(b);
        if(lenA<=lenB)
        {
            strcat(min,a);
            strcat(max,b);
            change=0;
        }
        else
        {
            strcat(min,b);
            strcat(max,a);
            change=1;
        }
        //判别两个集合的大小,并将小的放入min[],大的放入max[]
        charmin=strlen(min);
        charmax=strlen(max);
        pmax=max;
        pmin=min;
        find=0;
        countmax=0;
        countmin=0;
		//初始化

        for(i=0;i<charmin;i++,pmin++)
        {
            if(min[i]!=' ') continue;
            else if(min[i]==' ')
            {
                countmin++;
                for(j=0;j<charmax;j++,pmax++)
                {
                    if(max[j]!=' ') continue;
                    else if(max[j]==' ')
                    {
                        //countmax++;有问题,需要另外统计
                        if(atoi(pmin)==atoi(pmax))
                        {
                            find++;break;
                        }
                        else continue;
                    }
                }
                pmax=max;
            }
        }
		//记录find
        for(i=0;i<charmax;i++)
        {
            if(max[i]==' ')
            {
                countmax++;
            }
        }
	//单独记录max中数字个数
        if(find==countmax)
        {
            printf("A equals B\n");
        }
        else if(find==countmin)
        {
            if(change) printf("B is a proper subset of A\n");
            else printf("A is a proper subset of B\n");
        }
        else if(find<countmin&&find)
        {
            printf("I'm confused!\n");
        }
        else if(find==0)
        {
            printf("A and B are disjoint\n");
        }
		//按要求输出
        memset(min,0,sizeof(min));
        memset(max,0,sizeof(max));
        //置零清空max[],min[]为下一次大循环输入做准备
    }
    return 0;
}

总结

1.开始code前,先理清思路,注意数据类型,和所有情况。写伪代码。
2.对初始值的设定。
3.多反复测试,找问题。快速debug。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值