이 페이지는 코딩도장 데이터의 읽기 전용 정적 보관본입니다.

Cardinal to Ordinal 기수를 서수로 바꾸기

기수(Cardinal)를 입력하면 영어 서수(Ordinal)로 출력하는 함수를 작성합니다.

1, 21, 31, 41, ... → 1st, 21st, 31st, 41st, ...
2, 22, 32, 42, ... → 2nd, 22nd, 32nd, 42nd, ...
3, 23, 33, 43, ... → 3rd, 23rd, 33rd, 43rd, ...
11, 12, 13, 111, 112, 113, 211, 212, 213, ...  → 11th, 12th, 13th, 111th, 112th, 113th, 211th, 212th, 213th, ...
4, 5, 6, 11, 12, 13, 101, 111, 112, ... → 4th, 5th, 6th, 11th, 12th, 13th, 101th, 111th, 112th, ...
서수 ordinal 영어

2020/04/21 22:37

Katherine

101th -> 101st 입니다. - Noname, 2020/07/13 02:00
21st -> 21th, 101th -> 101st 등이 맞는 표현 같습니다. 확인부탁드립니다. - Jaeman Lee, 2021/07/15 15:50

41개의 풀이가 있습니다.

Python

def num(n):
    if n%10==1 and n%100!=11:
        return str(n)+"st"
    elif n%10==2 and n%100!=12:
        return str(n)+"nd"
    elif n%10==3 and n%100!=13:
       return str(n)+"rd"
    else:
        return str(n)+"th"
print (num(1))
print (num(11))
print (num(22))
print (num(12))
print (num(23))
print (num(13))
print (num(34))
print (num(101))
print (num(111))
print (num(112))
print (num(113))
print (num(114))
1st
11th
22nd
12th
23rd
13th
34th
101st
111th
112th
113th
114th

2020/04/21 22:41

Katherine

inputN = input()
N = int(inputN[len(inputN)-1])
if N == 1:
    print(inputN + 'st')
elif N == 2:
    print(inputN + 'nd')
elif N == 3:
    print(inputN + 'rd')
else:
    print(inputN + 'th')

2020/04/21 23:31

BlakeLee

def CtO(s):
    Lst = s.split(', ') #문자열을 리스트로 변환
    result = []
    for i in Lst:
        if i == '1':
            Ordinal = str(i) + 'st'
            result.append(Ordinal)
        elif i == '2':
            Ordinal = str(i) + 'nd'
            result.append(Ordinal)
        elif i == '3':
            Ordinal = str(i) + 'rd'
            result.append(Ordinal)
        else:
            Ordinal = str(i) + 'th'
            result.append(Ordinal)
    print(result)


CtO('1, 2, 4, 5, 6, 11, 12, 13, 101')

2020/04/21 23:45

ptjddn95

Java

package ordinal;
import java.util.Scanner;
public class Index {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Index i = new Index();
        int input;
        do {
            System.out.print("Input a number. (Quit: input 0):");
            input = in.nextInt();
            if(input!=0)
                System.out.println("Ordinal:"+i.ordinal(input));
        }while(input!=0);
    }

    String ordinal(int n) {
        String ord;
        if(n%10==1 && n%100!=11)
            ord="st";
        else if(n%10==2 && n%100!=12)
            ord="nd";
        else if(n%10==3 && n%100!=13)
            ord="rd";
        else ord="th";
        return (n+ord);
    }
}

Result:

Input a number. (Quit: input 0):1
Ordinal:1st
Input a number. (Quit: input 0):122
Ordinal:122nd
Input a number. (Quit: input 0):112
Ordinal:112th
Input a number. (Quit: input 0):143
Ordinal:143rd
Input a number. (Quit: input 0):145
Ordinal:145th
Input a number. (Quit: input 0):213
Ordinal:213th
Input a number. (Quit: input 0):0

2020/04/22 16:48

Katherine

Cardinal=(input("input number"))
if int(Cardinal)!=11 and int(Cardinal)%10==1 or Cardinal==1:print(Cardinal+'st')
elif int(Cardinal)!=12 and int(Cardinal)%10==2 or Cardinal==2:print(Cardinal+'nd')
elif int(Cardinal)!=13 and int(Cardinal)%10==3 or Cardinal==3:print(Cardinal+'rd')
else :print(Cardinal+'th')


2020/04/24 06:23

탕자감

num1 = input('')
numlist = list(str(num))
if numlist[-1] == '1':
        result = str(num1)+'st'
        print(result)
elif numlist[-1] == '2':
        result = str(num1)+'nd'
        print(result)
elif numlist[-1] == '3':
        result = str(num1)+'rd'
        print(result)
else:
        result = str(num1)+'th'
        print(result)

2020/04/27 00:13

PythonLover&Master_JK73

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int ori =0;
        int result=0;

        System.out.println("원하는 수를 입력하세요 : ");
        ori = sc.nextInt();

        if(ori>=10 && ori <=20) {
            result =0;
        }

        else if(ori>=100) {
            result = 0;
        }

        else if(ori % 10 ==1)
        {
            result = 1;
        }

        else if(ori % 10 ==2)
        {
            result = 2;
        }

        else if(ori %10 ==3) {
            result =3;
        } 

        else {
            result = 0;
        }



        switch(result) {
        case 1: 
            System.out.println(ori+"st");
            break;
        case 2:
            System.out.println(ori + "nd");
            break;
        case 3 :
            System.out.println(ori+"rd");
            break;
        default:
            System.out.println(ori+"th");
            break;


        }
    }
}

2020/05/04 16:37

민지킴

2020/05/05 03:26

high hi

def chnum(n) 
if n%1==1 and n%100!=11:
return str(n)+"st" 
elif n%2==2 and n%100! =12:
return str(n)+"nd" 
elif n%3==3 and n%100! = 13:
return str(n) + "rd" 
else:
return str(n) + "st"
``````{.python}

def chnum(n) if n%1==1 and n%100!=11: return str(n)+"st" elif n%2==2 and n%100! =12: return str(n)+"nd" elif n%3==3 and n%100! = 13: return str(n) + "rd" else: return str(n) + "st"

2020/05/06 11:32

loderun Lee

def cardinal_to_ordinal(n):
    string = str(n)
    if n >= 100:
        return string + "th"
    elif string[-1] == "1":
        return string + "st"
    elif string[-1] == "2":
        return string + "nd"
    elif string[-1] == "3":
        return string + "rd"
    else:
        return string + "th"

2020/05/07 00:34

김준혁

Clojure 입니다. 간단하게 만들어 봤습니다.

(defn c2o [s] (str s (if (= \1 (last (butlast s))) "th" (case (last s) \1 "st" \2 "nd" \3 "rd" "th"))))
(dorun (map #((comp println c2o str) %) '(1 21 31 2 22 32 3 23 33 11 12 13 111 112 113 4 5 6)))

2020/05/08 03:40

최수현

파이썬입니다.

cardinal_num = int(input("input cardinal number. : "))

if 10 < cardinal_num < 20 :
    print(f"{cardinal_num}th")
else:
    if int(str(cardinal_num)[-1]) == 1:
        print(f"{cardinal_num}st")
    elif int(str(cardinal_num)[-1]) == 2:
        print(f"{cardinal_num}nd")
    elif int(str(cardinal_num)[-1]) == 3:
        print(f"{cardinal_num}rd")
    else:
        print(f"{cardinal_num}th")

2020/05/08 10:04

peca lee

def conversion(num):
    s = str(num)
    if s[-2:] == "11" or s[-2:] == "12" or s[-2:] == "13":
        return s + "th"
    elif s[-1] == "1":
        return s + 'st'
    elif s[-1] == "2":
        return s + 'nd'
    elif s[-1] == "3":
        return s + 'rd'
    else:
        return s + 'th'

2020/05/21 03:39

이정환

C#

static string GetSuffix(int number)
        {
            string suffix = string.Empty;
            switch (number)
            {
                case int n when n % 10 == 1 && n % 100 != 11:
                    suffix = "st";
                    break;
                case int n when n % 10 == 2 && n % 100 != 12:
                    suffix = "nd";
                    break;
                case int n when n % 10 == 3 && n % 100 != 13:
                    suffix = "rd";
                    break;
                default:
                    suffix = "th";
                    break;
            }
            return $"{number}{suffix}";
        }

2020/06/04 21:59

mohenjo

def num(n): if n%10==1 and n%100!=11: return str(n)+"st" elif n%10==2 and n%100!=12: return str(n)+"nd" elif n%10==3 and n%100!=13: return str(n)+"rd" else: return str(n)+"th"

2020/06/17 05:11

jay lee

a=int(input())
a1=str(a) 
if a==11 or a==12 or a==13:
    print(a1+"th")
elif a <100:
    if a1[-1]=='1':
        print(a1+"st")
    elif a1[-1]=='2':
        print(a1+"nd")
    elif a1[-1]=='3':
        print(a1+"rd")
    else:
        print(a1+"th")
else:
    print(al+"th")

2020/06/17 06:44

SREBP1c

Go

package main

import "fmt"

func Ordinal(number int) (result string) {
    // *1*이 아닐 때
    if (number / 10) % 10 != 1 {
        switch number % 10 {
        case 1:
            result = "st"
        case 2:
            result = "nd"
        case 3:
            result = "rd"
        default:
            result = "th"
        }
    } else {
        // *1*일 때
        result = "th"
    }
    return
}

func main() {
    for {
        fmt.Print("숫자를 입력하세요 (0 입력 = 종료): ")
        var number int

        _, _ = fmt.Scanf("%d", &number)

        if number == 0 {
            break
        }
        fmt.Printf("%d%s\n", number, Ordinal(number))
    }
}

2020/07/06 18:02

구루마

s = input()
n = int(s)
if not 11 <= n % 100 <= 19 and 1 <= n % 10 <= 3:
    print(s + {1: "st", 2: "nd", 3: "rd"}[n % 10])
else:
    print(s + "th")

2020/07/13 02:16

Noname

def conversion_cord_ord(n):
    a = str(n)
    if a[-1] == '1' and a[-2] != '1':
        return a+'st'
    elif a[-1] == '2' and a[-2] != '1':
        return a+'nd'
    elif a[-1] == '3' and a[-2] != '1':
        return a+'rd'
    else:
        return a+'th'

2020/08/06 16:59

현상민

using System;
using System.Collections.Generic;

namespace _61일차_10월01일
{        
    class MainApp
    {
        static void Main(string[] args)
        {
            Console.Write("Input Number (Split Setting : ',') : ");
            string Input_Data = Console.ReadLine();
            string[] string_Array = Input_Data.Split(',');
            List<string> Temp = new List<string>();
            for(int i = 0; i < string_Array.Length; i++)
            {
                if(string_Array[0] == "1")
                {
                    Temp.Add(string_Array[i] + "st");
                }
                else if (string_Array[0] == "2")
                {
                    Temp.Add(string_Array[i] + "nd");
                }
                else if (string_Array[0] == "3")
                {
                    Temp.Add(string_Array[i] + "rd");
                }
                else
                    Temp.Add(string_Array[i] + "th");
            }

            foreach (var Result in Temp)
            {
                Console.Write($"{Result} ");
            }
        }
    }
}

2020/10/01 23:30

MinSeung Kang

def last_digit_reader(x):
    last1digit = x[-1:]
    if int(last1digit) == 1:
        print(x + "st")
    elif int(last1digit) == 2:
        print(x + "nd")
    elif int(last1digit) == 3:
        print(x + "st")
    else:
        print(x + "th")

def cardinal2ordinal(x):
    if len(x) > 1:
        last2digit = x[-2:]
        if int(last2digit) < 20:
            print(x + "th")
        else:
            last_digit_reader(x)
    else:
        last_digit_reader(x)

positive_integer = input("Enter a positive integer: ")
assert int(positive_integer) > 0, "Must enter a positive integer"
cardinal2ordinal(positive_integer)        

2020/10/03 23:00

돈 벌면 뭐하노

def Ordinal():
    n=input()
    if (len(n)>1 and n[-2]=='1')or(int(n[-1])>3):
        return n+'th'
    else:
        if n[-1]=='1':
            return n+'st'
        elif n[-1]=='2':
            return n+'nd'
        elif n[-1]=='3':
            return n+'rd'
print(Ordinal())

2020/10/30 10:47

AppleFarmer

num=int(input('숫자를 입력하시오:\n'))

if  (num<100 and num%10==1) or (num>=100 and num%100==1):
    print(num,'st')
elif (num<100 and num%10==2) or (num>=100 and num%100==2):
    print(num,'nd')
elif (num<100 and num%10==3) or (num>=100 and num%100==3):
    print(num,'rd')
else:
    print(num,'th')

2020/11/12 07:44

정코더

class goodConverter:
    def __init__(self):
        pass
    def convert2Ordinal(self,list):
        for i in list:
            j = self.toOrdinal(i)
            print(j,end=" ")
        print("")
    def toOrdinal(self,i):
        n = int(str(i)[-1])
        if n==1 and (i<10 or i>20):
            tail = "st"
        elif n==2 and (i<10 or i>20):
            tail = "nd"
        elif n==3 and (i<10 or i>20):
            tail = "rd"
        else:
            tail = "th"
        j = str(i)+tail
        return j

a = goodConverter()
a.convert2Ordinal([1, 21, 31, 41])
a.convert2Ordinal([2, 22, 32, 42])
a.convert2Ordinal([3, 23, 33, 43])
a.convert2Ordinal([11, 12, 13, 111, 112, 113, 211, 212, 213])
a.convert2Ordinal([ 4,  5,  6,  11,  12,  13, 101, 111, 112])

2020/11/14 14:47

footsize

numberList = input().split(",")
result = []

for i in numberList:
    if 10 < int(i[-2:]) < 20:
        result.append(i + "th")
    elif i[-1] == "1":
        result.append(i + "st")
    elif i[-1] == "2":
        result.append(i + "nd")
    elif i[-1] == "3":
        result.append(i + "rd")
    else:
        result.append(i + "th")
print(result)

2020/11/23 18:55

김우석

def Ordinal(text):
    temp = int(text)
    if temp%10 == 1 and temp%100 != 11:
        return text+'st'
    elif temp%10 == 2 and temp%100 != 12:
        return text+'nd'
    elif temp%10 == 3 and temp%100 != 13:
        return text+'rd'
    else:
        return text+'th'

print(Ordinal(input('NUMBER : ')))

2020/11/27 08:21

DSHIN

def ordinal(n):

  if len(n)==1:

    if n[-1]=='1':

      n=n+"st"

    elif n[-1]=='2':

      n=n+'nd'

    elif n[-1]=='3':

      n=n+'rd'

  else:

    if n[-1]=='1' and n[-2]!='1':

      n=n+"st"

    elif n[-1]=='2' and n[-2]!='1':

      n=n+"nd"

    elif n[-1]=='3' and n[-2]!='1':

      n=n+"rd"

    else:

     n=n+"th"

  return n


word=input('write something')

words=word.split(',')

print(words)

for i in range(0,len(words),1):

   k=ordinal(words[i])

  print(k)

  words[i]=k

  print(words)

print(", ".join(words))

2020/12/10 19:50

전준혁

a=str(input())

if a[-1]=='1' and a[-2]!='1':
    print(a+'st')
elif a[-1]=='2' and a[-2]!='1':
    print(a+'nd')
elif a [-1]=='3' and a[-2]!='1':
    print(a+'rd')
else:
    print(a+'th')

2020/12/20 22:32

코딩팡팡

def ordinary(n):
    if n[-1] == '1':
        end = "st"
    elif n[-1] == '2':
        end = 'nd'
    elif n[-1] == '3':
        end = 'rd'
    else:
        end = 'th'
    return end

n = input()
end = ordinary(n)

print(f'{n} -> {n}{end}')

2021/02/13 12:42

asdfa

x=input('기수 입력')

xlist=x.split(', ')
dic={'1':'st', '2':'nd', '3':'rd'}
ele={'11':'th', '12':'th', '13':'th'}
result=[]

for i in xlist:
    if i in ele:
        result.append(i+ele[i])
    else:
        if i in dic:
            result.append(i+dic[i])
        else:
            result.append(i+'th')

print((', ').join(result))


2021/03/12 10:03

최우진

x = int(input('기수 → 서수 :'))
if 10<x%100<14 : print(f'{x}th')
elif x%10 == 1: print(f'{x}st')
elif x%10 == 2 :print(f'{x}nd')
elif x%10 == 3 :print(f'{x}rd')
else : print(f'{x}th')

2021/05/24 09:50

약사의혼자말

#codingdojing_cardinal2ordinal

import sys

#command prompot // args = sys.argv[2:] 로 list 받기
#1st, 2nd, 3rd, 4th, 11th, 12th, 101st, 102nd, 103rd, 111th,.. 


car_list = input().split(',') #['1', '21', '23', ...]
ord_list = []

for n in car_list:
    if (int(n)%100) == 1:
        ord_list.append(n+'st')
    elif (int(n)%100) == 2:
        ord_list.append(n+'nd')
    elif (int(n)%100) == 3:
        ord_list.append(n+'rd')
    else:
        ord_list.append(n+'th')

print(','.join(ord_list))






2021/07/15 15:48

Jaeman Lee

파이썬 3.8.10으로 작성되었습니다.

data = input()
if data[-1] == '1' and data[-2] != '1':
    print(f'{data}st')
elif data[-1] == '2' and data[-2] != '1':
    print(f'{data}nd')
elif data[-1] == '3' and data[-2] != '1':
    print(f'{data}rd')
else:
    print(f'{data}th')


2021/07/28 17:16

baek choi

def number_distinguish(a):
    i = str(a)
    if i[-1] == '1' and i[-2] != '1':
        print(i+'st')
    elif i[-1] == '2' and i[-2] != '1':
        print(i+'nd')
    elif i[-1] == '3' and i[-2] != '1':
        print(i+'rd')    
    else:
        print(i+'th')
if __name__ == '__main__':
    number_distinguish(37)

2021/09/15 19:04

서현준

result=[]
for i in input().split(', '):
    if i[-1] == '1' : i+='st'
    elif i[-1] == '2' : i+='nd'
    elif i[-1] == '3' : i+='rd'
    else: i+='th'
    result.append(i)
print(', '.join(result))

2021/09/19 17:05

ninanino

static void ordinal(int... x) {
        for (int i = 0; i < x.length; i++) {
            if(x[i]%10 == 1 && x[i]%100 != 11) {
                System.out.print(x[i] + "st ");
            }else if(x[i]%10 == 2 && x[i]%100 != 12) {
                System.out.print(x[i] + "nd ");
            }else if(x[i]%10 == 3 && x[i]%100 != 13) {
                System.out.print(x[i] + "rd ");
            }else {
                System.out.print(x[i] + "th ");
            }
        }
        System.out.println("");
    }

    public static void main(String[] args) {
        ordinal(1,21,31,41);
        ordinal(2,22,32,42);
        ordinal(3,23,33,43);
        ordinal(11,12,13,111,112,113,211,212,213);
        ordinal(4,5,6,11,12,13,101,111,112);
    }

2021/10/24 11:59

박대현

for i in range(1,1000):
    if i % 10 == 1 and i % 100 != 11:
        print(str(i) + "st")
    elif i % 10 == 2 and i % 100 != 12:
        print(str(i)+"nd")
    elif i % 10 ==3 and i % 100 != 13:
        print(str(i)+"rd")
    else:
        print(str(i)+"th")

2021/12/02 22:40

semipooh

while True:
    cardinal = int(input("기수를 입력해주세요"))
    if cardinal > 0 :
        break


if cardinal == 1 or cardinal >=4 :
    print(str(cardinal)+"th")
elif cardinal == 2 :
    print(str(cardinal)+"nd")
elif cardinal == 3 :
    print(str(cardinal)+"rd")

2021/12/18 19:51

양캠부부

a = input()
if a[-1] == '1' and a[-2] != '1':
    print(a+'st')
elif a[-1] == '2'and a[-2] != '1':
    print(a+'nd')
elif a[-1] == '3'and a[-2] != '1':
    print(a+'rd')
else:
    print(a+'th')

2022/01/26 13:52

로만가

Python. 11과 12의 경우를 생각하지 못해 수정했습니다.

cardinal=int(input('임의의 숫자를 입력하시오. : '))
if cardinal % 10 == 1 and cardinal % 100 !=11:
    print(str(cardinal)+'st')
elif cardinal % 10 == 2 and cardinal % 100 !=12:
    print(str(cardinal)+'nd')
elif cardinal % 10 == 3:
    print(str(cardinal)+'rd')
else:
    print(str(cardinal)+'th')

2022/10/28 17:12

Frye 'de Bacon

num = input('숫자를 입력하세요: ')

if num[-1] == '1' and not (len(num) >1 and num[-2] == '1'):
    print('{0}st'.format(num))
elif num[-1] == '2' and not (len(num) >1 and num[-2] == '1'):
    print('{0}nd'.format(num))
elif num[-1] == '3' and not (len(num) >1 and num[-2] == '1'):
    print('{0}rd'.format(num))
else:
    print('{0}th'.format(num))

2023/07/22 17:15

insperChoi

목록으로