博客
关于我
1157 最高分和最低分
阅读量:399 次
发布时间:2019-03-05

本文共 929 字,大约阅读时间需要 3 分钟。

为了解决这个问题,我们需要读取10个整数,找出其中的最大值和最小值,并记录它们在数组中的位置。

方法思路

  • 读取输入:从键盘读取10个整数,存储在一个数组中。
  • 初始化变量:设置最大值和最小值为数组的第一个元素,位置为0。
  • 遍历数组:比较每个元素,更新最大值和最小值及其位置。
  • 输出结果:分别输出最大值及其位置,最小值及其位置。
  • 解决代码

    #include 
    #define N 10int main() { int a[N]; int i, min, max, minloc, maxloc; // 读取输入 for (i = 0; i < N; i++) { scanf("%d", &a[i]); } // 初始化最大值和最小值为第一个元素 min = max = a[0]; minloc = maxloc = 0; // 遍历数组找出最大值和最小值及其位置 for (i = 0; i < N; i++) { if (a[i] > max) { max = a[i]; maxloc = i; } if (a[i] < min) { min = a[i]; minloc = i; } } // 输出结果 printf("%d %d\n", max, maxloc); printf("%d %d\n", min, minloc); return 0;}

    代码解释

  • 读取输入:使用 scanf 函数读取输入并存储在数组 a 中。
  • 初始化变量minmax 初始化为数组的第一个元素,minlocmaxloc 初始化为0。
  • 遍历数组:使用两个循环,分别比较每个元素,更新最大值和最小值以及它们的位置。
  • 输出结果:按照要求输出最大值及其位置,最小值及其位置。
  • 这个方法确保了我们能够高效地找到最大值和最小值,并正确记录它们在数组中的位置。

    转载地址:http://amszz.baihongyu.com/

    你可能感兴趣的文章
    poj 2965 The Pilots Brothers' refrigerator-1
    查看>>
    poj 3026( Borg Maze BFS + Prim)
    查看>>
    POJ 3041 - 最大二分匹配
    查看>>
    POJ 3041 Asteroids(二分匹配模板题)
    查看>>
    Qt笔记——标准文件对话框QFileDialog
    查看>>
    poj 3083 Children of the Candy Corn
    查看>>
    POJ 3083 Children of the Candy Corn 解题报告
    查看>>
    POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)
    查看>>
    Qt笔记——控件总结
    查看>>
    poj 3262 Protecting the Flowers 贪心
    查看>>
    poj 3264(简单线段树)
    查看>>
    Qt笔记——布局管理三件套分割窗口、停靠窗口和堆栈窗口
    查看>>
    poj 3277 线段树
    查看>>
    POJ 3349 Snowflake Snow Snowflakes
    查看>>
    POJ 3411 DFS
    查看>>
    poj 3422 Kaka's Matrix Travels (费用流 + 拆点)
    查看>>
    Qt笔记——官方文档全局定义(二)Functions函数
    查看>>
    POJ 3468 A Simple Problem with Integers
    查看>>
    poj 3468 A Simple Problem with Integers 降维线段树
    查看>>
    poj 3468 A Simple Problem with Integers(线段树 插线问线)
    查看>>