안녕하세요, 매일프로그래밍 이번주 문제입니다.
정수(int)가 주어지면, 팰린드롬(palindrome)인지 알아내시오. 팰린드롬이란, 앞에서부터 읽으나 뒤에서부터 읽으나 같은 단어를 말합니다. 단, 정수를 문자열로 바꾸면 안됩니다.
Given an integer, check if it is a palindrome.
예제)
Input: 12345
Output: False
Input: -101
Output: False
Input: 11111
Output: True
Input: 12421
Output: True
풀이)
...더보기
- C/C++
#include <iostream>
using namespace std;
int main()
{
int num;
printf("input : ");
scanf("%d", &num);
int tmp1 = num;
int tmp2 = 0;
while(tmp1 > 0){
int a = tmp1 % 10;
tmp1 = tmp1 / 10;
tmp2 = (tmp2 + a) * 10;
}
tmp2 = tmp2 / 10;
if (num == tmp2)
printf("True");
else
printf("False");
return 0;
}
- C#
using System.IO;
using System;
class Program
{
static void Main()
{
int num;
Console.WriteLine("input : ");
int.TryParse(Console.ReadLine(),out num);
int tmp1 = num;
int tmp2 = 0;
while(tmp1 > 0){
int a = tmp1 % 10;
tmp1 = tmp1 / 10;
tmp2 = (tmp2 + a) * 10;
}
tmp2 = tmp2 / 10;
if (num == tmp2)
Console.WriteLine("True");
else
Console.WriteLine("False");
return ;
}
}
- Python
def main():
num = int(input("input : "))
tmp1 = num
tmp2 = 0
while tmp1 > 0:
a = tmp1 % 10
tmp1 = tmp1 / 10
tmp2 = (tmp2 + a) * 10
tmp2 = tmp2 / 10
if num == tmp2:
print True
else:
print False
main()
- Java
import java.util.Scanner;
public class HelloWorld{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
System.out.println("input : ");
int num = scan.nextInt();
int tmp1 = num;
int tmp2 = 0;
while(tmp1 > 0){
int a = tmp1 % 10;
tmp1 = tmp1 / 10;
tmp2 = (tmp2 + a) * 10;
}
tmp2 = tmp2 / 10;
if (num == tmp2) {
System.out.println("True");
}
else {
System.out.println("False");
}
}
}
'IT > 코딩테스트, 알고리즘, 파이썬, C, C++, C#, JAVA 등...' 카테고리의 다른 글
알고리즘 - 사다리만들기 (0) | 2019.05.01 |
---|
댓글