题目链接
思路:
判断是否全部相等,如果全部相等则输出n,否则输出1.
代码:
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N=2e6+7;
const int M=2e4+5;
const double eps=1e-8;
const int mod=998244353;
const int inf=0x7fffffff;
const double pi=3.1415926;
using namespace std;
int a[N];
signed main()
{
IOS;
int t;
cin>>t;
while(t--)
{
int n,ans=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(i>0&&a[i]!=a[i-1])
{
ans=1;
}
}
if(ans)
{
cout<<1<<endl;
}
else
{
cout<<n<<endl;
}
}
return 0;
}
判断元素一致性算法
本文介绍了一种简单而有效的算法,用于判断一组元素是否完全一致。通过遍历元素数组并比较相邻元素,该算法能快速确定所有元素是否相等。若发现不等,则输出1;若全部相等,则输出元素总数。
346

被折叠的 条评论
为什么被折叠?



