zoukankan      html  css  js  c++  java
  • 小L的区间求和

    在给定的一个整数序列中,小L希望找到一个连续的区间,这个区间的和能够被k整除,请你帮小L算一下满足条件的最长的区间长度是多少。
    输入
    第一行输入两个整数n、k。(1 <= n <= 105,1<=k<100)
    接下来一行输入n个整数,表示序列中的数。
    输出
    输出一个整数,满足条件区间的最长长度,如果不存在,输出0
    样例输入 Copy
    5 7
    1 2 4 1 1
    样例输出 Copy
    3
    这是一个有技巧性的题目,我们需要开一个数组,前缀“模”,然后当一个余数出现两次的时候说明,期间差一个模,也就是我们的K,当为0的时候说明从第一项到这一项的和为K的倍数
    所以我们需要记录每一个余数出现的位置,当再次出现的时候直接相减
    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    const int N=1e5+7;
    int arr[N];
    int s[N];
    int pre[N];
    int main(){
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            scanf("%d",&arr[i]);
            s[i]=(s[i-1]+arr[i])%m;
            pre[s[i]]=-1;
        }
        pre[0]=0;
        int ans=0;
        for(int i=1;i<=n;i++){
            if(pre[s[i]]==-1){
                pre[s[i]]=i;
            }
            else {
                ans=max(ans,i-pre[s[i]]);
            }
        }
        cout<<ans<<endl;
        
        
        return 0;
    } 

     http://poj.org/problem?id=2356

    POJ的这个题目跟这个题有点类似

    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
    题意 :要求要是求长度,但是要求输出长度和数字,随便输出一个就行(不要求最长)。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    const int N=1e5+7;
    int arr[N];
    int sum[N];
    int pre[N];
    int main(){
        int n;
        scanf("%d",&n);
        
        for(int i=1;i<=n;i++){
            scanf("%d",&arr[i]);
            sum[i]=(sum[i-1]+arr[i])%n;
            pre[i]=-1;
        }
        pre[0]=0;
        bool check=false;
        for(int i=1;i<=n;i++){
            if(pre[sum[i]]==-1){
                pre[sum[i]]=i;
            }
            else {
                check=true;
                printf("%d
    ",i-pre[sum[i]]);
                for(int j=pre[sum[i]]+1;j<=i;j++){
                    printf("%d
    ",arr[j]); 
                } 
                break;
            }
        }
        if(!check) puts("0");
        return 0;
    }
  • 相关阅读:
    卖家必须了解的库存数据,亚马逊库存报告都帮你整理好了
    Genymotion 无法安装 APK 解决方案
    这张系统架构图画的漂亮!
    IT项目风险大全
    ElasticSearch是一个基于Lucene的搜索服务器
    商业模式的定义、商业模式的好坏
    如何实现互联网+业务与IT的融合
    UBUNTU PHP 版本切换
    Ubuntu为PHP安装SOAP扩展
    实现Linux下的ls -l命令
  • 原文地址:https://www.cnblogs.com/Accepting/p/11360555.html
Copyright © 2011-2022 走看看