zoukankan      html  css  js  c++  java
  • A. Even Substrings

    A. Even Substrings

    time limit per test
    0.5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a string s=s1s2sns=s1s2…sn of length nn, which only contains digits 11, 22, ..., 99.

    A substring s[lr]s[l…r] of ss is a string slsl+1sl+2srslsl+1sl+2…sr. A substring s[lr]s[l…r] of ss is called even if the number represented by it is even.

    Find the number of even substrings of ss. Note, that even if some substrings are equal as strings, but have different ll and rr, they are counted as different substrings.

    Input

    The first line contains an integer nn (1n650001≤n≤65000) — the length of the string ss.

    The second line contains a string ss of length nn. The string ss consists only of digits 11, 22, ..., 99.

    Output

    Print the number of even substrings of ss.

    Examples

    input

    4
    1234
    

    output

    6

    input

    4
    2244
    

    output

    10

    Note

    In the first example, the [l,r][l,r] pairs corresponding to even substrings are:

    • s[12]s[1…2]
    • s[22]s[2…2]
    • s[14]s[1…4]
    • s[24]s[2…4]
    • s[34]s[3…4]
    • s[44]s[4…4]

    In the second example, all 1010 substrings of ss are even substrings. Note, that while substrings s[11]s[1…1] and s[22]s[2…2] both define the substring "2", they are still counted as different substrings.

    题意:任取主串中的一个区间,判断这个是减是不是偶数,然后是偶数的话,就数量加1,找出有多少个这样的偶数区间。比如:第一个样例1234,其中12,2,1234,234,34,4为偶数,所以答案是6。其思路是从第一个字符遍历到最后一个字符,如果遇到偶数,就将这个数的位数加入sum中,依次遍历过就行。

    #include<iostream>
    #include<cstdio>
    
    using namespace std;
    
    int main()
    {
        int n;
        cin>>n;
        string s;
        cin>>s;
        int sum=0;
        for(int i=0;i<n;i++)
            if((s[i]-'0')%2==0)    //判断当前数字是不是偶数
                sum+=i+1;            //加上当前数字的位数
        cout<<sum<<endl;
        return 0;
    }
  • 相关阅读:
    WEB版一次选择多个文件进行批量上传(swfupload)的解决方案
    WEB版一次选择多个文件进行批量上传(WebUploader)的解决方案
    WEB上传大文件解决方案
    kindeditor 富文本粘贴 图片
    Kindeditor图片粘贴上传(chrome)
    PHP超大文件下载,断点续传下载
    大文件断点上传 js+php
    常见的消息映射格式总结
    常见的消息映射格式总结
    C++项目中的extern "C" {}
  • 原文地址:https://www.cnblogs.com/buhuiflydepig/p/10632609.html
Copyright © 2011-2022 走看看