2022年11月9日 星期三

[914] X of a Kind in a Deck of Cards

什麼~~怎麼會有最大公因數這種東西~~~>< (抱頭)
搞的有點久 ><! 而且還有陷阱 >< !
人生好難數學好難>< 
int gcd(int a,int b)
{
if (b==0)
return a;
return gcd(b, a%b);
}

bool hasGroupsSizeX(int* deck, int deckSize){
int* hash=calloc(10001, sizeof(int));

for (int i=0;i<deckSize;i++)
hash[deck[i]]++;

int ans= -1;
for (int i=0;i<10001;i++)
{
if (hash[i]==0)
continue;
if (ans < 0 )
ans = hash[i];
int tmp = gcd(ans,hash[i]);
if (tmp != 1 && tmp != ans)
ans = (tmp < ans)? tmp : ans;
else if (tmp==1)
return false;
}
return true;
}

沒有留言:

張貼留言