Pagini recente » Borderou de evaluare (job #1068863) | Cod sursa (job #2552412) | Cod sursa (job #2530231) | Borderou de evaluare (job #2579077) | Cod sursa (job #1710110)
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.LinkedList;
class Pair {
int x,y;
public Pair(int x,int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
}
public class Consecutive {
int t;
ArrayList<Integer> array;
public void citire() throws IOException {
BufferedReader br = new BufferedReader(new FileReader("consecutive.in"));
String line = null;
line = br.readLine();
array = new ArrayList<Integer>();
t = Integer.parseInt(line);
while ((line = br.readLine()) != null) {
array.add(Integer.parseInt(line));
}
br.close();
}
public void solve() throws IOException {
int solvings;
ArrayList<Pair> pairs = new ArrayList<Pair>();
PrintWriter writer = new PrintWriter(new FileWriter("consecutive.out"));
for (Integer nr : array) {
solvings = 0;
LinkedList<Integer> coada = new LinkedList<Integer>();
int cons = 1;
coada.add(1);
int suma = 1;
while (coada.size() != 1 || coada.peek() == 1) {
if (suma == nr) {
pairs.add(new Pair(coada.getFirst(),coada.getLast()));
solvings++;
cons++;
coada.add(cons);
suma +=cons;
} else {
if (suma > nr) {
suma -= coada.remove();
} else {
cons++;
coada.add(cons);
suma += cons;
}
}
}
writer.println(solvings);
}
for(Pair pair : pairs) {
writer.println(pair);
}
writer.close();
}
public static void main(String args[]) throws IOException {
Consecutive obj = new Consecutive();
obj.citire();
obj.solve();
}
}