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; }
    				}
    			}
    			
    		}
    	}
    
    }
  • 相关阅读:
    题目1007:奥运排序问题(自定义排序问题)
    题目1005:Graduate Admission(录取算法)
    九度OJ小结2
    题目1049:字符串去特定字符(简单字符判断)
    题目1111:单词替换(字符串查找)
    题目1168:字符串的查找删除(字符串操作)
    题目1455:珍惜现在,感恩生活(多重背包问题)
    题目1454:Piggy-Bank(完全背包问题)
    题目1453:Greedy Tino(dp题目)
    题目1452:搬寝室(dp题目)
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788846.html
Copyright © 2011-2022 走看看