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 */
  • 相关阅读:
    [Github]在仓库中添加Sponsor赞助者按钮
    SpringMVC面试题常问的29道(附答案)
    Spring常问面试题及答案汇总(2020版)
    Java面试题及答案整理(2020最新版)
    MySQL数据库下载及安装教程(最新版!史上最详细!)
    +0与-0在plc传递数值上的坑
    halcon极坐标转换与亮暗缺陷检测结合的案例(转)
    互联网项目管理
    zabbix 监控IO DISK
    转 更改当前schema
  • 原文地址:https://www.cnblogs.com/pengge666/p/11567455.html
Copyright © 2011-2022 走看看