zoukankan      html  css  js  c++  java
  • POJ2356 Find a multiple 抽屉原理

    Find a multiple
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 4228   Accepted: 1850   Special Judge

    Description

    The input contains N natural (i.e. positive integer) numbers ( N <= 10000 ). Each of that numbers is not greater than 15000. This numbers are not necessarily different (so it may happen that two or more of them will be equal). Your task is to choose a few of given numbers ( 1 <= few <= N ) so that the sum of chosen numbers is multiple for N (i.e. N * k = (sum of chosen numbers) for some natural number k).

    Input

    The first line of the input contains the single number N. Each of next N lines contains one number from the given set.

    Output

    In case your program decides that the target set of numbers can not be found it should print to the output the single number 0. Otherwise it should print the number of the chosen numbers in the first line followed by the chosen numbers themselves (on a separate line each) in arbitrary order.

    If there are more than one set of numbers with required properties you should print to the output only one (preferably your favorite) of them.

    Sample Input

    5
    1
    2
    3
    4
    1
    

    Sample Output

    2
    2
    3
    /*
    抽屉原理,又叫鸽巢原理
    题意:
        给出N个数,问其中是否存在M个数使其满足M个数的和是N的倍数,如果有多组解,
        随意输出一组即可。若不存在,输出 0。
    题解:
        首先必须声明的一点是本题是一定是有解的。原理根据抽屉原理:
        因为有n个数,对n个数取余,如果余数中没有出现0,根据鸽巢原理,一定有两个数的余数相同,
    如果余数出现0,自然就是n的倍数。也就是说,n个数中一定存在一些数的和是n的倍数。
    本题的思路是从第一个数开始一次求得前 i(i <= N)项的和关于N的余数sum,并依次记录相应余数的存在状态,
    如果sum == 0;则从第一项到第i项的和即满足题意。如果求得的 sum 在前边已经出现过,假设在第j(j<i)项出现
    过相同的 sum 值,则从第 j+1 项到第i项的和一定满足题意。
    //
    //代码一:
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    const int MAX = 10001;
    int mod[MAX], num[MAX];
    int sum;
    
    void print(int begin, int end)
    {
        printf("%d\n", end - begin + 1);
        while(begin <= end)
            printf("%d\n", num[begin++]);
    }
    
    int main()
    {
        int n;
        bool flag = false;
        int begin, end;
        while(~scanf("%d", &n))
        {
            memset(mod, -1, sizeof(mod));
            sum = 0;
            for(int i = 1; i <= n; ++i)
            {
                scanf("%d", &num[i]);
                if(flag)
                    continue;
                sum = (sum + num[i]) % n;
                if(sum == 0)
                {
                   // print(1, i);
                    begin = 1;
                    end = i;
                    flag = true;
                    continue;
                }
                if(mod[sum] != -1)
                {
                   // print(sum + 1, i);
                    begin = sum + 1;
                    end = i;
                    flag = true;
                    continue;
                }
                mod[sum] = i;     //mod数组用来记录出现余数为sum时位置,即前i项和除以N余数为sum
            }
            print(begin, end);
        }
        return 0;
    }
    */
    //代码二:---COPY 用搜索做的
    //不过他是直接从头一直向后延伸,从第一个数开始直到找到前x项的和满足解为止,感觉只能适应本题
    //这种解肯定存在的情况,如果用在其他题中,这种贪心应该就不对了
    #include<iostream>
    using namespace std;
    
    int a[10001], n, m;
    
    bool dfs(int sum, int k)
    {
        int i;
        if(sum%n == 0 && sum >= n)
        {
            cout << m << endl;
                return true;
        }
        for(i = k+1; i <= n; i++)
        {
            m++;
            if(dfs(sum+a[i], i))
            {
                cout << a[i] << endl;
                return true;
            }
            m--;
        }
        return false;
    
    }
    int main()
    {
        int i, j;
        cin >> n;
        for(i = 1; i <= n; i++)
            cin >> a[i];
        m=0;
        if(!dfs(0,0))
            cout << 0 << endl;
        return 0;
    }
    

      

    功不成,身已退
  • 相关阅读:
    多线程博文地址 http://www.cnblogs.com/nokiaguy/archive/2008/07/13/1241817.html
    vs2010一运行就报错deven.exe assert failure 解决方法,卸载系统中.netFramework最新版本的(简体中文)
    Lambda语句中创建自定义类型时,也可指定某种特定类型,方法是在new与{}之间写上类型名称
    Win7开始菜单所在目录
    C#中Struct与Class的区别
    Linq语句:三表联查
    用exp、dmp导入导出用户到同一个实例下时,类型type会有问题
    列、约束重命名,原数据不丢失
    CDM中,创建一个或多个组合属性的唯一约束
    EF中新建表和关联表的方法
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2752725.html
Copyright © 2011-2022 走看看