10432 – Polygon Inside A Circle



A polygon surface area calculation problem.
The formula for the regular n-gon’s area is:
area = n \cdot {1 \over 2} \cdot r^2 \cdot sin(a)
And since the interior angle of a polygon is {n - 2\over n} \cdot \pi , a = \pi - {n - 2\over n} \cdot \pi
area = n \cdot {1 \over 2} \cdot r^2 \cdot sin(\pi - {n - 2\over n} \cdot \pi)
area = n \cdot {1 \over 2} \cdot r^2 \cdot sin({n - 2\over n} \cdot \pi)
C++ implementation:

#include <iostream>
#include <cmath>
using namespace std;

typedef long double gtype;
const gtype pi = M_PI;
#define polA(n) (((n) - 2) * pi / (n))

int main() {
	gtype r, n;
	cout.precision(3);
	while(cin >> r >> n)
		cout << fixed << (r * r * sin(polA(n)) * n * 0.5) << endl;
	return 0;
}

10814 – Simplifying Fractions


A simple math GCD problem with big arithmetic.

  • Get fraction a / b.
  • Find g : GCD(a, b).
  • Divide both a, b by g.
  • Output.

Rank 461 Java code:

import java.math.BigInteger;
import java.util.Scanner;

public class Main {

	private static BigInteger gcd(BigInteger a, BigInteger b) {
		BigInteger t;
		while (a.compareTo(BigInteger.ZERO) != 0) {
			t = a;
			a = b.mod(a);
			b = t;
		}
		return b;
	}

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		BigInteger a, b, g;
		while (n-- > 0) {
			a = s.nextBigInteger();
			s.next();
			b = s.nextBigInteger();
			g = gcd(a, b);
			System.out.println(a.divide(g) + " / " + b.divide(g));
		}
	}
}