zoukankan      html  css  js  c++  java
  • 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; }
    				}
    			}
    			
    		}
    	}
    
    }
  • 相关阅读:
    八月第二周学习心得
    七月第二周学习心得
    八月第一周学习
    八月第三周学习心得
    7月第一周学习心得
    php mysql_error()函数用法详解
    php mysql_select_db
    php中的释放“语句”unset和释放“函数”mysql_free_result()
    JavaScript]Cookie详解(转)
    Javascript类型转换的规则
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788846.html
Copyright © 2011-2022 走看看