Cod sursa(job #1699571)

Utilizator VladBogdanVlad Iulian Bogdan VladBogdan Data 7 mai 2016 20:29:08
Problema Stramosi Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 2.08 kb
import java.awt.geom.Area;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;

class Pair {
	int x;
	int y;
	
	Pair(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "(" + x + " " + y + ")";
	}
}

public class Stramosi {
	public static void main(String[] args) {
		ArrayList<Integer> array = new ArrayList<Integer>();
		ArrayList<Pair> pairs = new ArrayList<Pair>();
		File file = new File("stramosi.in");
		BufferedReader reader = null;

		try {
		    reader = new BufferedReader(new FileReader(file));
		    String text = null;

		    String first = reader.readLine();
		    String[] strings = first.split(" ");
		    
		    int n = Integer.parseInt(strings[0]);
		    int m = Integer.parseInt(strings[1]);
		    
		    first = reader.readLine();
		    strings = first.split(" ");
		    
		    for (String s : strings) {
		    	array.add(Integer.parseInt(s));
		    }
		    
		    while ((text = reader.readLine()) != null) {
		        strings = text.split(" ");
		        
		        pairs.add(new Pair(Integer.parseInt(strings[0]), Integer.parseInt(strings[1])));
		    }
		    
		    System.out.println(n + " " + m);
		    
		    System.out.println(array);
		    System.out.println(pairs);
		    Writer wr = new FileWriter("stramosi.out");
		    for (Pair p : pairs) {
		    	
		    	int index = p.x;
		    	int nr = p.y;
		    	
		    	while (nr != 0) {
		    		index = array.get(index - 1);
		    		
		    		if (index == 0) break;
		    		nr--;
		    	}
		    	
		    	wr.write(Integer.toString(index));
		    	wr.write("\n");
		    	System.out.println(index);
		    }
		    wr.close();
		} catch (FileNotFoundException e) {
		    e.printStackTrace();
		} catch (IOException e) {
		    e.printStackTrace();
		} finally {
		    try {
		        if (reader != null) {
		            reader.close();
		        }
		    } catch (IOException e) {
		    }
		    
		   
		}

	}
}