计数排序程序在 c 中的特定编译器中显示错误

标签 c sorting counting

我尝试计数排序并在“http://www.tutorialspoint.com/compile_c_online.php”中进行编译,它运行完美,但是当我尝试在“http://codepad.org”中编译时,它说段错误。我尝试使用 gdb,但它没有显示任何错误。 这是代码,任何人都可以找到导致它的行。

#include<stdio.h>
int main(void)
{
long long int t;
int i=0,j,max,min,temp,pos;
scanf("%lld",&t);//enter total numbers to be sorted
long long int a[t];
while(i<t)
    {   
        scanf("%lld",&a[i]);
        if(i==0) max=min=a[i];
        else
        {
            if(a[i]>=max) max=a[i];
            if(a[i]<min) min=a[i];
        }
        ++i;
    }
temp=(max-min+1);
long long int b[temp];
for(i=0;i<t;i++) 
    for(j=min;j<=max;j++)
        {
         if(i==0) b[j-min]=0;
         if(a[i]==j) ++b[j-min];
        }
for(i=0;i<temp;i++) if(i!=0) b[i]=b[i]+b[i-1];
long long int c[t];
for(i=0;i<t;i++)
{
    for(j=0;j<temp;j++)
        {
            if(a[i]==(j+min))
                {
                    pos=(b[j]-1);
                    c[pos]=j+min;
                    --b[j];
                }
        }
}
for(j=0;j<t;j++) printf("%lld\n",c[j]);
return(0);
}

最佳答案

看起来键盘不支持读取用户输入:

#include<stdio.h>
int main(void)
{
long long int t;
scanf("%lld",&t);//enter total numbers to be sorted
printf("%lld\n",t );
return 0;
}

生产

Output:
134513968

没有读取输入,但有一个随机数。有了这个数字,您的程序就会崩溃。这是一个 solution对于 C++。

关于计数排序程序在 c 中的特定编译器中显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38352757/

相关文章:

基于文本创建窗口

java - 使用 ArrayList <Integer> 对整数进行排序

c# - 如何对字母数字中的数字进行排序

javascript - 计算嵌套 Json 内的唯一值

Python:不使用内置函数的二进制计数

c - C 中的字符串查找

c - 如何将数字插入到 C 中的二叉搜索树中?

c - 如何将二维数组传递给函数?

java - Android - 通过多个字段(不同的对象/基元类型)比较自定义对象

algorithm - 计算实时事件频率的替代方法