零基础物联网学习(物联网学习教程)
零基础物联网学习(物联网学习教程)① a=0,不是二次方程。基本的算法:例: 求a+bx+c=0方程的解。
例: 写程序,判断某一年是否闰年。
用下图来表示判断闰年的算法。
#include <stdio.h>
void main()
{int year leap;
scanf("%d" &year);
if (year%4==0)
{if (year0==0)
{if (year@0==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*w*s*(1-d)
分析折扣变化的规律性:
折扣的“变化点”都是250的倍数,在横轴上加一种坐标c,c的值为s/250。
c代表250的倍数。
c<1,无折扣;
1≤c<2,折扣d=2%;
2≤c<4,d=5%;
4≤c<8,d=8%;
8≤c<12,d=10%;
c≥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=.4f\n" f);}