C++ STL map B1044/A1100.火星数字(读取带空格的string : 使用getline(cin,str)函数)

用了打表的技巧

#include <bits/stdc++.h>
#include<math.h>
#include <string>
using namespace std;
const int maxn = 40010;//最大学生人数
//[0,12]的火星文
string unitDigit[13]  = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
//13的[0,12]倍的火星文
string tenDigit[13] = {"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
string numToStr[170];//数字——>火星文
map<string,int> strToNum;//火星文——>数字
void init(){
    for(int i =0;i<13;++i){
        numToStr[i] = unitDigit[i];//个位为[0,12],十位为0
        strToNum[unitDigit[i]] = i;
        numToStr[i * 13] = tenDigit[i];//十位为[0,12],个位为0
        strToNum[tenDigit[i]] = i * 13;
    }
    for(int i = 1;i<13;++i){
        for(int j = 1;j<13;++j){
            string str = tenDigit[i] + " " + unitDigit[j];
            numToStr[i * 13 + j] = str;//数字——>火星文
            strToNum[str] = i * 13 + j; //火星文——>数字
        }
    }
}
int main(){
    init();//打表
    int T;
    scanf("%d%*c",&T);
    while(T--){
        string str;
        //cin>>str;
        getline(cin,str);
        if(str[0] >= ‘0‘ && str[0] <= ‘9‘){
            int num = 0;//字符串转换为数字
            for(int i=0;i<str.length();++i){
                num = num * 10 + (str[i] - ‘0‘);
            }
            cout<<numToStr[num]<<endl;
        }else{
            cout<<strToNum[str]<<endl;
        }
    }
    system("pause");
    return 0;
} 

原文地址:https://www.cnblogs.com/JasonPeng1/p/12203365.html

时间: 2024-08-29 17:27:33

C++ STL map B1044/A1100.火星数字(读取带空格的string : 使用getline(cin,str)函数)的相关文章

C++ STL map A1071 Speech Patterns(25) (注意如何从字符串里 分割出单词,注意读取整行带空格的string 需要使用 getlint(cin,str) 函数)

#include <bits/stdc++.h> #include<math.h> #include <string> using namespace std; const int maxn = 40010;//最大学生人数 bool check(char c){ if(c >= '0' && c<= '9') return true; if(c >= 'A' && c<= 'Z') return true; if

1044. 火星数字(20)

1044. 火星数字(20) 火星人是以13进制计数的: 地球人的0被火星人称为tret. 地球人数字1到12的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec. 火星人将进位以后的12个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou. 例如地球人的数字"29"翻译成火星文就是"hel mar":而

1044 火星数字 (20 分)

火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec. 火星人将进位以后的 12 个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou. 例如地球人的数字 29 翻译成火星文就是 hel mar:而火星文 elo nov 对应地球数字 1

ZOJ1109_Language of FatMouse(STL/map)

解题报告 题意: 略. 思路: map应用. #include <algorithm> #include <iostream> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <map> using namespace std; map<string,stri

PTA乙级 (*1044 火星数字 (20分))

1044 火星数字 (20分) https://pintia.cn/problem-sets/994805260223102976/problems/994805279328157696 #include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath> #include <algorithm> using namesp

hdu 1075 map的使用 字符串截取的常用手段 以及string getline 使用起来的注意事项

首先说字符串的截取套路吧 用坐标一个一个的输入 用遍历的方式逐个去检查字符串中的字符是否为符合的情况 如果是的话 把该字符放入截取string 中 让后坐标前移 如果不是的话 截取结束 坐标初始化 然后是map的使用 头文件为 <map> 定义的话 map<类型,类型> 变量名 对于map中值的传入的话 直接用数组的形式就好 这里由于有搜索 所以还要用到find函数   如果找不到的话 find返回值为end() 再就是string 类型变量的使用要注意 string类型的变量在下

1044 火星数字(map+打表)

火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec. 火星人将进位以后的 12 个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou. 例如地球人的数字 29 翻译成火星文就是 hel mar:而火星文 elo nov 对应地球数字 1

PAT-乙级-1044. 火星数字(20)

1044. 火星数字(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 火星人是以13进制计数的: 地球人的0被火星人称为tret. 地球人数字1到12的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec. 火星人将进位以后的12个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo,

HDU 1004 Let the Balloon Rise【STL&lt;map&gt;】

Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 123800    Accepted Submission(s): 48826 Problem Description Contest time again! How excited it is to see balloons floating ar