洛谷CF847M 题解

CF847M 明天的天气

题目传送门

根据题意,定义d为公差,有两种情况

ans={an+d(aiai1=d)an

于是我们可以写一个判断函数:

bool weather()
{
    d=a[2]-a[1];						//d为公差
    for(int i=2;i<=n;i++)
        if(a[i]-a[i-1]!=d)return false;
    return true;
}

复杂度 O(n),可以通过

AC Code:

#include<bits/stdc++.h>
#define re register
#define MAXN 102
using namespace std;
namespace FastIO
{
	char buf[1<<23],*p1,*p2;
	#ifdef ONLINE_JUDGE
	#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<22,stdin),p1==p2))?EOF:*p1++
	#else
	#define gc() getchar()
	#endif
	inline int read()
    {
        re int f=1,w=0;re char ch=gc();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
        while(ch>='0'&&ch<='9')w=w*10+ch-'0',ch=gc();
        return f*w;
    }
}
using FastIO::read;
int n,a[MAXN],d;
bool weather()
{
    d=a[2]-a[1];
    for(int i=2;i<=n;i++)
        if(a[i]-a[i-1]!=d)return false;
    return true;
}
int main()
{
    n=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    printf("%d\n",weather() ? a[n]+d:a[n]);
    system("pause");
    return 0;
}
posted @   ysl_Endline  阅读(44)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示