RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 943017
Accepted
Battrip
Battrip
Asked:2020-02-10 05:47:58 +0000 UTC2020-02-10 05:47:58 +0000 UTC 2020-02-10 05:47:58 +0000 UTC

查找数组中的最大值和最小值

  • 772

查找数组中的最小值和最大值。将最大值和最小值存储在单独的变量中。

实现算法:

    String gg = "-65649 -28979 -7098 60269 -20815 -13380 -78850 -7601 -62340 -1522 16584 57888 58337 58659 58809 29382 132 -56100 -14701 74564 11098 -79380 28418 -31457 50295 -44985 40248 -52332 25183 5280 71982 39533 56300 -15116 19802 -44514 51503 20952 27884 69163 -60570 -35531 47052 77766 -56872 25861 27149 23260 49761 -67551 17824 -19140 -66932 -33758 29402 63363 1257 -10350 -68969 -53559 74929 -76987 65973 51230 -12104 5776 -73284 -40600 -53272 34600 -51436 -33843 79068 75615 -36076 -57803 21476 71072 45456 -8762 -76479 -16719 52097 -63411 29523 1499 79951 -49220 71149 -69018 -22780 66078 -66005 -36806 37309 1891 48969 44025 41291 75697 -1374 69854 -38146 -2305 65469 5778 19891 6946 -3150 -14652 78183 371 48628 50281 16959 -1849 -28220 16910 28931 -37071 27892 -73848 -50992 41887 -30655 66317 -36222 -61685 30342 -74930 -65987 -51032 74923 -24134 26663 60392 61644 -33446 -12661 -21506 31901 -14478 58864 530 -44197 -4176 78680 7583 -67265 27612 50512 40627 33764 79520 2514 -76891 65837 46292 -58576 16180 51361 -44564 45148 46285 11302 -8189 26677 -7054 38365 -65983 51440 -9734 -462 30305 70795 35341 -53870 69476 -37075 -41136 17089 -66562 79490 -29147 -67042 2005 -26038 78795 -31702 -4615 14975 -60340 30821 -19876 65944 -37876 51934 12622 35070 10300 26638 6510 -79434 -53823 -43184 71361 61518 -17055 60838 -55557 21808 -2073 -42119 21299 48779 -29162 -56695 -57258 -30367 -8397 18127 64608 11262 -31052 -35267 -2793 11072 -63332 -70170 -33858 26967 36467 52652 27532 62644 -70532 18894 44162 -7588 -267 68605 -65779 77659 -53514 35519 46439 -2676 58824 69181 46957 -29573 7308 31566 61689 56256 76298 -21104 -12672 -67034 -11274 33470 39932 -54806 6122 -12536 -72162 15589 -73641 52000 -71998 6091 40606 -57778 3751 67092 57741 -29809 -15584 36566 -40628 -48626 -73007 46679 62939 68682 22935 59237 -32421 -69736 72202 36304 43733 32134 61497 -30145 -60401 69334 65444 -54042 41335";
    String[] mas = gg.split(" ");

    int[] mas_int = new int[mas.length];
    int counter = 0;

    for (String str : mas) {
        try {
            mas_int[counter] = Integer.parseInt(str);
        } catch (Exception error) {
            System.out.println("str = " + str);
        }
        counter++;
    }

    System.out.println("Колличество символов: " + mas_int.length);
    int max = mas_int[0], min = mas_int[1];

    for (int i = 0, j = 1; j < 300; i += 1, j += 1) {
        if (mas_int[i] >= mas_int[j]) {
            max = mas_int[i];
        }
        if (mas_int[i] < mas_int[j]) {
            min = mas_int[i];
        }
        if (mas_int[j] > mas_int[i]) {
            max = mas_int[j];
        }
        if (mas_int[j] < mas_int[i]) {
            min = mas_int[j];
        }

    }
    System.out.println("Максимум " + max + "\nМинимум " + min);
}

执行代码时,它显示不正确的值。

java
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Z.John
    2020-02-10T06:40:35Z2020-02-10T06:40:35Z

    您可以这样做 - 排序并获取数组的第一个和最后一个值

    Arrays.sort(arrInt);
    int min = arrInt[0];
    int max = arrInt[arrInt.length-1];
    
    • 2
  2. Battrip
    2020-02-10T06:03:16Z2020-02-10T06:03:16Z

    循环错误:

    for (int i = 0; i < mas_int.length; i++) {
        if (max <= mas_int[i]) {
            max = mas_int[i];
        }
        if (min >= mas_int[i]) {
            min = mas_int[i];
        }
    
    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    根据浏览器窗口的大小调整背景图案的大小

    • 2 个回答
  • Marko Smith

    理解for循环的执行逻辑

    • 1 个回答
  • Marko Smith

    复制动态数组时出错(C++)

    • 1 个回答
  • Marko Smith

    Or and If,elif,else 构造[重复]

    • 1 个回答
  • Marko Smith

    如何构建支持 x64 的 APK

    • 1 个回答
  • Marko Smith

    如何使按钮的输入宽度?

    • 2 个回答
  • Marko Smith

    如何显示对象变量的名称?

    • 3 个回答
  • Marko Smith

    如何循环一个函数?

    • 1 个回答
  • Marko Smith

    LOWORD 宏有什么作用?

    • 2 个回答
  • Marko Smith

    从字符串的开头删除直到并包括一个字符

    • 2 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5