zoukankan      html  css  js  c++  java
  • find a multiple

    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个数,从中找出数字,它们加起来之和是n的倍数,并将它们输出。

    分析:之和是n的倍数,就是对n求余是0。 通常大家会想把这些数随机组合,得到的和存放在一个数组里,但遇到很多数,这就不好办了。 由于问题只要找出一组即可,所以我们就是要找到一组。 所以我建立个抽屉模型,
    为什么可以用抽屉模型?有n个数,这些数的连续和分别对n取余得到的余数范围是0~n-1, 不相同的余数个数不超过n,当个数为n时, 那其中肯定有一组数的和满足条件,若小于n,这当中至少有两组数的余数相同,举个
    例子,1,3,5,9,2 ,5个数, 我把它们连续相加变成, 1,4, 9, 18, 20, 仍然是5个数。 使他们一一对5取余,得到 1, 4, 4, 3, 0。 我把余数当作抽屉,和作为物体, 这里有5个物体,4个抽屉,
    第一组数是1,3,第二组数是1,3,5。 用后者减前者便是答案。
    #include <stdio.h>
    #define M 10001
    int a[M], mod[M];
    int main()
    {
        int n, i, sum, begin, end;
        while(scanf("%d", &n) != EOF){
                          
                              sum = 0;
                              for(i = 1; i <= n; i++){
                                    
                                      scanf("%d", &a[i]);
                                      
                              }
                              for(i = 1; i <= n; i++){
                                    
                                      sum = (sum + a[i]) % n;
                                      if(!sum)
                                      {
                                              begin = 1;
                                              end = i;
                                              break;
                                      }
                                      else if(mod[sum] > 0)
                                      {
                                              begin = mod[sum] + 1;
                                              end = i;
                                              break;
                                      }
                                      mod[sum] = i;
                              
                              }
                              printf("%d
    ", end - begin + 1);
                              for(i = begin; i <= end; i++){
    
                                    printf("%d
    ", a[i]);
    
                              }
                              putchar('
    ');
    
        }
        return 0;
    }
    View Code
  • 相关阅读:
    android中样式和自定义button样式
    android——实现多语言支持
    sizeof,数组,指针
    C++预处理相关
    内联函数
    牛客C++刷题
    leetcode刷题列表
    ends在linux和Windows下输出结果不同
    计算机负数为什么使用补码及浮点型计算
    个人技术博客:VUE:0基础入门
  • 原文地址:https://www.cnblogs.com/the-one/p/3246568.html
Copyright © 2011-2022 走看看