• POJ 2356 Find a multiple【抽屉原理】


    题目链接:http://poj.org/problem?id=2356

    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个数,使得和为n的倍数。输出m,以及任意顺序的m个数。
    解题思路:用sum[i]表示第1~i个数的和对n取余的结果,那么共有n+1个数。而取余的结果为0~n-1, 相当于n个抽屉。根据抽屉原理,相当于至少有2个数在同一个抽屉中。
    1>如果存在sum[i]==0,那么就是说明1~i的和恰好为n的倍数。
    2>否则,一定存在两个值i, j(i<j)使得sum[i]==sum[j],那么i+1~j之间的数的和为n的倍数.由于要找两个相同的值,所以可以用一个数组pla[i]=j,表示模为i的值在j的位置。

    代码如下:

    View Code
    #include<stdio.h>
    #include<string.h>
    int sum[10003], a[10003], pla[10003];
    int main()
    {
        int i, n, pp, j;
        while(scanf("%d", &n)!=EOF)
        {
            sum[0]=0, pp=0;
            memset(pla, 0, sizeof(pla));
            for(i=1; i<=n; i++)
            {
                scanf("%d", &a[i]);
                sum[i]=(sum[i-1]+a[i])%n;
                if(sum[i]==0)
                    pp=i;
            }
            if(pp!=0)
            {
                printf("%d\n", pp);
                for(i=1; i<=pp; i++)
                    printf("%d\n", a[i]);  
            }
            else
            {
                for(i=1; i<=n; i++)
                {
                    if(pla[sum[i]]==0)
                        pla[sum[i]]=i;
                    else
                    {
                        printf("%d\n", i-pla[sum[i]]);
                        for(j=pla[sum[i]]+1; j<=i; j++)
                        {
                            printf("%d\n", a[j]);
                        }
                        break;
                    }
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    (转)Linux系统中sysctl命令详解 sysctl -p、sysctl -a、sysctl -w
    EM2 MP1 Vowel and Consonant Teacher:Ashley
    Phonics 自然拼读法 y,x,ch,sh,(voiced)th/ð/ ,(unvoiced) th/θ/ Teacher:Lamb
    java列表转成 int[] 的格式
    分类模型评估之ROC-AUC曲线和PRC曲线
    hive 抽样方法
    AUC理解
    Spark性能调优——基础篇
    scala 稀疏向量
    scala 建模
  • 原文地址:https://www.cnblogs.com/Hilda/p/2948607.html
Copyright © 2020-2023  润新知