POJ 2100

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40924940/article/details/83592471

 本题题目意思
一个国王想要造墓地,目前需要面积为 N ,目前又连续边长为 1 2 3 4 5.....n 的正方形,求几个连续区间 是的满足条件,输出区间个数,输出区间长度。输出区间。

还是尺取法,当然 这次用vector储存,用 pair 来保存区间 这种方法 我在网上看到的,眼前一亮。

扫描二维码关注公众号,回复: 3951773 查看本文章
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <cmath>
#include <vector>
#define ll long long

using namespace std;

int main()
{
    ll n;
    cin>>n;
    int ans = 0;
    vector<pair<int,int> >p;
    int l , r;ll t = 0;
    l = r = 1;
    while(true)
    {
        while(t < n && r*r <= n) 
        {
            t += r*r;
            r++;
        }
        if(t == n) 
            p.push_back(make_pair(l,r));
        if(t < n)
        break;
        t -= l*l;
        l++;
    }
    cout<<p.size()<<endl;
    for(int i = 0;i < p.size();i++)
    {
        printf("%d",p[i].second-p[i].first);
        for(int j = p[i].first;j < p[i].second;j++)
        {
            printf(" %d",j);
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40924940/article/details/83592471