【學習ios之路:C語言】If迴圈的應用的練習

來源:互聯網
上載者:User

標籤:style   io   color   os   使用   sp   on   bs   amp   

1.求三個數中的最大值

  <span style="font-size:14px;"></span><pre name="code" class="cpp"><span style="font-size:14px;">    //方法1:先找到兩個數的最大值,然後用最大值和第三個進行比較.</span>    int  n1 = 0, n2 = 0 ,n3 = 0 ;    printf("請輸入三個數:\n");    scanf("%d%d%d", &n1, &n2, &n3);        int max = 0;//定義一個最大值,初始值為0    if (n1 > n2) {        max = n1;    } else {        max = n2;    } //用條件運算式替換 為  max = n1 > n2 ? n1 : n2;    if (max < n3) {        max = n3;    }// max = max > n3 ? max : n3;    printf("最大值為:max = %d", max);    //方法2:用n1分別和 n2 ,n3 比較.<pre name="code" class="cpp"><pre name="code" class="cpp">    int  n1 = 0, n2 = 0 ,n3 = 0 ;    printf("請輸入三個數:\n");    scanf("%d%d%d", &n1, &n2, &n3);    int max = 0 ;    if (n1 >n2) {        if (n1 > n3) {            max = n1;        } else {            max = n3;        }    } else if ( n2 > n3) {        max = n2;    } else {        max = n3;    }//相當於 max = n1 > n2 ? n1 > n3 ? n1 : n3 : n2 ? n3 : n2 : n3    printf("最大值為:max = %d", max);




 
  

 2.輸入三個數,用兩種方法列印出中間值(即第二大值)
  提示:第一種,先求最大最小;第二種,只使用條件運算子

      //方法1     float x = 0.0;     float y = 0.0;     float z = 0.0;     float max = 0.0;//定義最大值     float min = 0.0;//定義最小值     float mid = 0.0;//定義中間值     printf("please input three number:\n");     scanf("%f%f%f", &x, &y, &z);         max = x > y ? x > z ? x : z : y > z ? y : z;    min = x < y ? x < z ? x: z : y < z ? y : z;        mid = x + y + z - min - max;    printf("the mid number is: %.2f\n", mid);          //方法2         float x = 0.0;    float y = 0.0;    float z = 0.0;    float max = 0.0;//定義最大值    float min = 0.0;//定義最小值    float mid = 0.0;//定義中間值    printf("please input three number:\n");    scanf("%f%f%f", &x, &y, &z);    if (x >= y) {        if (y >= z ) {            mid = y;        } else {            if (x <= z) {                mid = x;            } else {                mid = z;            }        }    } else {        if (y <= z) {            mid = y;        } else if (x > z) {            mid = y;        } else {            mid = x;        }    }    printf("the mid number is: %.2f\n", mid);    



【學習ios之路:C語言】If迴圈的應用的練習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.