Pagini recente » Cod sursa (job #2836102) | Cod sursa (job #378755) | Cod sursa (job #1577300) | Cod sursa (job #1178479) | Cod sursa (job #2588481)
package com.beniamin.savu.datorii;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
/**
* Hello world!
*
*/
public class App {
/**
* Code to detect when the father iterogates Gigel.
*/
private static final int FATHER_INTEROGATION_OPERATION = 1;
public static void main(String[] args) {
FileReader fileReader = new FileReader();
fileReader.open("datorii.in");
// Read "n" and "m"
String[] lineTokens = fileReader.readLine().split(" ");
int n = Integer.parseInt(lineTokens[0]);
int m = Integer.parseInt(lineTokens[1]);
System.out.println("n=" + n);
System.out.println("m=" + m);
// Read A1, A2, .... An
int[] customersDebts = new int[n];
lineTokens = fileReader.readLine().split(" ");
for (int i = 0; i<n; i++) {
customersDebts[i] = Integer.parseInt(lineTokens[i]);
}
StringBuilder result = new StringBuilder();
for (int i = 0; i<m; i++) {
lineTokens = fileReader.readLine().split(" ");
int query = Integer.parseInt(lineTokens[0]);
if(query == FATHER_INTEROGATION_OPERATION) {
int dayStart = Integer.parseInt(lineTokens[1]) - 1;
int dayEnd = Integer.parseInt(lineTokens[2]);
int totalDebt = 0;
for(int j = dayStart; j<dayEnd; j++) {
totalDebt += customersDebts[j];
}
result.append(totalDebt).append("\n");
System.out.println(totalDebt);
} else {
int day = Integer.parseInt(lineTokens[1]) - 1;
int amountPaid = Integer.parseInt(lineTokens[2]);
customersDebts[day] -= amountPaid;
}
}
FileWriter.write(result.toString(), "datorii.out");
}
}
/**
* Reads the content of a file.
*
* @author benis
*
*/
class FileReader {
/**
* The pointer for the current line.
*/
private int currentLine = 0;
/**
* The contents of the file.
*/
private String[] lines;
/**
* Reads the contents of the file.
*/
public void open(String filePath) {
StringBuilder contentBuilder = new StringBuilder();
try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
stream.forEach(s -> contentBuilder.append(s).append("\n"));
} catch (IOException e) {
e.printStackTrace();
}
lines = contentBuilder.toString().split("\n");
}
/**
* Resets the pointer for the current line.
*/
public void reset() {
this.currentLine = 0;
}
/**
* Reads the line at the current pointer.
*
* @return The line.
*/
public String readLine() {
String line = lines[currentLine];
currentLine++;
return line;
}
/**
* Check if there are more lines to read.
*
* @return <code>true</code> if there are more lines to read, <code>false</code> otherwise.
*/
public boolean hasLinesToRead() {
return lines.length > currentLine;
}
}
/**
* Writes content to a file.
*
* @author benis
*
*/
class FileWriter {
/**
* Writes the given content to the given file.
*
* @param content The content to write.
* @param filePath The file path
*/
public static void write(String content, String filePath) {
try {
Files.write(Paths.get(filePath), content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}