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

    Problem 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 的倍数

    思路:抽屉原理经典应用


    AC代码:

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <string.h>
     4 #include <algorithm>
     5 
     6 using namespace std;
     7 #define N 100502
     8 int arr[N];
     9 int vis[N];
    10 int sum[N];
    11 int main(){
    12     int n;
    13     while(~scanf("%d",&n)){
    14         int flag=0;
    15         sum[0]=0;
    16         for(int i=1;i<=n;i++){
    17             scanf("%d",&arr[i]);
    18             sum[i]=sum[i-1]+arr[i];
    19             if(sum[i]%n==0){
    20                 flag=i;
    21             }
    22         } 
    23         if(flag){
    24             printf("%d
    ",flag);
    25             for(int i=1;i<=flag;i++){
    26                 printf("%d
    ",arr[i]);
    27             }
    28             continue;
    29         }
    30         for(int i=1;i<=n;i++){
    31             if(vis[sum[i]%n]){
    32                 int ans=i-(vis[sum[i]%n]);
    33                 printf("%d
    ",ans);
    34                 for(int j=vis[sum[i]%n]+1;j<=i;j++){
    35                     printf("%d
    ",arr[j]);
    36                 }    
    37                 break;
    38             } 
    39             vis[sum[i]%n]=i;
    40         }    
    41     }
    42 
    43     return 0;
    44 }
    45 
    46 /*
    47 5
    48 4 3 4  3
    49 4 7 11 14
    50 
    51 */
  • 相关阅读:
    启动mysql时显示:/tmp/mysql.sock 不存在的解决方法
    python在使用MySQLdb模块时报Can't extract file(s) to egg cacheThe following error occurred while trying to extract file(s) to the Python eggcache的错误。
    文档发布测试
    我的日常
    CSS-基础优化策略
    Git-基本操作
    Git-配置SSH公钥
    Git-免密提交
    Wx-mpvue开发小程序
    Vue-移动端开发全家桶
  • 原文地址:https://www.cnblogs.com/pengge666/p/11567455.html
Copyright © 2011-2022 走看看