作业帮 > 综合 > 作业

C语言挑战题目!编写产生210个2.300到1.800范围内的随机数的程序,并且以降序排序

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/10/04 06:46:45
C语言挑战题目!编写产生210个2.300到1.800范围内的随机数的程序,并且以降序排序
如题!编写产生210个2.300到1.800范围内的随机数的程序,并且以降序排序!
程序输出如下:
2.235
2.234
2.225
2.214
2.211
.
.
1.921
1.916
1.834
1.827
1.814
1.808
1.800 ------------------------------------------结束!
是不是很难啊!哈哈哈!会的直接上程序!
C语言挑战题目!编写产生210个2.300到1.800范围内的随机数的程序,并且以降序排序
#include <stdio.h>
#include<time.h>
#include <stdlib.h>
#define N 210
int cmp(const void* a,const void* b)
{
 return *(int*)b-*(int*)a;
}
int main()

 double rd[N];
 int ri[N];
 int i;
 srand(time(0));
 for(i=0;i<N;i++)
 {
  ri[i]=rand()%501+1800;
 }
 qsort(ri,N,sizeof(int),cmp);
 for(i=0;i<N;i++)
 {
  rd[i]=ri[i]/1000.0;
  printf("%2.3f  ",rd[i]);
  if((i+1)%10==0)   //每行10个
   printf("\n");
 }
}
结果:
2.297  2.297  2.292  2.285  2.278  2.278  2.278  2.277  2.271  2.271
2.268  2.267  2.265  2.265  2.264  2.263  2.252  2.251  2.243  2.242
2.239  2.236  2.234  2.227  2.222  2.220  2.214  2.211  2.210  2.210
2.208  2.199  2.194  2.193  2.192  2.190  2.186  2.178  2.174  2.173
2.164  2.164  2.161  2.160  2.160  2.160  2.157  2.155  2.150  2.141
2.137  2.136  2.132  2.132  2.131  2.127  2.123  2.120  2.120  2.119
2.116  2.116  2.115  2.112  2.101  2.101  2.098  2.097  2.091  2.091
2.090  2.090  2.090  2.090  2.086  2.085  2.085  2.082  2.081  2.076
2.075  2.074  2.071  2.071  2.069  2.068  2.066  2.062  2.062  2.051
2.051  2.050  2.047  2.042  2.042  2.040  2.036  2.031  2.030  2.029
2.029  2.028  2.028  2.027  2.015  2.014  2.013  2.009  2.008  2.008
2.007  2.005  2.001  2.000  1.995  1.993  1.990  1.990  1.987  1.986
1.985  1.983  1.982  1.979  1.979  1.975  1.973  1.973  1.973  1.972
1.968  1.965  1.960  1.960  1.960  1.958  1.957  1.957  1.956  1.956
1.956  1.953  1.952  1.951  1.949  1.944  1.944  1.944  1.942  1.942
1.941  1.939  1.939  1.938  1.934  1.932  1.932  1.932  1.930  1.924
1.923  1.917  1.917  1.916  1.913  1.913  1.910  1.898  1.897  1.895
1.895  1.892  1.886  1.883  1.880  1.879  1.877  1.873  1.873  1.868
1.868  1.864  1.863  1.862  1.861  1.859  1.858  1.857  1.857  1.856
1.854  1.852  1.850  1.850  1.847  1.843  1.842  1.838  1.833  1.831
1.831  1.828  1.826  1.818  1.818  1.816  1.815  1.815  1.814  1.810
请按任意键继续...