zoukankan      html  css  js  c++  java
  • Find a multiple POJ

    抽屉原理:   

    形式一:设把n+1个元素划分至n个集合中(A1,A2,…,An),用a1,a2,…,an分别表示这n个集合对应包含的元素个数,则:至少存在某个集合Ai,其包含元素个数值ai大于或等于2。
     
    形式二:设把nm+1个元素划分至n个集合中(A1,A2,…,An),用a1,a2,…,an表示这n个集合对应包含的元素个数,则:至少存在某个集合Ai,其包含元素个数值ai大于或等于m+1。
     
    形式三:设把n个元素分为k个集合A1,A2,…,Ak,用a1,a2,…,ak表示这k个集合里相应的元素个数,需要证明至少存在某个ai大于或等于[n/k]。
      
    题意:n个不同的元素,任意一个或者多个相加为n的倍数。找到这些元素。第一个输出元素的个数,后面分别输出这些元素。(多种情况输出一组)
     
      分析:被n求模的余数为 0,1,2,3....n-1    有n个元素,任意几个数的和为n的倍数,那么这些和假设为 a1, a2 ,a3 ..... am 那么m一定大于n  
         把余数当做抽屉,一定会有至少一个抽屉有两个元素!就是抽屉原理的形式一。
     
    #include<cstdio>
    #include<cstring>
    
    const int maxn = 1e5 + 5;
    int num[maxn], hash[maxn], sum[maxn];
    int n;
    
    int main()
    {
        while (scanf("%d", &n) != EOF){
            memset(hash, 0, sizeof(hash));
            for (int i = 1; i <= n; ++i)
                scanf("%d", &num[i]);
    
            int t = 1, s = 1;
            for (int i = 1; i <= n; ++i)
            {
                sum[i] = (sum[i - 1] + num[i]) % n;
                if (sum[i] == 0){
                    t = i;
                    break;
                }
                if (hash[sum[i]] > 0){
                    s = hash[sum[i]] + 1;
                    t = i;
                    break;
                }
                hash[sum[i]] = i;
            }
            printf("%d
    ", t - s + 1);
            for (int i = s; i <= t; ++i)
                printf("%d
    ", num[i]);
        }
    }
  • 相关阅读:
    Working with the RadioButton and CheclBox controls
    Simple Data Binding in Silverlight 4.0
    Data Binding in Silverlight 4.0
    Building a Simple DataGrid in Silverlight 4.0
    EXCEL数据导入SQL Server数据库中
    正则表达式必知必会
    Eclipse插件一次copy多个文件的相对路径路径
    走出软件作坊
    写在前面的话
    [转载]有经验的Java开发者和架构师容易犯的10个错误
  • 原文地址:https://www.cnblogs.com/ALINGMAOMAO/p/9902103.html
Copyright © 2011-2022 走看看