随机函数产生30个200-300的随机整数存放在数组a中,找出其中的偶数放在数组b中,并对b数组的值按降序排列

如题所述

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.apache.commons.collections.CollectionUtils;

public class Test {

public static void main(String[] args) {
int num=30;//随机数个数
Random r = new Random();
int[] a=new int[num];
int[] b=null;
List<Integer> osNum=new ArrayList<Integer>();//偶数
for(int i=0;i<num;i++){
a[i]=r.nextInt(100)+200;
if(a[i]%2==0){
osNum.add(a[i]);
}
}
System.out.println("--------------------");
//排序
if(CollectionUtils.isEmpty(osNum)){
System.out.println("没有产生随机偶数");
}else{
Collections.sort(osNum);//升序
int count=osNum.size();
b=new int[count];
//降序
for(int j=0;j<count;j++){
b[count-1-j]=osNum.get(j);
}
}
}

}
这个是java 根据这个逻辑 c语言应该没有问题
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-05-31
若30个数据在A3~A32位置=RANDBETWEEN(200,300),则可以B3单元格输入公式:
是数组公式:需要用chtrl+shift+enter这三键同时
=IFERROR(LARGE(IF(MOD(($A$3:$A$32),2)=0,$A$3:$A$32),ROW(A1)),"")
复制并下拉即可。