Dressing(考试补题)

来源:互联网 发布:jquery.rotate.js api 编辑:程序博客网 时间:2024/05/01 06:48


题目描述

Wangpeng has N clothes, M pants and K shoes so theoretically he can have N×M×K different combinations of dressing.
One day he wears his pants Nike, shoes Adiwang to go to school happily. When he opens the door, his mom asks him to come back and switch the dressing. Mom thinks that pants-shoes pair is disharmonious because Adiwang is much better than Nike. After being asked to switch again and again Wangpeng figure out all the pairs mom thinks disharmonious. They can be only clothes-pants pairs or pants-shoes pairs.
Please calculate the number of different combinations of dressing under mom’s restriction.

输入

There are multiple test cases.
For each case, the first line contains 3 integers N,M,K(1≤N,M,K≤1000) indicating the number of clothes, pants and shoes.
Second line contains only one integer P(0≤P≤2000000) indicating the number of pairs which mom thinks disharmonious.
Next P lines each line will be one of the two forms“clothes x pants y” or “pants y shoes z”.
The first form indicates pair of x-th clothes and y-th pants is disharmonious(1≤x≤N,1 ≤y≤M), and second form indicates pair of y-th pants and z-th shoes is disharmonious(1≤y≤M,1≤z≤K).
Input ends with “0 0 0”.
It is guaranteed that all the pairs are different.

输出

For each case, output the answer in one line.

样例输入

2 2 202 2 21clothes 1 pants 12 2 22clothes 1 pants 1pants 1 shoes 10 0 0

样例输出

865
大概意思是他有N件衣服,M条裤子,K双鞋。他妈妈有不喜欢的搭配,求他妈妈喜欢的搭配?
思想,一共搭配的方式有N*M*K种,以裤子为入手点,就是M个n*k相加,对于每一条裤子,有n*k种搭配方式,如果有衣服或者是鞋不能和这条裤子搭配,就用n-1 or k-1;
代码如下
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
int main()
{
    int a[1006],b[1006];
    int z,x,c;
    while(~scanf("%d%d%d",&z,&x,&c))
    {
        char q[100],e[100];
        int w,r,i;
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        if(z==0&&x==0&&c==0)
        {
            break;
        }
        int t;
        scanf("%d",&t);
        for(i=0;i<t;i++)
        {
            scanf("%s%d%s%d",q,&w,e,&r);
            if(strcmp(q,"clothes")==0)
            {
                a[w]++;
            }
            else
            {
                b[r]++;
            }
        }
        int sum=0;
        for(i=0;i<x;i++)
        {
            sum+=(z-a[i])*(c-b[i]);
        }
        printf("%d\n",sum);
 
    }
}
0 0
原创粉丝点击