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; }
    				}
    			}
    			
    		}
    	}
    
    }
  • 相关阅读:
    面试题19:包含min函数的栈
    编程之美 计算字符串的相似度
    android 数据持久化——I/O操作
    SSD磁盘,CPU居高不下,高并发的情况下,是不是mysql解析器耗费的cpu资源高?
    Eclipse、MyEclipse优化,提高运行速度
    Sonar入门学习
    Oracle 生成指定范围内随机日期
    ios中的GCD
    如何使用Win8系统自带杀毒软件
    安装Ubuntu版本linux过程中没有提示设置root用户密码问题的解决办法
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/12788846.html
Copyright © 2011-2022 走看看