Ordinal Numbers

描述

Ordinal numbers refer to a position in a series. Common ordinals include zeroth, first, second, third, fourth and so on. Ordinals are not often written in words, they are written using digits and letters. An ordinal indicator is a sign adjacent to a numeral denoting that it is an ordinal number, rather than a cardinal number. In English, the suffixes -st (e.g. 21st), -nd (e.g. 22nd), -rd (e.g. 23rd), and -th (e.g. 24th) are used. The rules are as follows:

If the tens digit of a number is 1, then write “th” after the number. For example: 13th, 19th, 112th, 9311th.
If the tens digit is not equal to 1, then use “st” if the units digit is 1, “nd” if the units digit is 2, “rd” if the units digit is 3, and “th” otherwise: For example: 2nd, 7th, 20th, 23rd, 52nd, 135th, 301st.
输入

There are multiple test cases. The first line of input is an integer T ≈ 1000 indicating the number of test cases.

Each test case consists of a cardinal number 0 ≤ n < 1,000,000,000.

输出

For each test case, output the corresponding ordinal number.

样例输入

5
1
2
3
4
1024

样例输出
1st
2nd
3rd
4th
1024th

分析:练习自己的英文能力,题目十分简单,小坑看仔细就好。

代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int T;
cin>>T;
while(T–)
{
int n;
cin>>n;
int m=n%10;
int M=n%100/10;
if (M1)
{
printf("%dth\n",n);
}
else
{
if (m
1)
{
printf("%dst\n",n);
}
else
if (m2)
{
printf("%dnd\n",n);
}
else
if (m
3)
{
printf("%drd\n",n);
}
else
{
printf("%dth\n",n);
}
}
}
return 0;
}

发布了40 篇原创文章 · 获赞 0 · 访问量 693

猜你喜欢

转载自blog.csdn.net/Skynamer/article/details/103377326