7-2 序数词的缩写
分数 10
切换布局
作者 李祥
单位 湖北经济学院
在英语里,序数词的缩写形式参见下表:
1st | 2nd | 3rd | 4th | 5th | 6th | 7th | 8th | 9th | 10th |
11th | 12th | 13th | 14th | 15th | 16th | 17th | 18th | 19th | 20th |
21st | 22nd | 23rd | 24th | 25th | 26th | 27th | 28th | 29th | 30th |
31st | 32nd | 33rd | 34th | 35th | 36th | 37th | 38th | 39th | 40th |
41st | 42nd | 43rd | 44th | 45th | 46th | 47th | 48th | 49th | 50th |
51st | 52nd | 53rd | 54th | 55th | 56th | 57th | 58th | 59th | 60th |
61st | 62nd | 63rd | 64th | 65th | 66th | 67th | 68th | 69th | 70th |
71st | 72nd | 73rd | 74th | 75th | 76th | 77th | 78th | 79th | 80th |
81st | 82nd | 83rd | 84th | 85th | 86th | 87th | 88th | 89th | 90th |
91st | 92nd | 93rd | 94th | 95th | 96th | 97th | 98th | 99th | 100th |
请找出规律,编写程序,解决此问题。
首先输入正整数 n,然后输入 n 个正整数 a1,a2,⋯,an,最后输出 n 个对应的序数词缩写,缩写词间空一格,行末没有多余的空格。
输入格式
n
a1,a2,⋯,an
输出格式
n 个序数词的缩写
输入样例
4
71 412 1043 53618
输出样例
71st 412th 1043rd 53618th
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
其实本题有点小坑,如果n%100的值为11,12,13它输出的不是th只要注意一下这点做出来问题应该不大
接下来是c++的写法:
#include<iostream>
using namespace std;
int main()
{
long int i,n;
cin>>n;
long int a[n];
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;i++)
{
if(a[i]<4||a[i]>20&&a[i]%10<4&&a[i]%100>20||a[i]%100<10)
{
switch(a[i]%10)
{
case 1:cout<<a[i]<<"st";break;
case 2:cout<<a[i]<<"nd";break;
case 3:cout<<a[i]<<"rd";break;
default:cout<<a[i]<<"th";break;
}
}
else
cout<<a[i]<<"th";
if(i<n-1)
cout<<" ";
}
}