小米课
返回首页 | 淘宝问答 | 提交答案
关键字: 赤脚医生 优秀典型 理性情绪 情绪疗法 三个阶段 标杆管理 少数民族 省份 | 时间:2024-09-16 08:20

发布时间:2024-09-16 08:20

| 人浏览

【答案】C语言程序设计练习题1

练习题1


1. 已知三角形的底和高,求出三角形的面积。


#include "stdio.h"

void main()

{

int x,y;

float s;

x=4;

y=5;

s=x*y/2.0;

printf("//n s=%f",s);

}



2. 已知三角形的三边长,求出三角形的面积。


#include "stdio.h"

#include "math.h"

void main()

{

int a,b,c;

float p,s;

a=3;

b=4;

c=5;

p=(a+b+c)/2.0;

s=sqrt(p*(p-a) *(p-b) *(p-c));

printf("//n s=%f",s);

}



3. 已知二元一次方程的三个系数,求方程的一个根。


#include "stdio.h"

#include "math.h"

void main()

{

int a,b,c;

float root;

a=3;

b=4;

c=5;

root=(-b-sqrt(pow(b,2)-4*a*c))/(2.0*a);

printf("//n root=%f",root);

}



4. 编程实现符号函数。当x <0 ,则sgn(x)=-1, 当x >0 ,则sgn(x)=+1, 当x =0 ,则sgn(x)=0


#include "stdio.h"

void main()

{

float x;

int y;

scanf("%f",&x);

if (x>0) y=1;

if (x==0) y=0;

if (x<0) y=-1;

printf("//n x=%f ,sgn(x)=%d",x,y);

}


或者:


#include "stdio.h"

void main()

{

float x;

int y;

scanf("%f",&x);

if (x>0) y=1;

else if (x==0) y=0;

else y=-1;

printf("//n x=%f ,sgn(x)=%d",x,y);

}


或者:


#include "stdio.h"

void main()

{

float x;

int y;

scanf("%f",&x);

y=(x>0) ? 1: (x==0) ? 0 : -1;

printf("//n x=%f ,sgn(x)=%d",x,y);

}


答案有错

上一篇:电大《C语言程序设计A》练习题4

下一篇:暂无

小米课手机端XML联系我们