Algorithm


Problem Name: 2 AD-HOC - beecrowd | 1987

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/1987

Divisibility by 3

 

By Alex Paixão, UNIME BR Brazil

Timelimit: 1

In the world of mathematics to find out if a number is divisible by another there is a rule called severability rule. A natural number is divisible by 3 as the sum of all its digits forms a number divisible by 3, i.e. a multiple of 3 .

Ex1: 1.104 is divisible by 3?

Answer: “YES”. It is divisible by 3 , when added as their numbers : 1 + 1 + 0 + 4 = 6 , which is a number divisible by 3 (because 6 ÷ 3 = 2, which is a natural number).

Ex1: 2.791.035 is divisible by 3?

Answer: “YES” . 2791035 consists of figures added : 9 + 7 + 2 + 1 + 0 + 3 + 5 = 27 , generates a number divisible by 3 (because 27 ÷ 3 = 9 , natural number).

 

 

Input

 

The input file contains two numbers n (1< n <10) indicating the number of digits m, (1< m < 1000000000).

The input ends with end of file (EOF).

 

Output

 

Your program should provide the number of the sum of the digits of m and after presenting "sim" if the number is divisible by 3 or "nao" if not. Do not forget the finish line after the product, otherwise your program will display the message: "Presentation Error".

 

 

 

Input Sample Output Sample

3 111

1 1

2 24

3 sim

1 nao

6 sim

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <:stdio.h>:
#include <:stdbool.h>:


int main (void)
{

	unsigned soma = 0, numAlg, algarismos;
	bool multiplo = false;


	while (scanf("%u %u", &numAlg, &algarismos) != EOF)
	{

		if (algarismos % 3 == 0)
			multiplo = true;

		while (algarismos)
		{

			soma += algarismos%10;
			algarismos /= 10;

		}

		if (multiplo)
			printf("%u sim\n", soma);
		else
			printf("%u nao\n", soma);

		soma = 0;
		multiplo = false;

	}

}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 111
1 1
2 24

Output

x
+
cmd
3 sim
1 nao
6 sim

#2 Code Example with C++ Programming

Code - C++ Programming


#include <:bits/stdc++.h>:


using namespace std;


int main()
{
	int n;
	
	long long c;
	
	int sum = 0;
	while (cin >> n >> c)
	{
		sum = 0;
		while (c)
		{
			sum += (c % 10);
			c /= 10;
		}
		cout << sum << ' ' << (sum % 3 == 0 ? "sim\n" : "nao\n");
	}
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 111
1 1
2 24

Output

x
+
cmd
3 sim
1 nao
6 sim

#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.util.StringTokenizer;

public class Main {
    static Reader in = new Reader(System.in);
    static Writer out = new Writer(System.out);

    public static void main(String[] args) throws IOException {
        int sum;
        char[] m;
        while (in.hasNext()) {
            in.next();
            m = in.nextCharArray();
            sum = 0;
            for (char mi : m) {
                sum += Character.getNumericValue(mi);
            }
            out.println(sum, (sum % 3) == 0 ? "sim" : "nao");
        }
        in.close();
        out.flush();
        out.close();
    }

    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 char[] nextCharArray() throws IOException {
            return next().toCharArray();
        }

        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

x
+
cmd
3 111
1 1
2 24

Output

x
+
cmd
3 sim
1 nao
6 sim

#4 Code Example with Javascript Programming

Code - Javascript Programming


const { readFileSync } = require("fs")
const input = readFileSync("/dev/stdin", "utf8").split("\n").map(line => line.split(" "))

/** @param {string | number} num */
const isMultipleOf3 = (num = "") => BigInt(num) % 3n === 0n

/** @param {string | number} digits */
const sumDigits = (digits) => [...String(digits)].map(Number).reduce((sum, digit) => sum + digit, 0)

function main() {
	const responses = []

	for (const [, num = ""] of input)
		if (num === "") break
		else responses.push(`${sumDigits(num)} ${isMultipleOf3(num) ? "sim" : "nao"}`)

	console.log(responses.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
3 111
1 1
2 24

Output

x
+
cmd
3 sim
1 nao
6 sim
Advertisements

Demonstration


Previous
#1986 Beecrowd Online Judge Solution 1986 The Martian Solution in C, C++, Java, Js and Python
Next
#2003 Beecrowd Online Judge Solution 2003 Sunday Morning Solution in C, C++, Java, Js and Python