Cod sursa(job #2545400)

Utilizator nxssGrasu Pancreasu nxss Data 13 februarie 2020 02:03:16
Problema Cifra Scor 30
Compilator java Status done
Runda Arhiva de probleme Marime 1.65 kb

import java.io.*;
import java.util.Scanner;

public class Main {
	static int[] values;

	static void readAndWrite() {
		try {

		FileInputStream inputStream = null;
		Scanner sc = null;
		// try {
		    inputStream = new FileInputStream("cifra.in");
		    sc = new Scanner(inputStream, "UTF-8");
		    int t = Integer.parseInt(sc.nextLine());
		//     while (sc.hasNextLine()) {
		//         String line = sc.nextLine();
		//         // System.out.println(line);
		//     }
		//     // note that Scanner suppresses exceptions
		//     if (sc.ioException() != null) {
		//         throw sc.ioException();
		//     }
		// } finally {
		//     if (inputStream != null) {
		//         inputStream.close();
		//     }
		//     if (sc != null) {
		//         sc.close();
		//     }
// }




		// BufferedReader br = new BufferedReader(new FileReader());
		BufferedWriter bw = new BufferedWriter(new FileWriter("cifra.out"));
		// int t = Integer.parseInt(br.readLine());
		for(int i = 1; i<=t; i++) {
			String line = sc.nextLine();
			int where = (line.length()>2)? line.length()-2 : 0; 
			int n = Integer.parseInt(line.substring(where));
			bw.write(values[n]+"\n");
		}
		inputStream.close();
		sc.close();
		bw.close();
	}
	catch (Exception e) {
		e.printStackTrace();
	}

	}

	static int power(int val) {
		int x = (val >= 10) ? val % 10 : val;
		int pow = x;
		int n= val;
		while(--n > 0) {
			pow = pow * x;
			pow = (pow > 10) ? pow % 10 : pow;
		}
		return pow;
	}

	static void precompute() {
		values = new int[100];
		for(int i = 1; i < 100; i++) {
			values[i] = (power(i)+values[i-1])%10;
		}
	}

	public static void main(String[] args) {
		precompute();
		readAndWrite();
	}
}