fork download
  1. #include <iostream>
  2.  
  3. #include <time.h>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. void distributionsort(int [],int[],int,int);
  10.  
  11.  
  12.  
  13.  
  14.  
  15. int main()
  16.  
  17. {
  18.  
  19. int i;
  20.  
  21. srand(time(NULL));
  22.  
  23. int num[15]={8,3,2,2,1,4,6,5,4,1,3,7,3,7,2};
  24.  
  25. int result[15]={0};
  26.  
  27. cout<<"排序前:";
  28.  
  29. for(i=0;i<15;i++){
  30.  
  31. num[i] = rand() % 10;
  32.  
  33. cout<<num[i]<<" ";}
  34.  
  35.  
  36.  
  37. distributionsort(num,result,15,10);
  38.  
  39.  
  40.  
  41. cout<<endl;
  42.  
  43. cout<<"排序後:";
  44.  
  45. for(i=0;i<15;i++)
  46.  
  47. cout<<result[i]<<" ";
  48.  
  49.  
  50.  
  51. cout<<endl;
  52.  
  53.  
  54.  
  55.  
  56.  
  57. system("pause");
  58.  
  59. return 0;
  60.  
  61. }
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69. void distributionsort(int a[],int b[],int n,int k){
  70.  
  71.  
  72.  
  73. int count[k];
  74.  
  75.  
  76.  
  77.  
  78.  
  79. for(int i=0;i<k;i++)
  80.  
  81. count[i]=0;
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91. for(int i=0;i<n;i++)
  92.  
  93. count[a[i]]++;
  94.  
  95.  
  96.  
  97. for(int i=1;i<k;i++)
  98.  
  99. count[i]=count[i-1]+count[i];
  100.  
  101.  
  102.  
  103.  
  104.  
  105. for(int i=0;i<n;i++){
  106.  
  107. b[--count[a[i]]]=a[i];
  108.  
  109.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:21: error: ‘srand’ was not declared in this scope
prog.cpp:31: error: ‘rand’ was not declared in this scope
prog.cpp:57: error: ‘system’ was not declared in this scope
prog.cpp: In function ‘void distributionsort(int*, int*, int, int)’:
prog.cpp:107: error: expected `}' at end of input
prog.cpp:107: error: expected `}' at end of input
stdout
Standard output is empty