Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	x := make([]int, 100)
	y := make([]int, 100)
	r := make([]int, 100)
	for scanner.Scan() {
		a, _ := strconv.Atoi(scanner.Text())
		if a == 0 {
			break
		}
		for i := 0; i < a; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			tmpx, _ := strconv.Atoi(input[0])
			tmpy, _ := strconv.Atoi(input[1])
			tmpr, _ := strconv.Atoi(input[2])
			x[i], y[i], r[i] = tmpx, tmpy, tmpr
		}
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			scanner.Scan()
			input := strings.Split(scanner.Text(), " ")
			c, _ := strconv.Atoi(input[0])
			d, _ := strconv.Atoi(input[1])
			e, _ := strconv.Atoi(input[2])
			f, _ := strconv.Atoi(input[3])
			ok := false
			for j := 0; j < a; j++ {
				if (c-x[j])*(c-x[j])+(d-y[j])*(d-y[j]) >= r[j]*r[j] || (e-x[j])*(e-x[j])+(f-y[j])*(f-y[j]) >= r[j]*r[j] {
					if (e-c)*(x[j]-c)+(f-d)*(y[j]-d) < 0 {
						if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d) <= r[j]*r[j] {
							ok = true
						}
					} else {
						if (e-c)*(x[j]-c)+(f-d)*(y[j]-d)>(e-c)*(e-c)+(f-d)*(f-d) {
							if (x[j]-e)*(x[j]-e)+(y[j]-f)*(y[j]-f)<=r[j]*r[j] {
								ok = true
							}
						} else {
							if (x[j]-c)*(x[j]-c)+(y[j]-d)*(y[j]-d)-((e-c)*(x[j]-c)+(f-d)*(y[j]-d))*((e-c)*(x[j]-c)+(f-d)*(y[j]-d))/((e-c)*(e-c)+(f-d)*(f-d))<=r[j]*r[j] {
								ok = true
							}
						}
					}

				}
			}
			if ok {
				fmt.Println("Safe")
			} else {
				fmt.Println("Danger")
			}
		}
	}
}

C++: #include <iostream>
#include <cmath>
#include <vector>

class Rational {
	using N = int;
	friend Rational operator*(const int &, const Rational &);
public:
	Rational(const N &p = 1, const N &q = 1) :num{ p }, den{ q } { tuubun(); };
	Rational(const Rational &p, const Rational &q) :Rational(p.num * q.den, p.den * q.num) {};
	bool operator<(const Rational &other) const {
		return num * other.den < den * other.num;
	}
	bool operator>(const Rational &other) const {
		return num * other.den > den * other.num;
	}
	bool operator==(const Rational &other) const {
		return num * other.den == den * other.num;
	}
	bool operator<=(const Rational &other) const { return (*this) < other || (*this) == other; }
	bool operator>=(const Rational &other) const { return (*this) > other || (*this) == other; }
	Rational operator*(const Rational &other) const {
		return Rational(num * other.num, den * other.den);
	}
	Rational operator+(const Rational &other) const {
		return Rational(num * other.den + other.num * den, den * other.den);
	}
	Rational operator-(const Rational &other) const {
		return Rational(num * other.den - other.num * den, den * other.den);
	}
	Rational operator/(const Rational &other) const {
		return Rational(num * other.den, den * other.num);
	}
	Rational operator-() const {
		return Rational(-num, den);
	}

private:
	N gcd(const N &a, const N &b) const {
		if (b == 0) {
			return a;
		}
		else {
			return gcd(b, a % b);
		}
	}
	void tuubun() {
		auto g = gcd(num, den);
		num /= g; den /= g;
		if (den < 0) {
			abs();
			num *= -1;
		}
	}
	void abs() {
		num = std::abs(num); den = std::abs(den);
	}
	N num, den;
};
Rational operator*(const int &n, const Rational &r) {
	return Rational(n * r.den + r.num, r.den);
};
struct Point {
	Point(const Rational &ax = 0, const Rational &ay = 0) :x{ ax }, y{ ay } {};
	Rational dist_square(const Point &other) const {
		auto dx{ x - other.x }, dy{ y - other.y };
		return dx * dx + dy * dy;
	}
	Rational x, y;
};
struct Line {
	Line(const Rational &aa = 1, const Rational &ab = 1, const Rational &ac = 1) :a{ aa }, b{ ab }, c{ ac } {};
	Line(const Point &p1, const Point &p2) :a( p2.y - p1.y ), b( p1.x - p2.x ), c( -p1.x * a - p1.y * b ) {};
	Line get_vertical() const {
		return Line(b, -a, 0);
	}
	Line shift_to_point(const Point &p) const {
		return Line(a, b, -(a * p.x + b * p.y));
	}
	bool is_paraller(const Line &other) const {
		return a * other.b == b * other.a;
	}
	Point cross_point(const Line &other) const {
		return Point((b * other.c - c * other.b) / (a * other.b - b * other.a), (c * other.a - a * other.c) / (a * other.b - b * other.a));
	}
	Rational distance_square(const Point &other) const {
		auto num = a * other.x + b * other.y + c;
		return Rational(num * num, a * a + b * b);
	}
	Rational a, b, c;
};
struct Circle {
	Circle(const int &x = 0, const int &y = 0, const int &ar = 0) :center{ x, y }, r{ ar } {};
	bool is_in(const Point &other) const {
		return center.dist_square(other) <= r * r;
	}
	Point center;
	int r;
};
int main() {
	for (int n; std::cin >> n && n != 0;) {
		std::vector<Circle> walls(n);
		for (auto &w : walls) {
			int wx, wy, r;
			std::cin >> wx >> wy >> r;
			w = Circle(wx, wy, r);
		}
		int m;
		std::cin >> m;
		for (; m > 0; --m) {
			int x, y;
			std::cin >> x >> y;
			Point tarou(x, y);
			std::cin >> x >> y;
			Point oni(x, y);
			Line view(tarou, oni);
			Line measure = view.get_vertical();
			bool is_danger = true;
			for (const auto &wall : walls) {
				auto nearest = measure.shift_to_point(wall.center).cross_point(view);
				if (wall.is_in(nearest) && !(wall.is_in(tarou) && wall.is_in(oni))) {
					if ((tarou.x - nearest.x) * (oni.x - nearest.x) < 0 || (tarou.y - nearest.y) * (oni.y - nearest.y) < 0) {
						is_danger = false;
						break;
					}
					else if (wall.is_in(tarou) || wall.is_in(oni)){
						is_danger = false;
						break;
					}
				}
			}
			if (is_danger) {
				std::cout << "Danger\n";
			}
			else {
				std::cout << "Safe\n";
			}
		}
	}
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>

using namespace std;

double EPS = 1e-10;
double PI = 3.141592653589793238462643383279;

double add(double a, double b){
  if(abs(a+b) < EPS * (abs(a)+abs(b)))return 0;
  return a+b;
}

struct point{
  double x, y;
  point(){}
  point(double x,double y) : x(x) , y(y){}

  point operator + (point p){
    return point(add(x,p.x), add(y,p.y));
  }

  point operator - (point p){
    return point(add(x,-p.x), add(y,-p.y));
  }
};

struct circle{
  point p;
  double r;
};

double dot(point a, point b){return (a.x * b.x + a.y * b.y);}
double cross(point a, point b){return (a.x * b.y - a.y * b.x);}
double norm(point a){return sqrt(a.x*a.x+a.y*a.y);}

double dist(point a,point b){
  return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}

double distance_ls_p(point a, point b, point c) {
    if ( dot(b-a, c-a) < EPS ) return norm(c-a);
    if ( dot(a-b, c-b) < EPS ) return norm(c-b);
  return abs(cross(b-a, c-a)) / norm(b-a);
}

int crossCS(point a, point b, circle c){
  double res=distance_ls_p(a,b,c.p);
  double ac=dist(a,c.p);
  double bc=dist(b,c.p);

  if(res>c.r || (ac<c.r && bc<c.r))return 0;
  else if(c.r-res<EPS ||(ac<c.r && bc>=c.r)||(ac>=c.r && bc<c.r))return 1;
  else return 2;
}

int main(void){
  int n,m;
  circle c[100];
  point t,s;

  while(cin >> n,n){
    for(int i=0;i<n;i++)
      cin >> c[i].p.x >> c[i].p.y >> c[i].r;
    
    cin >> m;
    for(int i=0;i<m;i++){
      cin >> t.x >> t.y >> s.x >> s.y;

      int fg=0;
      for(int j=0;j<n;j++)fg+=crossCS(t,s,c[j]);
      
      if(fg==0)cout << "Danger" << endl;
      else cout << "Safe" << endl;
    }
  }
  return 0;
}