본문 바로가기

코딩테스트

CodeSignal / Intro / The Journey Begins

여기는 엄청엄청 쉽다 ..!

SOOoooooooooooooooo EEEEEAsY~~~~~~~~~


Ⅰ. add

 

Write a function that returns the sum of two numbers. // 두 수의 합 리턴

Example

For param1 = 1 and param2 = 2, the output should be

add(param1, param2) = 3.

Input/Output

[execution time limit] 3 seconds (java)

[input] integer param1

Guaranteed constraints:

-1000 ≤ param1 ≤ 1000.

[input] integer param2

Guaranteed constraints:

-1000 ≤ param2 ≤ 1000.

[output] integer

The sum of the two inputs.

》》

int add(int param1, int param2) {

return param1+param2;

}

Ⅱ. centuryFromYear

 

Given a year, return the century it is in. The first century spans from the year 1 up to and including the year 100, the second - from the year 101 up to and including the year 200, etc.

// 주어진 Year의 세기를 리턴 _ 1~100 year -> 1 century , 101~200 year -> 2 century

Example

For year = 1905, the output should be

centuryFromYear(year) = 20;

For year = 1700, the output should be

centuryFromYear(year) = 17.

Input/Output

[execution time limit] 3 seconds (java)

[input] integer year

A positive integer, designating the year.

Guaranteed constraints:

1 ≤ year ≤ 2005.

[output] integer

The number of the century the year is in.

》》

int centuryFromYear(int year) {

int y=year/100;

if (year%100 >0){ // year를 100으로 나눈 나머지가 0보다 클 경우 (year/100) +1

return y+1;

}else{ return y;}

}

Ⅲ. checkPalindrome

 

Given the string, check if it is a palindrome.

// palindrome -> 순서를 거꾸로 나열해도 원래 값과 같은 문자열 _ 토마토..기러기.. 이런거

Example

For inputString = "aabaa", the output should be

checkPalindrome(inputString) = true;

For inputString = "abac", the output should be

checkPalindrome(inputString) = false;

For inputString = "a", the output should be

checkPalindrome(inputString) = true.

Input/Output

[execution time limit] 3 seconds (java)

[input] string inputString

A non-empty string consisting of lowercase characters.

Guaranteed constraints:

1 ≤ inputString.length ≤ 105.

[output] boolean

true if inputString is a palindrome, false otherwise.

》》

boolean checkPalindrome(String inputString) {

char[] arr= inputString.toCharArray(); // 문자열을 문자로 배열에 저장

int j=(inputString.length()/2);

int k=inputString.length()-1; // 배열의 시작은 0, 끝은 length-1

if(inputString.length()%2== 0){ // 문자의 개수가 짝수

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

if(arr[i] != arr[k-i]){

return false;

}

}return true;

}else{ // 문자의 개수가 홀수

for(int i=0;i<j+1;i++){

if(arr[i] != arr[k-i]){

return false;

}

}return true;

}

}