1.
/**
* ACM Training 2009
* ACM Problem 10378 Complex Numbers
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=15&problem=1319&mosmsg=Submission+received+with+ID+7614555
*
* @author Felix Dietrich
* @version 1.0, 12/10/2009
*
* Methode: Complex Numbers
* Status : Accepted
* Runtime: 1.652
*/

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;

class Main
{
public static void main(String args[]) throws IOException
{
int casenumber = 1;

Scanner sc = new Scanner(System.in);
sc.useLocale(Locale.ENGLISH);

while (sc.hasNext())
{
Complex c = new Complex(sc.next());

System.out.println("Case " + casenumber + ":");
casenumber++;

String result = "";
for (Complex root : c.roots(Integer.parseInt(sc.next())))
result += root + "\n";

System.out.println(result);
}
}

private static class Complex
{
final double real;
final double complex;

public Complex(String string)
{
if (string.startsWith("+"))
string = string.substring(1);
if (string.substring(1).contains("+"))
{
real = Integer.parseInt(string.substring(0, string.lastIndexOf("+")));
complex = Integer.parseInt(string.substring(string.lastIndexOf("+") + 1, string.length() - 1));
} else
{
real = Integer.parseInt(string.substring(0, string.lastIndexOf("-")));
complex = Integer.parseInt(string.substring(string.lastIndexOf("-"), string.length() - 1));
}
}

public Complex(double r, double phi)
{
this.real = Math.cos(phi) * r;
this.complex = Math.sin(phi) * r;
}

public double r()
{
return Math.hypot(real, complex);
}

public double phi()
{
return Math.atan2(complex, real);
}

double fix(double v)
{
if (v > 0)
return v;
if (v > -0.0005)
return 0.0;
return v;
}

public Complex[] roots(int n)
{
Complex[] result = new Complex[n];
for (int k = 0; k < n; k++)
result[k] = new Complex(Math.pow(this.r(), 1. / n), (this.phi() + 2. * k * Math.PI) / (double) n);

List<Complex> orderedResult = new ArrayList<Complex>();

for (Complex c : result)
orderedResult.add(c);

Collections.sort(orderedResult, new Comparator<Complex>()
{

@Override
public int compare(Complex arg0, Complex arg1)
{
if (arg0.real - arg1.real > 1e-6)
return -1;
if (Math.abs(arg0.real - arg1.real) < 1e-6 && arg0.complex - arg1.complex > 1e-6)
return -1;
if (Math.abs(arg0.real - arg1.real) < 1e-6 && Math.abs(arg0.complex - arg1.complex) < 1e-6)
return 0;
return 1;
}

});

return orderedResult.toArray(result);
}

@Override
public String toString()
{
double real = fix(this.real);
double complex = fix(this.complex);
return String.format("%.3f%s%.3fi", real, (complex >= 0 ? "+" : ""), complex);
}
}
}