打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
物联网学习教程——程序举例

例: 写程序,判断某一年是否闰年。

用下图来表示判断闰年的算法。


#include <stdio.h>
void main()
{int year, leap;
 scanf("%d",&year);
 if (year%4==0)
{if (year%100==0)
    {if (year%400==0)  leap=1;
      else leap=0;}
  else  leap=1;}
    else  leap=0;
     
if (leap) printf("%d is ",year);
      else  printf("%d is not ",year);
      printf("a leap year.\n");}



: 求a+bx+c=0方程的解。

基本的算法:

a=0,不是二次方程。

-4ac=0,有两个相等实根。

-4ac>0,有两个不等实根。

-4ac<0,有两个共轭复根。


 #include  <stdio.h>
 #include  <math.h>
     void main ( )   
       {float a,b,c,disc,x1,x2,realpart,imagpart;
       scanf("%f,%f,%f",&a,&b,&c);
       printf("the equation ");
      if(fabs(a)<=1e-6)
printf("is not a quadratic\\n");
       else
      { disc=b*b-4*a*c;
    if(fabs(disc)<=1e-6)
      printf("has two equal roots:%8.4f\n",-b/(2*a));


  else if(disc>1e-6)
{x1=(-b+sqrt(disc))/(2*a);
 x2=(-b-sqrt(disc))/(2*a);
      printf(″has distinct real roots:%8.4f and %8.4f\n″,x1,x2);
 }
 else
{realpart=-b/(2*a);
 imagpart=sqrt(-disc)/(2*a);
      printf(″has complex roots∶\n″);
 printf(″%8.4f+%8.4fi\n″,realpart,imagpart);
 printf(″%8.4f-%8.4fi\n″,realpart,imagpart);
 }
}}

例:运输公司对用户计算运费。

路程(s)越远,每公里运费越低。标准如下:

   s<250km       没有折扣

    250≤s<500          2%折扣

    500≤s<1000        5%折扣

   1000≤s<2000        8%折扣

   2000≤s<3000        10%折扣

   3000≤s                  15%折扣

设每公里每吨货物的基本运费为p,货物重为w,

距离为s,折扣为d,则总运费f的计算公式为:

f=p***(1-d) 

分析折扣变化的规律性:

折扣的“变化点”都是250的倍数

在横轴上加一种坐标c,c的值为s/250

c代表250的倍数。     

<1,无折扣;

≤c<2,折扣d=2%;

≤c<4,d=5%;

≤c<8,d=8%;

≤c<12,d=10%;

    ≥12,d=15%。

#include <stdio.h>
void main ( )
   {int c,s;
float p,w,d,f;
scanf("%f,%f,%d",&p,&w,&s);
if(s>=3000) c=12;
else c=s/250;
switch(c){
  case 0:d=0;break;
  case 1:d=2;break;
  case 2:case 3:d=5;break
  case 4:case 5:case 6:case 7:d=8;break;
  case 8:case 9:case 10:
  case 11:d=10;break;
  case 12:d=15;break; }
f=p*w*s*(1-d/100.0);
printf("freight=%15.4f\n",f);}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
3.11 第一次做伪代码的记录(最基础的算法)
第4章选择结构程序设计
C语言题目和答案
C语言经典程序举例
第五章 选择结构程序设计
《C语言程序设计》第三版课后答案
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服