問題:
長(zhǎng)和寬已知的兩個(gè)矩形A和B,判斷B能否放入到A里面。
下面是用C語言的實(shí)現(xiàn)版本V0.1:(若有錯(cuò)誤懇請(qǐng)斧正
)

我的解答
#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;
}


長(zhǎng)和寬已知的兩個(gè)矩形A和B,判斷B能否放入到A里面。
下面是用C語言的實(shí)現(xiàn)版本V0.1:(若有錯(cuò)誤懇請(qǐng)斧正
#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;
}


