• PTA題目的處理(二)


    題目7-1 計算分段函數[1]

    1.實驗代碼

    #include <stdio.h>
    int main()
    {
      float x,y;
      scanf("%f",&x);
      if(x==0)
      {
        y=0;
        printf("f(%.1f) = %.1f",x,y);
      }
      else
      {
        y=1/x;
        printf("f(%.1f) = %.1f",x,y);
      }
      return 0;
    }

    2.設計思路

    (1)第一步:輸入實數自變量x

    第二步:若自变量x=0,则实数因变量y=0,否则因变量y=1/x

    第三步:输出f(x)=y(x和y各保留一位小数)

    (2)

    3.調試過程中無問題

    題目7-2 A除以B

    1.實驗代碼

    #include <stdio.h>
    int main()
    {
      signed int A,B;
      double C;
      scanf("%d %d",&A,&B);
      if(B==0)
      {
        printf("%d/%d=Error",A,B);
      }
      else if(B>0)
      {
        C=(double)A/B;
        printf("%d/%d=%.2f",A,B,C);
      }
      else
      {
        C=(double)A/B;
        printf("%d/(%d)=%.2f",A,B,C);
      }
      return 0;
    }

    2.設計思路

    (1)第一步:輸入整數A,B

    第二步:若B=0,則輸出A/B=Error,否則下一步

    第三步:實數C=A/B

    第四步:若B>0,輸出A/B=C(C保留兩位小數),否則輸出A/(B)=C(C保留兩位小數)

    (2)

     

    3.調試過程中無問題

    題目7-6 階梯電價

    1.實驗代碼

    #include <stdio.h>
    #define sold 26.5
    int main()
    {
      float consumption,cost;
      scanf("%f",&consumption);
      if(consumption<=50&&consumption>=0)
      {
        cost=consumption*0.53;
        printf("cost = %.2f",cost);
      }
      else if(consumption<0)
      {
        printf("Invalid Value!");
      }
      else
      {
        cost=(consumption-50)*0.58+sold;
        printf("cost = %.2f",cost);
      }
    }

    2.設計思路

    (1)第一步:輸入實數用電量consumption,令使用50千瓦時的電價sold=26,5

    第二步:若0<=consumption<=50,則實數電價cost=consumption*0.53,然後輸出cost=cost(後一個cost保留兩位小數),否則下一步

    第三步:若consumption=<0,則輸出Invalid Value!,否則下一步

    第四步:cost=(consumption-50)*0.58+sold,輸出cost=cost(cost保留兩位小數)

    (2)

    3.調試過程中無問題

    題目7-7 出租車計價

    1.實驗代碼

    #include <stdio.h>
    #define price1 10.0f
    #define price2 24.0f
    int main()
    {
      float distance,cost1,cost2;
      int minute;
      scanf("%f %d",&distance,&minute);
      if(distance<=3.0f)
      {
        cost1=price1;
      }
      else if(distance<=10.0f)
      {
        cost1=price1+(distance-3)*2.0f;
      }
      else
      {
        cost1=price2+(distance-10)*3;
      }
      if(minute>=5)
      {
        if((minute%5)==0)
        {
          cost2=cost1+(minute/5)*2.0f;
          printf("%.0f",cost2);
        }
        else
        {
          cost2=cost1+(minute/5)*2;
          printf("%.0f",cost2);
        }
      }
      else
      {
        cost2=cost1;
        printf("%.0f",cost2);
      }
      return 0;
    }

    2.設計思路

    (1)第一步:令起步價price1=10,超10公里後的價格price2=24,輸入實數路程distance、整數時間分鐘minute

    第二步:若distance<=3,則路程費cost1=price1,跳到第四步,否則下一步

    第三步:若distance<=10,則cost1=price1+(distance-3)*2,跳到第四步,否則cost1=price2+(distance-10)*3,跳到第四步

    第四步:若minute>=5,則總車價cost2=cost1+(minute/5)*2,否則cost2=cost1

    第五步:輸出cost2(cost2四捨五入取整數)

    (2)

    3.問題1:沒搞清楚超出10km是什麼意思,不知道是在3km的基礎上再超出10km還是就只行駛了10km

    解決辦法:通過提交答案判斷正誤知道是只是行駛了10km

    問題2:不知道題目中少於5分鐘的到底是什麼意思,是總時間小於5分鐘的不算錢,還是5分鐘的都算完後剩下不滿5分鐘的不收費

    解決辦法:通過提交答案知道多出來的不滿5分鐘和總時間小於5分鐘的都不算錢

    Git地址:https://git.coding.net/MemoriesBook/123.git

    代碼行數 時間 博客字數 時間 涉及知識點
     200 10.21  0  3  if語句
     1000 10.22  1000  7  if語句
     20 10.23  0  1.5  if語句
     0 10.24  0  0  無
     100 10.25  0  1.5  if語句,for語句,輸入輸出
     0 10.26  0  0  無
     0 10.27  1000  2  無
     0 10.28  100  4  無
     0 10.29  0  0  無

    馮子旋的博客地址:http://www.cnblogs.com/fengzx/p/7745112.html

    董欣的博客地址:http://www.cnblogs.com/dx2017/p/7726120.html

    董雅潔的博客地址:http://www.cnblogs.com/exo123/p/7737213.html

     ***學習終結**本週主要學習了if語句的else if的部分,還講了部分PTA的題目,同時在上課時老師稍微提了一下for語句,為了讓我們知道if語句是一條語句,並不是多條語句。遇到的問題是:if後面是不是一定要加else,客上講的例子有些沒有else也是對的,但是PTA上做的時候else if裡面是一定要加else的。

  • 相关阅读:
    设计模式-11-代理模式
    设计模式-10-装饰者
    设计模式-9-组合
    设计模式-8-适配器
    设计模式-7-原型模式
    设计模式-6-建造者
    设计模式-5-单例模式
    u-boot-1.1.6 设置新分区支持设备树
    u-boot-2014.10移植(8)重定位,支持NAND启动
    u-boot-2014.10移植(7)修改环境变量的存储位置
  • 原文地址:https://www.cnblogs.com/lixiaojing/p/7711650.html
Copyright © 2020-2023  润新知