2023年12月5日 星期二

[1399] Count Largest Group

這題是不是太無聊了,怎麼用C寫的人那麼少XD

但一開始的時候有點卡住QQ 看完解答豁然開朗(?)
原來就一個一個下去算就是了,原來不會超過時間XD
那就降吧

int countSum(int n)
{
int ret = 0;
while (n>0)
{
ret += n%10;
n/=10;
}
return ret;
}

int countLargestGroup(int n) {
int *count = calloc (37, sizeof (int));
int max = 0, ret=0;
for (int i=1;i <=n ; i++)
{
int idx = countSum(i);
count[idx]++;
if (count[idx]>max)
{
max = count[idx];
ret=1;
}
else if (count[idx] == max)
ret++;
}
return ret;
}

沒有留言:

張貼留言