洛谷CF847M 题解
CF847M 明天的天气
根据题意,定义d为公差,有两种情况
\[ans=\left\{\begin{matrix}&a_n+d(\forall a_i-a_{i-1}=d) \\&a_n\end{matrix}\right.
\]
于是我们可以写一个判断函数:
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;
}

浙公网安备 33010602011771号