cześć, ostro przygotowuje się do Matury z informatyki ale blokuje mnie takie dosyć proste z pozoru zadanie. Otóż wszystko było by łatwe gdyby nie to że długość ciągu cyfr w ostatnim pliku jest strasznie wielka i nawet long jest za mały... Albo robię coś źle.. Podrzucam zadanie + mój kod żeby nie było że nic nie robię.
Miałem wcześniej inaczej napisaną wersję ale już za dużo zmieniałem
import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.stream.Stream;
import static java.lang.Integer.parseInt;
/**
* Created by Gladus on 13.03.2018.
*/
public class Main {
static ArrayList<String> lista = new ArrayList<String>();
static ArrayList<Long> convertedlist = new ArrayList<Long>();
static int l1suma2 = 0, l1suma3 = 0, l1suma5 = 0, l2suma2 = 0, l2suma3 = 0, l2suma5 = 0;
static Scanner inp = new Scanner(System.in);
static long result = 0;
static String s;
public static void main(String[] args) {
inp.toString();
czytaj("liczby3");
}
static public void czytaj(String file) {
file = file + ".txt";
try {
int k = 0;
Scanner sc = new Scanner(new File(file));
while (k < 2) {
s = sc.nextLine();
result = 0;
for (Integer i=0 ; i < s.length(); i++) {
result = (long) (result + (s.charAt(i) - '0') * Math.pow(2, s.length() - i - 1));
}
System.out.println(result);
convertedlist.add(result);
if ((convertedlist.get(convertedlist.size() - 1)) % 2 == 0) {
l1suma2++;
}
if ((convertedlist.get(convertedlist.size() - 1)) % 3 == 0) {
l1suma3++;
}
if ((convertedlist.get(convertedlist.size() - 1)) % 5 == 0) {
l1suma5++;
}
}
sc.close();
System.out.println(" w pliku " + file
+ " jest " + l1suma2 + "liczb podzielnych przez dwa" + l1suma3 + " liczb podzielnych przez 3 oraz " + l1suma5 + "Liczb podzielnych przez 5");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}