原题链接: http://acm.timus.ru/problem.aspx?space=1&num=1413
对小键盘的 1- 9 , 0 作为控制源, 分别能够让 机器人不同方向前进 ( 长度为1米) 5不动. 0 是自动销毁.
对一输入的一串字符串 求最终位置.
1413. Mars Jumper
Time limit: 0.5 second
Memory limit: 64 MB
Memory limit: 64 MB
Mars Jumper is a new improved model of Moon Rover. Clever heads from the engineering department proved that in the conditions of low gravity it's easier to jump, so the new vehicle moves by jumping. By the way, the length of its jump is always one meter. Mars Jumper is controlled with a standard computer keyboard. To assign the direction of jumping, one uses the complementary digit keys (those which are on the right). It is very convenient and handy: 8 means a jump northward, 2 means a jump southward, 6 means a jump eastward, 7 means a jump to the north-west, and so on. 5 is an order to take a sample of soil. In order to use the zero key, the chief engineer invented one more function: if this key is pressed, the motor of the vehicle is self-destroyed. He thinks that this function could be very useful.
Of course, to operate the Jumper is not an easy task. Not anyone can quickly move the vehicle, for example, half a meter to the north. According to the designers, Mars Jumper can jump to a position arbitrarily close to any specied point by means of a finite number of jumps.
The customers agreed to pay for the design and manufacture of Mars Jumper only after a trial. To conduct a trial, the chief engineer seated his daughter at a computer and oered her to press some buttons. The result of the trial was that the Jumper had jumped away. Now the engineers need to find it urgently.
Your task is to tell the search team where to find the Jumper. You know that the vehicle stopped at the moment when the zero key was first pressed, or, if this key was not pressed, at the end of the route determined by the sequence of pressed keys. You are given this sequence. You must determine the final position of Mars Jumper. The testing area is considered to be an innite plane.
Input
Not more than 10
6 digits from 0 to 9.
Output
You should output the final coordinates of Mars Jumper (in meters, accurate to 10 decimal places) in the form X Y, where X is the displacement of the Jumper from the initial point to the east and Y is its displacement to the north.
Sample
input | output |
---|---|
1236987412369870234567890123456789 | 1.0000000000 0.0000000000 |
Problem Author: Stanislav Vasilyev
ruby 的代码没有写 看到了别人的 c++ 格式挺喜欢 转到这里.
c ++ code
/*
* ACM Timus Online
* Mars Jumper - Problem 1413
*/
#include <stdio.h>
double X, Y;
#define rad2 0.70710678118654752440084436210485
int main()
{
char ch;
X = Y = 0;
while (scanf("%c", &ch) == 1 && ch != '0')
{
switch (ch)
{
case '1': X -= rad2; Y -= rad2; break;
case '2': Y--; break;
case '3': X += rad2; Y -= rad2; break;
case '4': X--; break;
case '6': X++; break;
case '7': X -= rad2; Y += rad2; break;
case '8': Y++; break;
case '9': X += rad2; Y += rad2; break;
}
}
printf("%.10lf %.10lf\n", X, Y);
return 0;
}