• 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; }
    				}
    			}
    			
    		}
    	}
    
    }
  • 相关阅读:
    VBA trouble
    深入浅出Automation Anywhere
    sqlserver--install/uninstall
    linux遇到的问题
    Java并发编程:线程池的使用(转载)
    java——线程
    关于项目管理工具 maven
    从xml文件取值
    jquery
    小结-9.20
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788846.html
Copyright © 2020-2023  润新知