#6 Java Loops

HackerRank/HR-Java 2015. 11. 18. 00:38 by 후뤼한잉여

https://www.hackerrank.com/challenges/java-loops


Problem Statement

In this problem you will test your knowledge of Java loops. Given three integers ab, and n, output the following series:

a+20b,a+20b+21b,......,a+20b+21b+...+2n1b

Input Format

The first line will contain the number of testcases t. Each of the next t lines will have three integers, ab, and n.

Constraints:

0a,b50
1n15

0a,b50
1n15

Output Format

Print the answer to each test case in separate lines.

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

In the first case:

1st term=0+1*2=2
2nd term=0+1*2+2*2=6
3rd term=0+1*2+2*2+4*2=14

and so on.

보낸 답)

테스트 케이스가 많으면 메모리를 많이 먹겠지만 초보니까 쿨하게 패스~!(+ 거기다 범위가 있었다는걸 나중에 깨달음...)

import java.util.*;

import java.math.*;


public class Solution {


    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        

        int testCaseCnt = sc.nextInt();

        int a[] = new int[testCaseCnt];

        int b[] = new int[testCaseCnt];

        int n[] = new int[testCaseCnt];

        int sum;

        

        for(int i=0; i<testCaseCnt; i++) {

            a[i] = sc.nextInt();

            b[i] = sc.nextInt();

            n[i] = sc.nextInt();

        }

        

        for(int i=0; i<testCaseCnt; i++) {

            sum = 0;

            

            for(int j=0; j<n[i]; j++) {

                if(j == 0) {

                    sum +=  a[i];

                }

                sum += (b[i] * (int)Math.pow(2,j));

                System.out.print(sum + " ");

            }

            System.out.println();

        }

    }

}




'HackerRank > HR-Java' 카테고리의 다른 글

#9 Java Static Initializer Block  (0) 2015.11.23
#8 Java End-of-file  (0) 2015.11.23
#5 Java Output Formatting  (0) 2015.11.17
#4 Java Stdin and Stdout 2  (0) 2015.11.17
#3 Java If-Else  (0) 2015.11.17

#5 Java Output Formatting

HackerRank/HR-Java 2015. 11. 17. 23:50 by 후뤼한잉여

https://www.hackerrank.com/challenges/java-output-formatting


Take exactly 3 lines of input. Each line consists of a string and an integer. Suppose this is the sample input:

java 100
cpp 65
python 50

The strings will have at most 10 alphabetic characters and the integers will range between 0 to 999.

In each line of output there should be two columns. The string should be in the first column and the integer in the second column. This is the output for the input above:

================================
java           100 
cpp            065 
python         050 
================================

The first column should be left justified using exactly 15 characters. The integer of the second column should have exactly 3 digits. If the original input has less than 3 digits, you should pad with zeros to the left.

To make the problem easier, some part of the solution is already given in the editor, just complete the remaining parts.


입력은 문자열은 최대 10자리, 정수형은 0부터 999로 입력이 된다.

출력시에는 첫번째 컬럼 즉 문자열은 15자리로 왼쪽 정렬이 되어야 하며, 두번째 컬럼 즉 정수형은 3자리여야 한다.

정수 값이 3자리 보다 작으면 왼쪽에 0으로 채운다.


보낸 답)

printf를 사용하여 문자열은 15자리인데 왼쪽정렬(-)을 하고(%-15s), 정수형은 3자리인데 앞에 부족한 부분은 0으로 채워놓는 형식(%03d)으로 해서 제출했다.

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();

                System.out.printf("%-15s%03d\n",s1, x);

            }

            System.out.println("================================");


    }

}


'HackerRank > HR-Java' 카테고리의 다른 글

#8 Java End-of-file  (0) 2015.11.23
#6 Java Loops  (0) 2015.11.18
#4 Java Stdin and Stdout 2  (0) 2015.11.17
#3 Java If-Else  (0) 2015.11.17
#2 Java Stdin and Stdout 1  (0) 2015.11.17

#1 Revising the Select Query - 1

HackerRank/HR-SQL 2015. 11. 17. 11:52 by 후뤼한잉여

https://www.hackerrank.com/challenges/revising-the-select-query


Given a City table, whose fields are described as

+-------------+----------+
| Field       | Type     |
+-------------+----------+
| ID          | int(11)  |
| Name        | char(35) |
| CountryCode | char(3)  |
| District    | char(20) |
| Population  | int(11)  |
+-------------+----------+

write a query that will fetch all columns for every row in the table where the CountryCode = 'USA' (i.e, we wish to retreive data for all American cities) and the population exceeds 100,000.

City라는 테이블은 다음과 같은 명세로 주어져 있다.

모든 컬럼을 가져오는데 CountryCode는 USA로 하며 Population이 100,000 초과하는 도시에 대해서 조회하라는 듯 하다.


보낸 답)

SELECT  *

FROM    City

WHERE   CountryCode = 'USA'

AND     Population >= 100000;


Nav