How many people have ipad II(数学)

How many people have ipad II

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 174    Accepted Submission(s): 60

Problem Description
hh found more and more of his friends are having ipad IIs. One day when they get together, hh asked his five friends, "How many of you have ipad II now?"

"One!"
"Three!"
"Everyone!"
"Four!"
"Two!"

hh's friends knew each other. They were clear about the "how many" question, while the answers are different, so there must be some people telling lies.

One of hh's friends told him(hh):
number of people, who had ipad IIs, and lied, was no more than 1.
number of people, who didn't have ipad IIs, and told the truth, was no more than 2.
at least one people have ipad II.

Given the information, hh realized there may be one or two people having ipad IIs.

Now hh asks N people the "how many" question. These N friends answer one by one. Some tell the truth, some lie. What hh knows is:
1.number of people, who have ipad IIs, and lie, is no more than A.
2.number of people, who don't have ipad IIs, and tell the truth, is no more than B.
3.At least one people have ipad II.

How many ipad IIs do these N people have?
 
Input
The input begins with an integer T(1<=T<=100).
The next T blocks each indicates a case.
The first line of each case contain a number N(1<=N<=16) then N positive integers follow, integers won't be lager than N.
Then following two numbers A , B(0 <= A,B <= N).
 
Output
Output the number of people have ipad II.
There may be many answers, output them by increasing order. (separated by space)
Output "impossible" if that's impossible.
 
Sample Input
3 5 1 2 3 4 5 1 2 3 0 0 0 1 1 4 0 0 0 0 0 1
 
Sample Output
1 2 1 impossible
 
Author
NotOnlySuccess
 
Source
 
Recommend
lcy
 

Statistic | Submit | Discuss | Note


m为拥有ipad的人数,tr为说真话的人数,N - tr为说谎人数。
假设tr个说真话的人中有j个拥有ipad,得
1、拥有ipad但说谎的人数为m - j,且0 <= m - j <= A;
2、木有ipad但说真话的人数为tr - j,且0 <= tr - j <= B;
3、j的范围:0 <= j <= tr.
4、i的范围:0 <= m <= N.

此外,这题有一个让人极其无语之处,就是N的范围不是题中所说的那么小。。。

AC Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 
 6 int N, A, B, T, a[50], ans[50], top;
 7 
 8 bool Judge(int m)
 9 {
10     int tr = 0;
11     for(int i = 0; i < N; i++)
12         if(a[i] == m) tr++;
13     for(int j = 0; j <= tr; ++j)
14     {
15         if(m - j >= 0 && m - j <= A && tr - j >= 0 && tr - j <= B) return true;
16     }
17     return false;
18 }
19 
20 int main()
21 {
22     scanf("%d", &T);
23     while(T--)
24     {
25         top = 0;
26         scanf("%d", &N);
27         for(int i = 0; i < N; i++)
28         {
29             scanf("%d", &a[i]);
30         }
31         scanf("%d %d", &A, &B);
32         for(int i = 1; i <= N; i++)
33         {
34             if(Judge(i)) ans[top++] = i;
35         }
36         if(!top) puts("impossible");
37         else
38         {
39             for(int i = 0; i < top - 1; i++) printf("%d ", ans[i]);
40             printf("%d\n", ans[top - 1]);
41         }
42     }
43     return 0;
44 }



原文地址:https://www.cnblogs.com/cszlg/p/2910430.html