zoukankan      html  css  js  c++  java
  • 十只小猪称体重

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<math.h>
    #include<time.h>

    #define SIZE 10

    //通过输入10只小猪的体重,比较后将最重的小猪的体重打印出来

    int main()

    {

      int arr[SIZE];

      for(int i =0; i<SIZE; i++)//这里循环10次的作用是为了下面输入10个元素

      {                     //数组//0  1  2  3  4  5  6  7  8  9

        scanf("%d",&arr [i]);      //数组对应的值//7  5  3  1  2  4  6  8  10 9    

      }

      int max =arr [0];//默认最大值为arr[0]=7;int max=0代码优化int max = arr[0]; int i=0代码优化为int i=1;代码将少执行一层循环

      for(int i =  1; i<SIZE; i++)//这里循环9次的作用是为了依次比较输入的元素

      {

        if(arr [i]>max)//从arr[1]开始与arr[0]=7比较:到arr[7]=8时,arr[7]>arr[0]满足条件,将8赋值给max;到数组8时,满足条件10>8,将10赋值给max。到数组9时,不满足条件,循环结束

        {

          max=arr [i];

        }

      }

      printf("最重的小猪体重是:%d/n",max);

      return 0;

    //结果

    }

  • 相关阅读:
    async/await语法
    generator生成器函数
    数组练习
    解决异步(重点promise函数)
    iterator遍历器
    各种遍历方法(重点for....of)
    ES6代理proxy
    Symbol新数据类型
    函数(rest 箭头)
    ES6常用方法(字符串,数字,数组,对象)
  • 原文地址:https://www.cnblogs.com/wanghong19991213/p/13490711.html
Copyright © 2011-2022 走看看