• 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 */
  • 相关阅读:
    解决SQL Server Compact 无法在64位系统下正常运行
    Mvc5+Entity Framework6 之二----在MVC中用Entity Framework实现基本的CRUD
    Asp.net MVC5中Html.DropDownList的使用
    C# 使用微软的Visual Studio International Pack 类库提取汉字拼音首字母
    MVC学习 (二) Razor语法
    MVC学习 (一)
    编程实现机器人相遇
    jquery优化引发的思考
    (续)检测到有潜在危险的 Request.Form 值
    检测到有潜在危险的 Request.Form 值
  • 原文地址:https://www.cnblogs.com/pengge666/p/11567455.html
Copyright © 2020-2023  润新知