m0_57340823
呆头。
采纳率50%
2021-05-09 12:18 阅读 36

球球大佬了!! 一据数组中有10个0~100之间的随机整数,编程求这10个数的最大值、最小值、平均值

球球大佬了!! 一据数组中有10个0~100之间的随机整数,编程求这10个数的最大值、最小值、平均值,并输出小于平均数的数。
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    bosaidongmomo bosaidongmomo 2021-05-09 18:47
    import java.util.*;
    class Test{
    private int x=0; //循环初值
    private int max=0;//保存最大值
    private int min=0;//保存最小值
    //保存随机整数的集合
    private LinkedList<Integer> arr=new LinkedList<Integer>();
    private int calc(){
    //产生10个整数
    while(x<10){
    int random=(int)(Math.random()*100+1);
    arr.add(random);
    x++;
    }
    //按题目要求,转换为你要的数组
    Object[] arrs=arr.toArray();
    //求出最大值
    max=Collections.max(arr);
    //求出最小值
    min=Collections.min(arr);
    //求出平方和
    return max*max+min*min;
    }
    public static void main(String[] args){
    //输出平方和
    System.out.println(new Test().calc());
    }
    }
    点赞 评论 复制链接分享
  • bosaidongmomo bosaidongmomo 2021-05-09 12:19

    用什么语言

    点赞 1 评论 复制链接分享
  • benbenli benbenli 2021-05-09 13:42

    Answered in C:

    
    // 数组中有10个0~100之间的随机整数,编程求这10个数的最大值、最小值、平均值,并输出小于平均数的数。
    
    #include <stdio.h>
    #include <math.h>
    #include <time.h>
    
    #define MAX 100
    #define COUNT 10
    
    int main()
    {
        int n[COUNT];
        int s = 0;
        float a;
        time_t t;
        // Intializes random number generator
        srand((unsigned) time(&t));
         
        printf("Generating 10 random numbers in range 0..100: \n");
        for (int i = 0; i < COUNT; ++i) {
            n[i] = rand() % (MAX + 1);
            s += n[i];
            printf("%d ", n[i]);
        }
        printf("\n");
        
        // bubble short
        for (int i = 0; i < COUNT - 1; ++i)
            for (int j = i + 1; j < COUNT; ++j)
                if (n[i] > n[j]) 
                {
                    int t = n[i];
                    n[i] = n[j];
                    n[j] = t;
                }
        
        printf("The numbers sortded in ascending order: \n");
        for (int i = 0; i < COUNT; ++i) {
            printf("%d ", n[i]);
        }
        printf("\n");
        
        printf("The maximal number: %d\n", n[COUNT - 1]);
        printf("The minimal number: %d\n", n[0]);
    
        a = (float)s / COUNT;
        printf("The average number: %f\n", a);
    
        printf("The numbers below average: \n");
        for (int i = 0; i < COUNT; ++i) {
            if (n[i] < a) {
                printf("%d ", n[i]);
            }
            else {
                break;
            }
        }
        printf("\n");
    
        return 0;
    }
    
    
    // Output:
    Generating 10 random numbers in range 0..100:                                                                                                                                        
    5 6 82 4 21 45 67 57 85 82                                                                                                                                                           
    The numbers sortded in ascending order:                                                                                                                                              
    4 5 6 21 45 57 67 82 82 85                                                                                                                                                           
    The maximal number: 85                                                                                                                                                               
    The minimal number: 4                                                                                                                                                                
    The average number: 45.400002                                                                                                                                                        
    The numbers below average:                                                                                                                                                           
    4 5 6 21 45

     

     

    点赞 评论 复制链接分享

相关推荐