We use the integers , , and to create the following series:
You are given queries in the form of , , and . For each query, print the series corresponding to the given , , and values as a single line of space-separated integers.
Input Format
The first line contains an integer, , denoting the number of queries.
Each line of the subsequent lines contains three space-separated integers describing the respective , , and values for that query.
Each line of the subsequent lines contains three space-separated integers describing the respective , , and values for that query.
Constraints
Output Format
For each query, print the corresponding series on a new line. Each series must be printed in order as a single line of space-separated integers.
Sample Input
2
0 2 10
5 3 5
Sample Output
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
Explanation
We have two queries:
- We use , , and to produce some series :... and so on.Once we hit , we print the first ten terms as a single line of space-separated integers.
- We use , , and to produce some series :We then print each element of our series as a single line of space-separated values.
import java.util.*;
import java.io.*;
import java.lang.Math.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
int j;
for(int i=0;i<t;i++){
int a = in.nextInt();//initial value
int b = in.nextInt();
int n = in.nextInt();//sequence number
int arr[]=new int[n];
for(j=0;j<n;j++)
{
int temp=a;
for(int inc=0;inc<=j;inc++)
{
temp=temp+(int)(Math.pow(2,inc)*b);
}
arr[j]=temp;
System.out.print(arr[j]+" ");
}
System.out.println();
}
in.close();
}
}
**************
import java.util.*;
import java.io.*;
import java.lang.Math.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
int j;
for(int i=0;i<t;i++){
int a = in.nextInt();//initial value
int b = in.nextInt();
int n = in.nextInt();//sequence number
int arr[]=new int[n];
for(j=0;j<n;j++)
{
int temp=a;
for(int inc=0;inc<=j;inc++)
{
temp=temp+(int)(Math.pow(2,inc)*b);
}
arr[j]=temp;
}
for(j=0;j<n;j++)
{
System.out.print(arr[j]+" ");
}
System.out.println();
}
in.close();
}
}
import java.io.*;
import java.lang.Math.*;
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
int j;
for(int i=0;i<t;i++){
int a = in.nextInt();//initial value
int b = in.nextInt();
int n = in.nextInt();//sequence number
int arr[]=new int[n];
for(j=0;j<n;j++)
{
int temp=a;
for(int inc=0;inc<=j;inc++)
{
temp=temp+(int)(Math.pow(2,inc)*b);
}
arr[j]=temp;
}
for(j=0;j<n;j++)
{
System.out.print(arr[j]+" ");
}
System.out.println();
}
in.close();
}
}
***********************************************************************************
Arrays - DS
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 .
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
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]+" "); } } }
2D Array - DS
Context
Given a 2D Array, :
Given a 2D Array, :
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
We define an hourglass in to be a subset of values with indices falling in this pattern in 's graphical representation:
a b c
d
e f g
There are hourglasses in , and an hourglass sum is the sum of an hourglass' values.
Task
Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.
Calculate the hourglass sum for every hourglass in , then print the maximum hourglass sum.
Note: If you have already solved the Java domain's Java 2D Array challenge, you may wish to skip this challenge.
Input Format
There are lines of input, where each line contains space-separated integers describing 2D Array ; every value in will be in the inclusive range of to .
Constraints
Output Format
Print the largest (maximum) hourglass sum found in .
Sample Input
1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 2 4 4 0
0 0 0 2 0 0
0 0 1 2 4 0
Sample Output
19
Explanation
contains the following hourglasses:
1 1 1 1 1 0 1 0 0 0 0 0
1 0 0 0
1 1 1 1 1 0 1 0 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0
1 1 0 0
0 0 2 0 2 4 2 4 4 4 4 0
1 1 1 1 1 0 1 0 0 0 0 0
0 2 4 4
0 0 0 0 0 2 0 2 0 2 0 0
0 0 2 0 2 4 2 4 4 4 4 0
0 0 2 0
0 0 1 0 1 2 1 2 4 2 4 0
The hourglass with the maximum sum () is:
2 4 4
2
1 2 4
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Scanner;
public class twoDArray {
public static void main(String[] args) {
/* Create an input Scanner instance for reading input */
Scanner stdIn = new Scanner(System.in);
/* Name some constants used in the program */
int rows = 6; // Number of rows in the matrix
int cols = 6; // Number of columns in the matrix
int minValueInArray = -9; // Minimum value of an element in the matrix
int elementsInHourGlass = 7;// Number of elements in an hourglass
// Set max to the minimum hourglass sum possible
int maxHourGlassSum = minValueInArray * elementsInHourGlass;
// Declare the matrix of numbers
int matrix[][] = new int[rows][cols];
int hourGlassSum; // Sum of the elements in the hourglass
/* Read the values for the matrix looping thru the rows */
for(int i=0; i < rows; i++){
/* Loop thru the columns for each row, reading the matrix */
for(int j=0; j < cols; j++){
/* Read the next value from standard input */
matrix[i][j] = stdIn.nextInt();
}
}
/* For debugging, print out the matrix */
for(int i=0; i < rows; i++){
for(int j=0; j < cols; j++){
// System.out.print(matrix[i][j] + " ");
}
// System.out.println();
}
/* Loop thru the possible starting points of the hourglass */
for (int i=0; i < (rows-2); i++) {
for (int j=0; j<(cols-2); j++) {
/* Print the hourglass starting position */
//System.out.println ("Checking hourglass at (" + i + "," + j + ")");
/* Print out the hourglass elements */
//System.out.println(matrix[i][j] + " " + matrix[i][j+1] + " " + matrix[i][j+2]);
// System.out.println(" " + matrix[i+1][j+1]);
// System.out.println(matrix[i+2][j] + " " + matrix[i+2][j+1] + " " + matrix[i+2][j+2]);
/* Compute the sum of the elements in the hourglass */
hourGlassSum = matrix[i][j] + matrix[i][j+1] + matrix[i][j+2] +
matrix[i+1][j+1] +
matrix[i+2][j] + matrix[i+2][j+1] + matrix[i+2][j+2];
/* Print out the sum of the elements in this hourglass */
//System.out.println("hour glass sum = " + hourGlassSum);
// System.out.println("max glass sum = " + maxHourGlassSum );
/* Is the new hourglass sum greater than the max found so far */
if (hourGlassSum > maxHourGlassSum) {
/* If so, then replace the max hour glass sum, with the current sum */
maxHourGlassSum = hourGlassSum;
}
}
}
/* Print out the maximum hour glass sum */
System.out.println(maxHourGlassSum);
}
}
No hay comentarios:
Publicar un comentario