online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
import java.io.*; import java.nio.file.*; import java.util.*; class Main { private static List<Integer> readFile(String path) throws IOException { Path filePath = Path.of(path); List<String> content = Files.readAllLines(filePath); List<Integer> result = new ArrayList(); for (String line : content) { result.add(Integer.parseInt(line)); } return result; } private static void writeResults(Iterable<String> results) throws IOException { Path filePath = Path.of("./wyniki3.txt"); Files.write(filePath, results); } public static void main(String[] args) throws IOException { Task[] tasks = new Task[] { new FirstTask(), new SecondTask(), new ThirdTask() }; List<Integer> sampleData = readFile("./liczby_przyklad.txt"); List<Integer> realData = readFile("./liczby.txt"); ArrayList<String> results = new ArrayList<String>(); for (Task task : tasks) { String name = task.getTaskName(); List<String> sampleSolution = task.doTask(sampleData); List<String> realSolution = task.doTask(realData); System.out.println(name); System.out.println("Przykład:"); System.out.println(String.join("\n", sampleSolution)); System.out.println("Prawdziwe dane:"); System.out.println(String.join("\n", realSolution)); results.add(name); results.addAll(realSolution); } writeResults(results); } }
import java.util.*; class FirstTask implements Task { public String getTaskName() { return "3.2."; } public List<String> doTask(List<Integer> entries) { int result = 0; boolean[] sieve = Task.getPrimeNumbers(1000000); for (int number : entries) { if (sieve[number - 1]) { result++; } } return Arrays.asList(Integer.toString(result)); } }
468 736 1000 996 556 578 152 400 22 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
114318 297906 319716 704696 245302 831126 452988 774182 729296 300514 229754 183808 304304 368860 209324 912952 936616 156162 34756 743606 226532 197436 627384 635350 265700 936844 452656 902354 538716 369062 133530 653030 691080 477356 381838 936378 308482 834822 734674 61890 159448 964424 245694 463748 333284 479130 400814 269898 635288 435566 37616 885930 632998 664996 521280 898694 625952 998046 825160 164666 391220 958688 841808 82298 460156 223644 18676 768634 82580 753346 830520 242024 741882 100328 705770 75164 579454 106582 369172 238854 542144 430898 124784 175140 95894 670174 97946 721842 668218 923104 910620 59436 905904 752426 141732 366058 976066 184518 134692 82758
import java.util.*; class SecondTask implements Task { private static final String resultFormat = "%d %d"; public String getTaskName() { return "3.3."; } public List<String> doTask(List<Integer> entries) { // na początek wykonujemy sito Eratostenesa boolean[] sieve = Task.getPrimeNumbers(1000000); // zmienne do przechowania wyników int maxResult = 0; int maxResultNumber = 0; int minResult = 1000000; int minResultNumber = 0; // iterujemy po wszystkich liczbach z zadania for (int entry : entries) { // interesują nas tylko parzyste // w przypadku nieparzystych przechodzimy do kolejnej if (entry % 2 != 0) { continue; } // zmienna, która przechowa ile liczba zawiera rozkładów int result = 0; // iterujemy po wszystkich liczba od 2 do połowy aktualnej liczby // od połowy wzwyż będą powtarzać się pary for (int i = 2; i <= entry / 2; i++) { // jeśli aktualna liczba nie jest pierwsza, przechodzimy dalej if (!sieve[i]) { continue; } // drugi składnik sumy będzie różnicą dwóch znanych nam liczb int second = entry - i; // jeśli drugi składnik jest liczbą pierwszą // to inkrementujemy liczbę rozkładów if (sieve[second]) { result++; } } // jeśli mamy jakiekolwiek rozkłady, // to sprawdźmy, czy możemy je przechować if (result > 0) { // sprawdzamy czy mamy najwięcej rozkładów if (result > maxResult) { maxResult = result; maxResultNumber = entry; } // sprawdzamy czy mamy najmniej rozkładów if (result < minResult) { minResult = result; minResultNumber = entry; } } } return Arrays.asList(String.format(resultFormat, maxResultNumber, maxResult), String.format(resultFormat, minResultNumber, minResult)); } }
import java.util.*; import java.lang.Math; interface Task { String getTaskName(); List<String> doTask(List<Integer> entries); public static boolean[] getPrimeNumbers(int N) { boolean[] sieve = new boolean[N + 1]; sieve[0] = false; sieve[1] = false; for (int i = 2; i <= N; i++) { sieve[i] = true; } for (int i = 2; i <= Math.sqrt(N); i++) { if (sieve[i]) { int j = i * i; while (j <= N) { sieve[j] = false; j += i; } } } return sieve; } }
import java.util.*; class ThirdTask implements Task { public String getTaskName() { return "3.4."; } public List<String> doTask(List<Integer> entries) { List<Character> digits = Arrays.asList('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'); HashMap<Character, Integer> counter = new HashMap(); // uzupełnienie mapy wartościami for (Character digit : digits) { counter.put(digit, 0); } // algorytm wykonujący zadanie for (int number : entries) { String hex = Integer.toHexString(number).toUpperCase(); for (int i = 0; i < hex.length(); i++) { Character current = hex.charAt(i); counter.put(current, counter.get(current) + 1); } } List<String> result = new ArrayList(); // spisanie rezultatów for (Character digit : digits) { result.add(String.format("%s:%d", digit, counter.get(digit))); } return result; } }
3.2. 21 3.3. 910620 9932 18676 195 3.4. 0:32 1:26 2:37 3:31 4:43 5:25 6:28 7:23 8:38 9:28 A:45 B:33 C:29 D:23 E:44 F:10

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue