Algorithm
Problem Name: 2 AD-HOC - beecrowd | 2217
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2217
Nove
By Pedro Michael, IFCE Brazil
Timelimit: 1
Paulo Bruno is a boy that like Pokemons, whoever he hates math, he hates exponentiation and for some reason he doesn't calculate correctly operations with the number 9. Knowing about that, his friend Werlesson decided to do a challenge for Paulo. Werlesson wants Paulo calculates the N-th power of 9 and answer the last digit of that. For example: given N=2, the answer should be 1, because 92=81. The difficulty is that 9N for a big N is a very very huge number. Without ideas, Paulo decided ask for your help.
Input
The input consists of many instances of the problem. The first line contains just an integer T that represents the number of instances.
Each instance is composed by one line that has an integer number N(0 ≤ N ≤ 109).
Output
For each instance in the input, your must print a line with the last digit of 9N.
Input Samples | Output Samples |
2 |
9 |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main (void)
{
unsigned long numero;
unsigned short casos;
scanf("%hu", &casos);
while (casos--)
{
scanf("%lu", &numero);
if (numero % 2 == 0)
printf("1\n");
else
printf("9\n");
}
}
Copy The Code &
Try With Live Editor
Input
1
2
Output
1
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <cstdio>
int main() {
int t;
scanf("%d", &t);
while (t--) {
int n;
scanf("%d", &n);
printf("%d\n", (n % 2 == 0) ? 1 : 9);
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
1
2
Output
1
#3 Code Example with Java Programming
Code -
Java Programming
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.StringTokenizer;
public class Main {
static Reader in = new Reader(System.in);
static Writer out = new Writer(System.out);
static final BigInteger NOVE = new BigInteger("9");
public static void main(String[] args) throws IOException {
int T = in.nextInt(), N;
while (T-- > 0) {
N = in.nextInt();
out.println((N % 2 == 0) ? 1 : 9);
}
in.close();
out.flush();
out.close();
}
////////////////////////////////////////////////////////////////////////////
///////////////////////////// INPUT / OUTPUT /////////////////////////////
////////////////////////////////////////////////////////////////////////////
static class Reader implements Closeable {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public Reader(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
private StringTokenizer getTokenizer() throws IOException {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = nextLine();
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer;
}
public boolean hasNext() throws IOException {
return getTokenizer() != null;
}
public String next() throws IOException {
return hasNext() ? tokenizer.nextToken() : null;
}
public String nextLine() throws IOException {
tokenizer = null;
return reader.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public float nextFloat() throws IOException {
return Float.parseFloat(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public String[] nextStringArray(int size) throws IOException {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = next();
}
return array;
}
public int[] nextIntArray(int size) throws IOException {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = nextInt();
}
return array;
}
public long[] nextLongArray(int size) throws IOException {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = nextLong();
}
return array;
}
public double[] nextDoubleArray(int size) throws IOException {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = nextDouble();
}
return array;
}
@Override
public void close() throws IOException {
tokenizer = null;
reader.close();
}
}
static class Writer {
private final PrintWriter writer;
public Writer(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
}
Copy The Code &
Try With Live Editor
Input
1
2
Output
1
#4 Code Example with Javascript Programming
Code -
Javascript Programming
const { readFileSync } = require("fs")
const [numLines, ...lines] = readFileSync("/dev/stdin", "utf8").split("\n")
function main() {
const responses = lines
.slice(0, +numLines)
.map(Number)
.map(line => line % 2 == 0 ? 1 : 9)
console.log(responses.join("\n"))
}
main()
Copy The Code &
Try With Live Editor
Input
1
2
Output
1