2014
03-06

# Logo

Logo is a programming language built around a turtle. Commands in the language cause the turtle to move. The turtle has a pen attached to it. As the turtle moves, it draw lines on the page. The turtle can be programmed to draw interesting pictures.

We are interested in making the turtle draw a picture, then return to the point that it started from. For example, we could give the turtle the following program:

fd 100 lt 120 fd 100 lt 120 fd 100

The command fd causes the turtle to move forward by the specified number of units. The command lt causes the turtle to turn left by the specified number of degrees. Thus the above commands cause the turtle to draw an equilateral triangle with sides 100 units long. Notice that after executing the commands, the turtle ends up in the same place as it started. The turtle understands two additional commands. The command bk causes the turtle to move backward by the specified number of units. The command rt causes the turtle to turn right by the specified number of degrees.

After executing many commands, the turtle can get lost, far away from its starting position. Your task is to determine the straight-line distance from the turtle’s position at the end of its journey back to the position that it started from.

The first line of input contains one integer specifying the number of test cases to follow. Each test case starts with a line containing one integer, the number of commands to follow. The commands follow, one on each line. Each test case will contain no more than 1000 commands.

The first line of input contains one integer specifying the number of test cases to follow. Each test case starts with a line containing one integer, the number of commands to follow. The commands follow, one on each line. Each test case will contain no more than 1000 commands.

1
5
fd 100
lt 120
fd 100
lt 120
fd 100

0

//Time：15MS
//Memory：244K
#include <stdio.h>
#include <string.h>
#include <math.h>
const double PI=acos(-1.0);
double x,y,th;
void fd(int l)
{
x+=l*sin(2*PI*th/360);
y+=l*cos(2*PI*th/360);
}
void lt(int l)
{
th=th+l;
while(th>360)
th-=360;
while(th<0)
th+=360;
}
int main()
{
int cas,n,l;
char s[100];
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
x=0;y=0;th=0;
while(n--)
{
scanf("%s%d",s,&l);
switch(s[0])
{
case 'f':fd(l);break;
case 'b':fd(-l);break;
case 'l':lt(l);break;
case 'r':lt(-l);break;
}
}
double ans=x*x+y*y;
ans=pow(ans,0.5);
printf("%.0lf\n",ans);
}
return 0;
}

1. 您没有考虑 树的根节点是负数的情况， 若树的根节点是个很大的负数，那么就要考虑过不过另外一边子树了