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 */
  • 相关阅读:
    dynamic和匿名对象
    生成1亿个不重复的8位随机整数
    Storage,Memcache,KVDB都是存储服务,如何区分何时用何种服务
    深入理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法
    常用工具
    数字格式化,保留一位小数,无小数用0补充
    学习某些API的方法
    程序员的学习方法(程序员必看)【风中叶老师讲述】
    html的dtd声明
    数据库管理工具navicat基本使用方法——以MySql为例
  • 原文地址:https://www.cnblogs.com/pengge666/p/11567455.html
Copyright © 2011-2022 走看看