问题:长和宽已知的两个矩形A和B,判断B能否放入到A里面。
下面是用C语言的实现版本V0.1:(若有错误恳请斧正
)

我的解答
#include <math.h>
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#endif

#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
int main(int argc, char* argv[])


{
double lA, wA, lB, wB;
if(argc > 4)

{
lA = atof(argv[1]);
wA = atof(argv[2]);
lB = atof(argv[3]);
wB = atof(argv[4]);
}
else

{
printf("Input the length and width of two rectangles:\n");
scanf("%lf %lf %lf %lf", &lA, &wA, &lB, &wB);
}

if(min(lA, wA) < min(lB, wB))

{
printf("0 Can't!\n");
return 0;
}

double tmp = (lA * lA + wA * wA - lB * lB - wB * wB)/(2*lB*wB);
if(tmp < 0.0 )

{
printf("1 Can't!\n");
return 0;
}
if(tmp > 1.0)

{
printf("OK!\n");
return 0;
}
double arcsin = asin(tmp)/2.0;
if(arcsin < 0.000001 || arcsin > 3.141593/2.0)

{
printf("2 Can't!\n");
return 0;
}
double lAmin = lB * cos(arcsin) + wB * sin(arcsin);
double wAmin = lB * sin(arcsin) + wB * cos(arcsin);
if(lA < lAmin || wA < wAmin)

{
printf("3 Can't!\n");
return 0;
}

printf("OK!\n");

return 0;
}


posted on 2005-11-01 11:16
力为 阅读(392)
评论(7) 编辑 收藏 引用 所属分类:
C++ Practice