首页 > ACM题库 > HDU-杭电 > HDU 3807-How many people have ipad II-枚举-[解题报告]HOJ
2015
04-13

HDU 3807-How many people have ipad II-枚举-[解题报告]HOJ

How many people have ipad II

问题描述 :

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?

输入:

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).

输出:

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).

样例输入:

3
5
1 2 3 4 5
1 2

3
0 0 0
1 1

4
0 0 0 0
0 1

样例输出:

1 2
1
impossible

很好的思路     枚举有多少人有ipad 判是否满足题目给出的条件

#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>
#define INF 0x7f7f7f7f
using namespace std;

int num[1010];
vector<int> ans;
int main()
{
    int t, n, x, A, B;
    scanf("%d", &t);
    while(t--)
    {
        memset(num, 0, sizeof(num));
        ans.clear();
        scanf("%d", &n);
        for(int i = 0; i < n; ++ i)
        {
            scanf("%d",&x);
            num[x]++;
        }
        scanf("%d%d", &A, &B);
        for(int i = 1; i <= n; ++ i)
        {
            for(int j = 0; j <= A; ++ j)
            {
                int lie = n-num[i];
                if(i-j >= 0 && lie-j >= 0 && (n-(i-j+lie) >= 0 && n-(i-j+lie) <= B))
                {
                    ans.push_back(i);
                    break;
                }
            }
        }
        if((int)ans.size() == 0)
            printf("impossible\n");
        else
        {
            int len = ans.size();
            for(int i = 0; i < len-1; ++ i)
                printf("%d ", ans[i]);
            printf("%d\n", ans[len-1]);
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/xlc2845321/article/details/24707367


  1. #!/usr/bin/env python
    def cou(n):
    arr =
    i = 1
    while(i<n):
    arr.append(arr[i-1]+selfcount(i))
    i+=1
    return arr[n-1]

    def selfcount(n):
    count = 0
    while(n):
    if n%10 == 1:
    count += 1
    n /= 10
    return count

  2. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。