zoukankan      html  css  js  c++  java
  • poj 23565-Find a multiple

    Find a multiple

    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和n个数,让你求出是否存在某几个数的加和为n的倍数。
    如有多种情况,输出一种。

    题解 第一种 存在一个数为n的倍数。
    第二种sum[1]=a[1],sum[2]=a[1]+a[2],…
    其中有n的倍数,输出,就好了。
    第三种 有sum[]中有n个数,sum%n的范围是0~n-1,这里就用到了抽屉原理,有n个数字,范围在1~n-1, 必定存在 sum[i]%n-sum[j]%n==0;
    就可以知道有i+1~j

    代码有点乱,,,,,

    #include<stdio.h>
    int a[10010],sum[10010];
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            int sum1=0;
            int s;
            int flag=0;
            int k;
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
                if(a[i]%n==0)
                {
                    k=a[i];
                    flag=1;
                }
                sum1+=a[i];
                sum[i]=sum1;
            }
            if(flag==1)
            {
                return  printf("1
    %d
    ",k);
    
            }
            else
            {
                int d=0;
                for(int i=1;i<=n;i++)
                {
                    if(sum[i]%n==0)
                    {
                        printf("%d
    ",i);
                        for(int j=1;j<=i;j++)
                         printf("%d
    ",a[j]);
                        d=1;
                    }
                }
                if(d==0)
                {
                    for(int i=1;i<n;i++)
                    {
                        for(int j=i+1;j<=n;j++)
                        {
                            if((sum[j]%n-sum[i]%n)==0)
                            {
                                printf("%d
    ",j-i);
                                for(int k=i+1;k<=j;k++)
                                printf("%d
    ",a[k]);
                                break; 
    
                            }
                        }
                    }
                }
    
            }
    
    
        }
        return 0;
    }
  • 相关阅读:
    常见SQL语句
    测试用例的设计
    移动端测试注意事项
    markdown编辑模式基本使用
    常用修改请求或返回方法
    前端性能测试工具Lighthouse
    presto环境部署
    pyenv管理python版本
    python2.6.6升级python2.7.14
    InfluxDB权限认证机制
  • 原文地址:https://www.cnblogs.com/-xiangyang/p/9220271.html
Copyright © 2011-2022 走看看