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

    /*
    POJ-2356 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 <iostream>
    #include<cmath>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    #define maxn 16000
    int a[maxn];
    int sum[maxn];
    int main()
    {
        int i,j,k,n,flag;
        while(scanf("%d",&n)!=EOF)
        {
            flag=false;
            sum[0]=0;
            for(i=1; i<=n; i++)
            {
                scanf("%d",&a[i]);
            }
    
            for(i=1; i<=n; i++)
            {
                sum[i]=(sum[i-1]+a[i])%n;
                if(sum[i]==0)
                {
    
                    printf("%d
    ",i);
                    for(k=1; k<=i; k++)
                        printf("%d
    ",a[k]);
                    break;
                }
                else
                {
                    for(j=1; j<i; j++)
                    {
                        if(sum[i]==sum[j])
                        {
                            printf("%d
    ",i-j);
                            for(k=j+1; k<=i; k++)
                                printf("%d
    ",a[k]);
                            flag=true;
                            break;
                        }
                    }
                }
                if(flag)
                    break;
            }
        }
        return 0;
    }
  • 相关阅读:
    MySQL索引优化入门
    优雅实现订单关闭及定时器的使用
    web.xml详解
    多个Tomcat之间实现Session共享
    tomcat8.0 基本参数调优配置
    tomcat部署会碰到的问题
    Centos7以上的版本 mysql 无法启动,无法停止问题
    tomcat 重启报错unregister mbean error javax.management.InstanceNotFoundException
    日线做多,15min做空的情况收集
    葛兰碧均线法则
  • 原文地址:https://www.cnblogs.com/heqinghui/p/3186088.html
Copyright © 2011-2022 走看看