• 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 */
  • 相关阅读:
    asp.net发布网站的时候三个选项
    Web Service 异常处理
    Asp.net 缓存技术总结
    自定义控件:广告内容后期加载。以及NamingContainer层次的应用
    图片显示时加水印(不改变原图片)
    .NET 2.0中的企业库异常处理块简述
    .NET反射、委托技术与设计模式
    三角函数Table.AddColumn(Power Query 之 M 语言)
    统计信息Table.AddColumn(Power Query 之 M 语言)
    提取Table.AddColumn(Power Query 之 M 语言)
  • 原文地址:https://www.cnblogs.com/pengge666/p/11567455.html
Copyright © 2020-2023  润新知