题目背景
Osu is a very popular music game. Basically, it is a game about clicking. Some points will appear on the screen at some time, and you have to click them at a correct time.
题目
Now, you want to write an algorithm to estimate how diffecult a game is.
To simplify the things, in a game consisting of N points, point i will occur at time ti at place (xi, yi), and you should click it exactly at ti at (xi, yi). That means you should move your cursor from point i to point i+1. This movement is called a jump, and the difficulty of a jump is just the distance between point i and point i+1 divided by the time between ti and ti+1. And the difficulty of a game is simply the difficulty of the most difficult jump in the game.
Now, given a description of a game, please calculate its difficulty.
输入格式
The first line contains an integer T (T ≤ 10), denoting the number of the test cases.
For each test case, the first line contains an integer N (2 ≤ N ≤ 1000) denoting the number of the points in the game. Then N lines follow, the i-th line consisting of 3 space-separated integers, ti(0 ≤ ti < ti+1 ≤ 106), xi, and yi (0 ≤ xi, yi ≤ 106) as mentioned above.
题目简译
杀死地精游戏,计算游戏的难度。
难度:取决于 点i到点i+1的距离除以ti和ti+1之间的时间。一次测试案例的难度就是本次N次测试中最困难的难度。
输入简译
首先输入T (T ≤ 10)表示测试案例的次数。
每次测试案例的第一行输入N(2 ≤ N ≤ 1000) 表示测试点的数目,接下来输入的N行,。每行三个数字 ti(0 ≤ ti < ti+1 ≤ 106), xi, yi (0 ≤ xi, yi ≤ 106)分别表示,时间t,x坐标,y坐标。
样例
输入:
2 //T,测试案例的次数
5 //第一次测试的测试点数目
2 1 9
3 7 2
5 9 0
6 6 3
7 6 0 //t,x,y
10 //第二次测试的测试点数目
11 35 67
23 2 29
29 58 22
30 67 69
36 56 93
62 42 11
67 73 29
68 19 21
72 37 84
82 24 98
输出
9.2195444573 //注意十位小数
54.5893762558
分析
-
T个测试案例,每个案例有N个测试点,嵌套情况,需要用两个循环嵌套。
-
每次需要输入并保存三个数字,t,x,y,使用结构体进行保存这三个数字。
-
计算两个测试点的距离需用sqrt函数,注意函数的数据类型。
函数原型:double sqrt(double x);
头函数:#include <math.h> -
每个案例的难度为所有测试点中的最大难度,所以需要进行比较找到最大值。
rank = rank > rank1 ? rank : rank1;(rank存储最大值)
“?:” 三目运算符,若满足rank > rank1,则rank=rank,否则rank=rank1 -
输出每个案例的难度时需要精确到10位小数。scanf(“%.10f\n”,rank)为保留10位小数;
scanf("%.1f\n",rank),保留一位小数。
代码
#include <stdio.h>
#include <math.h>
struct zuobiao
{
double t;
double x;
double y;
}; //定义结构体,表示“时间+坐标”
int main()
{
int n,n1;
int i,j;
double rank,rank1; //rank表示难度等级,由于最后输出小数,所以数据类型为double
double d; //d计算两个测试点的距离
struct zuobiao a[100]; //结构体定义存储测试点
scanf("%d",&n); //输入n个测试案例
for(i=0;i<n;i++) //外循环,循环每个案例
{
scanf("%d",&n1); //输入每个案例的测试点,n1
rank=0; //每个案例,初始情况的难度等级都为0
for(j=0;j<n1;j++) //内循环,循环每个测试点
{
scanf("%lf%lf%lf",&a[j].t,&a[j].x,&a[j].y); //存储”坐标”
if(j>=1) //计算难度
{
d=sqrt((a[j].x-a[j-1].x)*(a[j].x-a[j-1].x)+(a[j].y-a[j-1].y)*(a[j].y-a[j-1].y));
rank1=d/(a[j].t-a[j-1].t);
rank=rank>rank1?rank:rank1; //一个测试案例的难度为测试点中最大的难度
}
}
printf("%.10f\n",rank); //输出每个案例的难度,精确到10位小数。
}
return 0;
}