Posts

(URI) Soma de Impares Consecutivos I - Solução

import java.io.*; import java.util.*; import java.lang.*; class Main  {     public static void main(String[] args) throws NumberFormatException, IOException {         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                int x = leitor(br);         int y = leitor(br);                int menor = x;         int maior = y;         if (x > y) {             menor = y;             maior = x;                   }          ...

(URI) Positivos e Média - Solução

import java.io.*; import java.util.*; import java.lang.*; class Main  {     public static void main(String[] args) throws NumberFormatException, IOException {         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 final int QTE = 6;                 double soma = 0;         int contador = 0;         for (int i = 0; i < QTE; i++) {             Double d = Double.parseDouble(br.readLine());             if (d > 0) {                 soma += d;        ...

(URI) Sentença Dançante - Solução

import java.io.*; import java.util.*; import java.lang.*; class Main  {     public static void main(String[] args) throws NumberFormatException, IOException {         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));                 String s = "";         while ((s = br.readLine()) != null) {             String nS = "";                         boolean maiuscula = true;             for (int i = 0; i < s.length(); i++) {                 if (maiuscula && !(s.charAt(i) == ' ')) {   ...

(Coderbyte) Dash Insert II - Solução

Have the function DashInsertII( str ) insert dashes ('-') between each two odd numbers and insert asterisks ('*') between each two even numbers in str . For example: if str is 4546793 the output should be 454*67-9-3 . Don't count zero as an odd or even number. import java.util.*; import java.io.*; class Function {    String DashInsertII(String num) {     int anterior = Integer.parseInt(String.valueOf(num.charAt(0)));     String resultado = String.valueOf(num.charAt(0));         for (int i = 1; i < num.length(); i++) {       int atual = Integer.parseInt(String.valueOf(num.charAt(i)));       if (anterior%2 == 0 && atual%2 == 0 && anterior != 0 && atual != 0) {         resultado += "*";       }       else if (anterior%2 == 1 && atual%2 == 1) { ...

(Coderbyte) Simple Mode - Solução

Using the Java language, have the function SimpleMode( arr ) take the array of numbers stored in arr and return the number that appears most frequently (the mode). For example: if arr contains [10, 4, 5, 2, 4] the output should be 4 . If there is more than one mode return the one that appeared in the array first ( ie. [5, 10, 10, 6, 5] should return 5 because it appeared first). If there is no mode return -1 . The array will not be empty. import java.util.*; import java.io.*; class Function {    int SimpleMode(int[] arr) {      int contador;     int contadorMaisFrequente = -1;     int maisFrequente = -1;         for (int i = 0; i < arr.length; i++) {       contador = 1;       for (int j = i+1; j < arr.length; j++) {            if (arr[j] == arr[i]) {      ...

(Coderbyte) Run Length - Solução

Using the Java language, have the function RunLength( str ) take the str parameter being passed and return a compressed version of the string using the Run-length encoding algorithm. This algorithm works by taking the occurrence of each repeating character and outputting that number along with a single character of the repeating sequence. For example: "wwwggopp" would return 3w2g1o2p . The string will not contain any numbers, punctuation, or symbols. import java.util.*; import java.io.*; class Function {    String RunLength(String str) {     String resposta = "";         int contador = 1;     char c = str.charAt(0);     for (int i = 1; i < str.length(); i++) {       if (str.charAt(i) == c) {         contador++;       }       else {         res...

(Coderbyte) Formatted Division - Solução

Have the function FormattedDivision( num1 , num2 ) take both parameters being passed, divide num1 by num2 , and return the result as a string with properly formatted commas and 4 significant digits after the decimal place. For example: if num1 is 123456789 and num2 is 10000 the output should be "12,345.6789" . The output must contain a number in the one's place even if it is a zero. import java.util.*; import java.io.*; import java.text.*; class Function {    String FormattedDivision(int num1, int num2) {     DecimalFormat df = new DecimalFormat("#,##0.0000");     return df.format((double)num1/num2);       }     public static void main (String[] args) {      // keep this function call here         Scanner  s = new Scanner(System.in);     Function c = new Function();     System.out.print(c.FormattedDivision(s.nextLine()...