浙大版《C语言程序设计(第3版)》题目集(编程题q31-q40)
习题3-5 三角形判断 (15分)
给定平面上任意三个点的坐标(x1 ,y1 )、(x2 ,y2 )、(x3 ,y3 ),检验它们能否构成三角形。
#include "stdio.h"
#include "math.h"
int main()
{
float x1, x2, x3, y1, y2, y3;
float a, b, c, s, l, area; //定义三边,半周长,周长,面积
scanf("%f%f%f%f%f%f", &x1, &y1, &x2, &y2, &x3, &y3);
a = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
b = sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
c = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2));
if (a + b > c && a + c > b && b + c > a) {
s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c)); //海伦公式求面积
l = a + b + c;
printf("L = %.2f, A = %.2f\n", l, area);
} else {
printf("Impossible\n");
}
return 0;
}
练习4-3 求给定精度的简单交错序列部分和 (15分)
本题要求编写程序,计算序列部分和 1 - 1/4 + 1/7 - 1/10 + … 直到最后一项的绝对值不大于给定精度eps。
#include "stdio.h"
int main()
{
int fenmu, flag; //定义分母,符号判断
double eps, item, sum; //定义正实数
scanf("%lf", &eps);
flag=1;
fenmu