A - Horizon
套公式直接输出即可,注意读入h时不要用int,否则在计算h*(12800000+h)时会溢出变成负数,导致nan。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
double h;
cin >> h;
double ans = sqrt(h * (12800000 + h));
printf("%.9f", ans);
return 0;
}
B - Integer Division
简单的运算,正数或者可以被10整除的数可以直接除10,因为C++默认向下取整,对于其他情况则需要再额外减一。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
long long x;
cin >> x;
if (x >= 0 || x % 10 == 0)
x /= 10;
else
x=x/10-1;// x /= 10 - 1不对
cout << x;
return 0;
}
C - Knight Fork
1.根据题目给出的所有8个点,求出任意两个点的距离,这些就是所有成立的情况了。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
long long x1, x2, y1, y2;
cin >> x1 >> y1 >> x2 >> y2;
long long distence = (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
if (distence == 4 || distence == 2 || distence == 16 || distence == 18 || distence == 10 || distence == 20)
printf("Yes\n");
else
printf("No\n");
return 0;
}
2.对其中一个点(x1,y1)进行一次“马步”操作,判断是否存在一个点到(x2,y2)的距离是根号5。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
long long x1, x2, y1, y2;
bool condition = false;
cin >> x1 >> y1 >> x2 >> y2;
int dx[8] = {2, 2, 1, 1, -1, -1, -2, -2};
int dy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
for (int i = 0; i < 8; i++)
{
int xi = x1 + dx[i];
int yi = y1 + dy[i];
if ((abs(xi - x2) == 2 && abs(yi - y2) == 1) || (abs(xi - x2) == 1 && abs(yi - y2) == 2))
{
condition = true;
break;
}
}
if (condition)
printf("Yes");
else
printf("No");
return 0;
}