Dressing

Dressing

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.
 

Input

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.
 

Output

For each case, output the answer in one line.
 

Sample Input

2 2 2
0
2 2 2
1
clothes 1 pants 1
2 2 2
2
clothes 1 pants 1
pants 1 shoes 1
0 0 0

Sample Output

8
6
5

Source

 
题目大意:
  第一行输入N,M,K,表示有N件衣服,M件裤子,K双鞋子,每次搭配一套衣服是从中各选取一件进行搭配,然后,第二行是表示不能搭配的的情况,输入P,表示有P种情况不能够搭配。而且输入的要求只有clothes-pants pairs or pants-shoes pairs.表示输入的只会是:衣服a-裤子b或者裤子a-鞋子b这样的情况。
  最后,问你还能够搭配的情况有几种,输出答案即可。
解法:
  用Map1[i][j],Map2[i][j]来表示衣服-裤子和裤子-鞋子的搭配的情况。如果直接遍历N*M*K的话会超时,则是不可行的。因此,还需要设置一个状态数组Sign[i],用来表示搭配裤子i,所还能够搭配鞋子的情况个数。刚开始把Map1[i][j]和Map[i][j]初始化为1,在输入P不能够搭配的情况的时候,分别把对应的情况的Map1[i][j]或者Map2[i][j]变成0,表示不可行。如果是输入裤子-鞋子的情况的话,还需要根据Map2[i][j]的情况,改变状态数组的情况。
  输出答案的话,只需要遍历N*M次(每次分别在累计上Sign[Mi]的数值即可)。累加和即是答案、
 1 #include <iostream>
 2 #include <string.h>
 3 #include <stdio.h>
 4 using namespace std;
 5 int a,b,c;
 6 int Map1[1010][1010];
 7 int Map2[1010][1010];
 8 int Sign[1010];
 9 void Cread()/*初始化*/
10 {
11     int i,j,k;
12     for(i=0;i<=a;i++)
13         for(j=0;j<=b;j++)
14             Map1[i][j]=1;/*初始化衣服-裤子的情况*/
15     for(i=0;i<=b;i++)
16     {
17         Sign[i]=c;/*初始化裤子i所能够搭配的情况*/
18         for(j=0;j<=c;j++)
19             Map2[i][j]=1;/*初始化裤子-鞋子的情况*/
20     }
21 }
22 int main()
23 {
24     int i,j,k,M;
25     int aa,bb;
26     char aaa[10],bbb[10];
27     while(scanf("%d%d%d",&a,&b,&c)!=EOF)
28     {
29         if(a==0&&b==0&&c==0)break;
30         Cread();/*初始化*/
31         scanf("%d",&M);
32         for(i=0;i<M;i++)
33         {
34             scanf(" %s %d %s %d",aaa,&aa,bbb,&bb);
35             if(aaa[0]=='c'&&bbb[0]=='p')
36             {
37                  Map1[aa][bb]=0;
38             }
39             else
40             {
41                  if(Map2[aa][bb])
42                  {
43                      Sign[aa]--;/*如果这边还没有去除,则--*/
44                      Map2[aa][bb]=0;
45                  }
46             }
47         }
48         int SUM=0;
49         for(i=1;i<=a;i++)
50         {
51             for(j=1;j<=b;j++)
52             {
53                 if(Map1[i][j])
54                     SUM+=Sign[j];
55             }
56         }
57         printf("%d\n",SUM);
58     }
59     return 0;
60 }
View Code

 

转载于:https://www.cnblogs.com/Wurq/articles/4580841.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
不使用LINQ查询和操作集合 改进代码 namespace SandwichCalories { class Program { static void Main(string[] args) { // sandwich ingredients and their associated calories Dictionary<string, int> ingredients = new Dictionary<string, int>() { { "Bread", 66 }, { "Ham", 72 }, { "Bologna", 57 }, { "Chicken", 17 }, { "Corned Beef", 53 }, { "Salami", 40 }, { "Cheese, American", 104 }, { "Cheese, Cheddar", 113 }, { "Cheese, Havarti", 105 }, { "Mayonnaise", 94 }, { "Mustard", 10 }, { "Butter", 102 }, { "Garlic Aioli", 100 }, { "Sriracha", 15 }, { "Dressing, Ranch", 73 }, { "Dressing, 1000 Island", 59 }, { "Lettuce", 5 }, { "Tomato", 4 }, { "Cucumber", 4 }, { "Banana Pepper", 10 }, { "Green Pepper", 3 }, { "Red Onion", 6 }, { "Spinach", 7 }, { "Avocado", 64 } }; // prompt user for calorie range Console.Write("Enter minimum calories: "); int min_calories = int.Parse(Console.ReadLine()); Console.Write("Enter maximum calories: "); int max_calories = int.Parse(Console.ReadLine()); // calculate the minimum and maximum calories for the sandwich int min_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Min() * 2; int max_sandwich_calories = 2 * ingredients["Bread"] + ingredients.Values.Max() * 2; // check if the calorie range is valid if (max_calories < min_sandwich_calories) { Console.WriteLine("Sorry, it is impossible to create a sandwich within the given calorie range."); } else { // create the sandwich List<string> sandwich = new List<string> { "Bread" }; int sandwich_calories = 1 * ingredients["Bread"]; while (sandwich_calories < min_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; } while (sandwich_calories <= max_calories) { // add random ingredient string ingredient = ingredients.Keys.ElementAt(new Random().Next(ingredients.Count)); // check if the ingredient is the same as the previous one if (sandwich.Count >= 3 && ingredient == sandwich[sandwich.Count - 2]) { continue; } sandwich.Add(ingredient); sandwich_calories += ingredients[ingredient]; // check if the sandwich is already at the maximum calorie limit if (sandwich_calories == max_sandwich_calories) { break; } } // add the last slice of bread sandwich.Add("Bread"); // print the sandwich and its total calories Console.WriteLine("Your sandwich: " + string.Join(", ", sandwich)); Console.WriteLine("Total calories: " + sandwich_calories); } } } }
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值