ABC 234 E - Arithmetic Number(暴力枚举/思维)

https://atcoder.jp/contests/abc234/tasks/abc234_e

题目大意:给定一个数字n,让我们求出>=n的一个满足n[2]-n[1]==n[3]-n[2]==n[4]-n[3]==...==n[n]-n[n-1]的最接近n的数字。
Sample Input 1 
152
Sample Output 1 
159

Sample Input 2  
88
Sample Output 2  
88 

Sample Input 3  
8989898989
Sample Output 3  
9876543210

因为要有规律的用[0,9]填满数字的每一个空,所以我们可以枚举方差,
在填满17位的路上,顺道比较一下最接近n的数字即可

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200200,M=2002;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL n;
    cin>>n;
    LL ans=1e18;
    for(int i=0;i<10;i++)//这是数字的头
    {
        for(int j=0;j<10;j++)//方差范围[-9,9]
        {
            int d=j-i;//方差
            int tmp=i;//数字的头
            //接下来就是用这个数字为头,这个方差不断扩展它的长度

            LL sum=0;//记录一下能够合成的数据
            for(int k=0;k<18;k++)//题目要求最长是17位
            {
                if(tmp<0||tmp>9) break;//加或减成了非[0,9]的数字
                sum=sum*10+tmp;//一直向后加长尾巴
                tmp+=d;
                //cout<<sum<<endl;
                if(sum>=n) ans=min(ans,sum);//它在变的同时,我看看有没有和我接近的
            }
        }
    }
    cout<<ans<<endl;//在比较完所有数字之后输出最接近我的那个
    return 0;
}

posted @ 2022-09-04 10:41  高尔赛凡尔娟  阅读(22)  评论(0编辑  收藏  举报