martes, 11 de abril de 2017

JAVA hackerrank data structres JAVA

An array is a type of data structure that stores elements of the same type in a contiguous block of memory. In an array, , of size , each memory location has some unique index,  (where ), that can be referenced as  (you may also see it written as ).
Given an array, , of  integers, print each element in reverse order as a single line of space-separated integers.
Note: If you've already solved our C++ domain's Arrays Introduction challenge, you may want to skip this.
Input Format
The first line contains an integer,  (the number of integers in ).
The second line contains  space-separated integers describing .
Constraints
Output Format
Print all  integers in  in reverse order as a single line of space-separated integers.
Sample Input
4
1 4 3 2
Sample Output
2 3 4 1
SOLUCION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int arr[] = new int[n];
        for(int arr_i=0; arr_i < n; arr_i++){
            arr[arr_i] = in.nextInt();
        }
        
        for(int i=(arr.length-1); i>=0; i--)
            {
            System.out.print(arr[i]+" ");
        }
    }
    }


EN C
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


int solveMeFirst(int a, int b) {
    // Hint: Type return a+b; below
 return(a+b);
}
int main() {
    int num1,num2;
    scanf("%d %d",&num1,&num2);
    int sum; 
    sum = solveMeFirst(num1,num2);
    printf("%d",sum);
    return 0;
}

En Perl

sub solveMeFirst{ ($x,$y) = @_; # Hint: Type $res = $x + $y; below $res = $x + $y; return $res ; } $choice1 = <STDIN> ; $choice2 = <STDIN> ; $res = solveMeFirst($choice1 , $choice2); print "$res" ;



EN PHP


<?php

function solveMeFirst($a,$b){
  // Hint: Type return $a + $b; below  
  return $a + $b;
}
$handle = fopen ("php://stdin","r");
$_a = fgets($handle);
$_b = fgets($handle);
$sum = solveMeFirst((int)$_a,(int)$_b);
print ($sum);
fclose($handle);
?>

*****************************************************

Java Output Formatting

Java's System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf.
To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.
Input Format
Every line of input will contain a String followed by an integer.
Each String will have a maximum of  alphabetic characters, and each integer will be in the inclusive range from  to .
Output Format
In each line of output there should be two columns:
The first column contains the String and is left justified using exactly  characters.
The second column contains the integer, expressed in exactly  digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes.
Sample Input
java 100
cpp 65
python 50
Sample Output
================================
java           100 
cpp            065 
python         050 
================================
Explanation
Each String is left-justified with trailing whitespace through the first  characters. The leading digit of the integer is the  character, and each integer that was less than  digits now has leading zeroes.



import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner sc=new Scanner(System.in); System.out.println("================================"); for(int i=0;i<3;i++){ String s1=sc.next(); int x=sc.nextInt(); //Complete this line // System.out.println(s1 +" " +x); System.out.printf("%-14s %03d %n", s1, x); //note the use of printf // %-14s fifteen characters left-justified o to 14 // %03d padded with leading zero } System.out.println("================================"); } }


******************************************************************************

Java Loops I


Objective
In this challenge, we're going to use loops to help us do some simple math.
Task
Given an integer, , print its first  multiples. Each multiple  (where ) should be printed on a new line in the form: N x i = result.
Input Format
A single integer, .
Constraints
Output Format
Print  lines of output; each line  (where ) contains the  of  in the form:
N x i = result.
Sample Input
2
Sample Output
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Solution
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int i;
        for(i = 1; i<=10; i++)
            {
            System.out.print(N+ " x "+i +" = "+ N*i+ "\n");
        }
    }
}

No hay comentarios:

Publicar un comentario

Blogger Widgets