土下座しながら探索中

主に競技プログラミング

UVa 11505 : Logo

問題リンク:http://uva.onlinejudge.org/external/115/11505.html

問題概要:

解法:
その通りにシュミレーションする
出力する際に(int)round(sqrt(x*x+y*y))のように
(int)でキャストしないとWAになる
もしくはepsをつける

コード:

#include<iostream>
#include<cmath>

#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

int main()
{
  string opr;
  int T,n,dir,value;
  double x,y;
  cin >> T;
  while(T--)
    {
      dir = 0;
      x = y = 0;
      cin >> n;

      rep(_,n)
	{
	  cin >> opr >> value;

	  double arg = dir * M_PI / 180.0;
	  if(opr[0] == 'f')
	    x += cos(arg) * value, y += sin(arg) * value;
	  else if(opr[0] == 'b')
	    x -= cos(arg) * value, y -= sin(arg) * value;
	  else if(opr[0] == 'r')
	    dir = (dir - value + 360) % 360;
	  else 
	    dir = (dir + value + 360) % 360;
	}
      cout << (int)round(sqrt(x*x+y*y)) << endl;
    }
  return 0;
}