gpt4 book ai didi

c - 使用 C 中的计数出现次数进行 Turbo Sort 排序最多 10^6

转载 作者:行者123 更新时间:2023-11-30 20:35:14 25 4
gpt4 key购买 nike

我们需要接受最多 10^6 个输入并对它们进行排序。

所以我在纸上将整个数组初始化为零,这段代码可以工作,但我发现的问题是,每个数组索引都没有初始化为零,这导致了这个问题,你能帮助我为什么数组的每个索引都没有初始化为零?

int main()
{
unsigned int j,i=0,k,t=0,n,input,flag;
unsigned int a[10^6]={0};//with this method still give garbage value
scanf("%d",&n);
flag=0;
for(k=n;k>=1;k--)
{

scanf("%u",&input);
a[input]=a[input]+1;
if(input>flag)
{
flag=input;
}
}
while(i<=flag)
{
if(a[i]>0)
{
while(a[i])
{
printf("%u\n",i);
a[i]=a[i]-1;
}
}
i++;
}

return 0;
}

如果你想知道问题就在这里 Click here
you can see here i check fresh array for first 25 values and this is the result

最佳答案

BLUEPIXY 在简短回复中解决了该问题

使用

static unsigned int a[1000001]={0};

在初始化部分。
代码已接受。
执行时间=0.19秒

关于c - 使用 C 中的计数出现次数进行 Turbo Sort 排序最多 10^6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40048516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com