出了什么问题,它只输出 0 没有别的
我有 2 种范围为 35 的 B 和范围为 17 的 M,如果他们可以射击,它应该输出 1 但它不会:( 我也没有忘记数学。
int main() {
//i got p o k l and kind1 from user
can_hit(p,o,k,l,kind1);
printf("%d", can_hit(p, o, k, l, kind1));
_getch();
}
double distance(int y, int r, int u, int t) {
return sqrt(((u - y) ^ 2) + ((t - r) ^ 2));
}
int can_hit(int x_0, int y_0, int x_1, int y_1, char kind) {
int w = 17;
int e = 35;
int hit = 0;
double n = distance(x_0, y_0, x_1, y_1);
switch (kind) {
case 'm':
if (w >= n) {
hit = 1;
}
break;
case 'b':
if (e >= n) {
hit = 1;
}
break;
}
return hit;
}
(u - y) ^ 2
不会做你认为它做的事情:^
是按位异或,而不是幂。
使用例如 (u-y)*(u-y)
,或者pow(u-y, 2)
.当然,(t-r) ^ 2
也是如此。