Solution
a [ i ] a[i] a[i]等价于 a [ i ] + i a[i]+i a[i]+i,若有数重复出现,则是 S a d Sad Sad,否则是 H a p p y Happy Happy
Code
#include<bits/stdc++.h>
using namespace std;
int i,x,n;
map<int,int>mp;
inline char gc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd(){
int x=0,fl=1;char ch=gc();
for (;ch<48||ch>57;ch=gc())if(ch=='-')fl=-1;
for (;48<=ch&&ch<=57;ch=gc())x=(x<<3)+(x<<1)+(ch^48);
return x*fl;
}
int main(){
n=rd();
for (i=0;i<n;i++){
x=rd()+i;
if (mp[x]++) return puts("Sad"),0;
}
puts("Happy");
}
本文介绍了一种算法,用于判断一系列整数中是否存在重复元素。若存在,则称该数列是悲伤数;若不存在,则称其为快乐数。算法通过使用哈希映射来跟踪每个元素是否出现过,从而实现高效判断。

739

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



