• PAT (Advanced Level) Practice 1100 Mars Numbers (20分)


    1.题目

    People on Mars count their numbers with base 13:

    • Zero on Earth is called "tret" on Mars.
    • The numbers 1 to 12 on Earth 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

    2.代码

    #include<iostream>
    #include<string>
    #include<cstring>
    #include<sstream>
    using namespace std;
    string a[13] = { "tret","jan", "feb","mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
    string b[13] = { "","tam", "hel","maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
    bool isnum(string str)
    {
    	for (int i = 0; i < str.length(); i++)
    		if (!isdigit(str[i]))return false;
    	return true;
    }
    int change(string str)
    {
    	int number;
    	stringstream ss;
    	ss << str;
    	ss >> number;
    	return number;
    }
    
    int main()
    {
    	int n;
    	cin >> n;
    	string temp;
    	int aa=0, bb=0;
    	getchar();
    	for (int i = 0; i < n; i++)
    	{
    		getline(cin, temp);
    		if (isnum(temp)) 
    		{
    			aa = change(temp)/13;
    			bb = change(temp) % 13;
                	if (change(temp) == 0)cout << "tret";
    			if (aa != 0)cout << b[aa];
    			if (bb != 0) {if(aa!=0) cout << ' ' << a[bb] << endl;else cout<< a[bb] << endl;}
    			else cout << endl;
    		}
    		else
    		{
    			if (temp.find(' ') != string::npos)
    			{
    				string temp2 = temp.substr(temp.find(' ') + 1, temp.length() - 1);
    				temp = temp.substr(0, temp.find(' '));
    				for (int j = 0; j < 13; j++)
    					 if (temp == b[j]) { aa=j * 13; break; }
    				for (int j = 0; j < 13; j++)
    					if (temp2 == a[j]) { aa =aa+ j; break; }
    				cout << aa << endl;
    			}
    			else
    			{
    				for (int j = 0; j < 13; j++)
    				{
    					if (temp == a[j]) { cout << j << endl; break; }
    					else if (temp == b[j]) { cout << j * 13 << endl; break; }
    				}
    			}
    			
    		}
    	}
    
    }
  • 相关阅读:
    win7下安装配置tomcat,java运行环境
    Ubuntu 12.10安装配置JDK7环境
    全面介绍Linux终端命令
    Ubuntu 配置 Tomcat
    Linux手动导入导出mysql数据库
    navicat数据库管理软件(支持mysql,oracle,sqlserver,sqlite,postgreSQL)
    信号量(semaphore)和互斥量(mutex)
    最新版fcitx 4.1.2源码编译安装(ubuntu 10.04)
    Groovy动态语言简介
    读书笔记:《java脚本编程:语言、框架与模式》(2)jvm内部的脚本语言
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788846.html
Copyright © 2020-2023  润新知