您的位置:首页 > 其它

A1100. Mars Numbers (20)

2016-03-11 21:58 288 查看

题目描述

People on Mars count their numbers with base 13:

Zero on Earth is called "tret" on Mars.
The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.
For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.
For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for
mutual translation between Earth and Mars number systems.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (< 100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.

Output Specification:

For each number, print in a line the corresponding number in the other language.

Sample Input:
4
29
5
elo nov
tam

Sample Output:
hel mar
may
115
13

题意解析

这道题目就是根据题目描述,进行地球数字和火星数字的互换。

解题思路

按照题目描述一步一步做就ok,注意点是格式问题;当输入13时,输出是tam,而不是tam tret。

参考代码

#include <cstdio>
#include <string.h>
#include <algorithm>

using namespace std;

const int maxn = 110;

char mars_number[maxn][maxn] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char mars_number1[maxn][maxn] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};

int n;

int main()
{
int l,result=0;
char str[maxn];
bool flag = false;
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++)
{
flag = false;
result = 0;
gets(str);
l = strlen(str);
for(int i=0;i<l;i++)
{
if(str[i]>='0' && str[i] <='9')
{
result = result * 10 + str[i] - '0';
flag = true;
}
}
if(flag)
{
int a,b;
a = result / 13;
b = result % 13;
if(a != 0 && b != 0)
printf("%s %s\n",mars_number1[a-1],mars_number[b]);
if(a != 0 && b == 0)
printf("%s\n",mars_number1[a-1]);
if(a == 0)
printf("%s\n",mars_number[b]);
}
else
{
int j = 0;
char curr[maxn];
for(int i=0;i<l;i++)
{
if(str[i] == ' ')
{
curr[j] = '\0';
for(int x = 0;x < 13;x++)
{
if(strcmp(curr,mars_number[x]) == 0)
{
result = result + x;
break;
}
}
for(int x = 0;x < 12;x++)
{
if(strcmp(curr,mars_number1[x]) == 0)
{
result = result + (x + 1) * 13;
break;
}
}
for(int x = 0;x <= j;x++)
{
curr[x] = '\0';
j = 0;
}
}
else
{
curr[j++] = str[i];
}
}
curr[j] = '\0';
for(int x = 0;x < 13;x++)
{
if(strcmp(curr,mars_number[x]) == 0)
{
result = result + x;
break;
}
}
for(int x = 0;x < 12;x++)
{
if(strcmp(curr,mars_number1[x]) == 0)
{
result = result + (x + 1) * 13;
break;
}
}
printf("%d\n",result);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat 算法