import java.io.PrintStream; import java.util.Arrays; import java.util.Scanner; /** * Solution to Inflation * * @author vanb */ public class inflation_vanb { /** The Input. */ private static Scanner sc; /** The Output. */ private static PrintStream ps; /** * Is this value known? * * @param x Value to test * @return true, if x is known (not -1), otherwise false. */ private boolean known( double x ) { return x>-0.5; } /** * Transitive closure. * * @param a An array * @param i An index * @param j Another index * @param k A third index * @return true, if a[i][k] is previously unknown but can be computed from a[i][j] and a[j][k] */ private boolean transitive( double a[][], int i, int j, int k ) { boolean changed = false; if( !known(a[i][k]) && known(a[i][j]) && known(a[j][k]) ) { a[i][k] = a[i][j] * a[j][k]; a[k][i] = 1.0 / a[i][k]; changed = true; } return changed; } /** * Do It! */ private void doit() { int y = sc.nextInt(); int c = sc.nextInt(); int q = sc.nextInt(); // We get the inflation for year i to year i+1. // But, we can find the inflation rate between any two years. // So, inflation needs to be a 2D array. double inflation[][] = new double[y][y]; // Initialize inflation array for( int i=0; i-1.5 ) { if( known(prices[i][x]) && known(prices[j][x]) && known(prices[i][z]) && known(prices[j][z]) ) { if( Math.abs(prices[j][x]/prices[j][z] - prices[i][x]/prices[i][z])<0.00000001 ) { ratios[x][z] = prices[j][x]/prices[j][z]; ratios[z][x] = 1.0/ratios[x][z]; changed = true; } else { ratios[x][z] = ratios[z][x] = -2.0; changed = true; } } } } } // See of we can use ratios for( int k1=0; k1