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;
    }
  • 相关阅读:
    Oracle JET Model 数据获取与使用
    Windows 10 安装MySQL 8.0.11
    Windows安装JDK9
    Centos安装JDK
    Centos7下安装php-redis扩展及简单使用
    XAMPP开启虚拟目录
    centos7.2 环境下配置 Apache2.4 +PHP5.6+Redis+Supervisord
    CentOS 7 开放3306端口访问
    编译Apache时,如何enable所有组件
    LINUX创建www的用户组和用户,并且不允许登录权限:
  • 原文地址:https://www.cnblogs.com/heqinghui/p/3186088.html
Copyright © 2011-2022 走看看