1.27 Daisy Chains(花瓣)

问题 L: Daisy Chains
题目描述
Every day, as part of her walk around the farm, Bessie the cow visits her favorite pasture, which has N flowers (all colorful daisies) labeled 1…N lined up in a row (1≤N≤100). Flower i has pi petals (1≤pi≤1000).
As a budding photographer, Bessie decides to take several photos of these flowers. In particular, for every pair of flowers (i,j) satisfying 1≤i≤j≤N, Bessie takes a photo of all flowers from flower i to flower j (including i and j).

Bessie later looks at these photos and notices that some of these photos have an “average flower” – a flower that has P petals, where P is the exact average number of petals among all flowers in the photo.

How many of Bessie’s photos have an average flower?
输入
The first line of input contains N. The second line contains N space-separated integers p1…pN.
输出
Please print out the number of photos that have an average flower.
样例输入 Copy
4
1 1 2 3
样例输出 Copy
6
提示
Every picture containing just a single flower contributes to the count (there are four of these in the example). Also, the (i,j) ranges (1,2) and (2,4) in this example correspond to pictures that have an average flower.

大意就是在给定的花中找一个子区间,使子区间花瓣的平均数在子区间中存在,首选写的是循环,和上次求子串的题感觉有些类似,贴循环的代码

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,a[110];
    int i,ans=0,j,cnt=1,sum=0;
    double ave;
    cin>>n;
    ans=n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(i=1;i<=n;i++)
    {
        while(i+cnt<=n)
        {
            for(j=i;j<=i+cnt;j++)
            {
                sum+=a[j];
            }
            ave=sum*1.0/(cnt+1);
            for(j=i;j<=i+cnt;j++)
            {
                if(a[j]==ave)
                {
                    ans++;
                    break;
                }
            }
            cnt++;
            sum=0;
        }
        if(i+cnt>n)
        {
            cnt=1;
            continue;
        }
    }
    cout<<ans;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值