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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <complex>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <functional>
#include <iostream>
#include <map>
#include <set>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
int n,m;
vector<P> vec;
vector<int> rad;
P s,t;
ll sq(ll x)
{
	return x*x;
}
bool in(int x,P a)
{
	return sq(a.fi-vec[x].fi)+sq(a.sec-vec[x].sec)<rad[x]*rad[x];
}
bool check()
{
	for(int i=0;i<n;i++)
	{
		bool in1,in2;
		in1=in(i,s);in2=in(i,t);
		if(in1!=in2)return false;
		if(in1&&in2)continue;
		ll a=s.fi,b=s.sec,c=t.fi,d=t.sec,p=vec[i].fi,q=vec[i].sec;
		if(a>c)
		{
			swap(a,c);swap(b,d);
		}
		if(a==c)
		{
			if(min(b,d)<=vec[i].sec&&vec[i].sec<=max(b,d)&&abs(vec[i].fi-a)<=rad[i])
			{
				return false;
			}
			else continue;
		}
		ll A=sq(d-b)+sq(c-a);
		ll B=(b*c-a*d)*(d-b)-p*sq(c-a)-(c-a)*(d-b)*q;
		ll C=p*p*sq(c-a)+sq(b*c-a*d-q*c+q*a)-sq(rad[i])*sq(c-a);
		if(B*B-A*C<0)continue;
		else if(B*B-A*C==0)
		{
			if(a<=(-B/A)&&(-B/A)<=c)return false;
			else continue;
		}
		else
		{
			double ans1=(-B+sqrt(B*B-A*C))/A;
			double ans2=(-B-sqrt(B*B-A*C))/A;
			if(ans1>=a&&ans2<=c)return false;
			else continue;
		}
	}
	return true;
}
int main()
{
	while(1)
	{
		vec.clear();
		rad.clear();
		scanf("%d",&n);
		if(n==0)break;
		for(int i=0;i<n;i++)
		{
			P k;
			int r;
			scanf("%d %d %d",&k.fi,&k.sec,&r);
			vec.pb(k);
			rad.pb(r);
		}
		scanf("%d",&m);
		for(int i=0;i<m;i++)
		{
			scanf("%d %d %d %d",&t.fi,&t.sec,&s.fi,&s.sec);
			if(check())puts("Danger");
			else puts("Safe");
		}
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

class Position {
public:
	double x, y, r;
};

const double EXP = 1e-8;
Position CircleData[100];
Position Person, Ghost;
int N, M;

bool isInCircle(const Position &data, const Position &circle) {
	double x, y;

	x = data.x - circle.x;
	y = data.y - circle.y;
	return (x*x + y*y < circle.r*circle.r);
}

bool isSafe() {
	bool inCircle1, inCircle2;
	double lenOfPGToCircle, p, q, r, k, b, deta, x1, x2, y1, y2, min, max;

	for (int j = 0; j < N; j++) {
		p = CircleData[j].x;
		q = CircleData[j].y;
		r = CircleData[j].r;

		inCircle1 = isInCircle(Person, CircleData[j]);
		inCircle2 = isInCircle(Ghost, CircleData[j]);
		if (inCircle1 != inCircle2) {
			return true;
		}
		if (inCircle1 && inCircle2) {
			continue;
		}

		if (Person.x == Ghost.x) {
			lenOfPGToCircle = fabs(Person.x - CircleData[j].x);
			//cout << "lenOfPGToCircle:" << lenOfPGToCircle << endl;
			if (lenOfPGToCircle > CircleData[j].r) {
				continue;
			}

			min = Person.y;
			max = Ghost.y;
			if (min > max) {
				min = Ghost.y;
				max = Person.y;
			}

			if (lenOfPGToCircle == CircleData[j].r) {
				y1 = q;
				if (min <= y1 && y1 <= max) {
					return true;
				}
			}
			else if (lenOfPGToCircle < CircleData[j].r) {
				y1 = q - sqrt(r*r - (Person.x - p)*(Person.x - p));
				y2 = q + sqrt(r*r - (Person.x - p)*(Person.x - p));
				if (min <= y1 && y2 <= max) {
					return true;
				}
			}
			continue;
		}


		k = (Person.y - Ghost.y) / (Person.x - Ghost.x);
		b = Person.y - k*Person.x;
		//cout << "k:" << k << "  b:" << b << endl;

		deta = 4 * pow((k*b - p - k*q), 2) - 4 * (1 + k*k)*(p*p + pow((b - q), 2) - r*r);
		//cout << "deta:" << deta << endl;
		if (deta < 0) {
			continue;
		}

		min = Ghost.x;
		max = Person.x;
		if (min > max) {
			min = Person.x;
			max = Ghost.x;
		}
		if (deta > EXP) {
			x1 = (2 * (p + k*q - k*b) - sqrt(deta)) / (2 * (1 + k*k));
			x2 = (2 * (p + k*q - k*b) + sqrt(deta)) / (2 * (1 + k*k));
			//cout << "x1:" << x1 << "  x2:" << x2 << endl;
			if ((min <= x1 && x1 <= max) || (min <= x2 && x2 <= max) || (min <= x1 && x2 <= max)) {
				return true;
			}
			continue;
		}

		if (deta <= EXP) {
			x1 = (p + k*q - k*b) / (1 + k*k);
			//cout << "x1:" << x1 << endl;
			if (min <= x1 && x1 <= max) {
				return true;
			}
		}
	}

	return false;
}


void solve() {


	if (isSafe()) {
		cout << "Safe" << endl;
	}
	else {
		cout << "Danger" << endl;
	}

}


int main() {

	while (cin >> N, N) {
		for (int i = 0; i < N; i++) {
			cin >> CircleData[i].x >> CircleData[i].y >> CircleData[i].r;
		}
		cin >> M;
		for (int i = 0; i < M; i++) {
			cin >> Person.x >> Person.y >> Ghost.x >> Ghost.y;
			solve();
		}


		//show();
	}

	return 0;
}