1100. Mars Numbers (20) / 1044. 火星数字(20), 字符串坑

来源:互联网 发布:手机信号干扰器软件 编辑:程序博客网 时间:2024/05/22 03:21

题目地址:http://www.patest.cn/contests/pat-a-practise/1100
People on Mars count their numbers with base 13:

Zero on Earth is called “tret” on Mars.
The numbers 1 to 12 on Earch 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

14分 代码 如下

#include <stdio.h>#include <string>#include <vector>#include <iostream>#include <sstream>using namespace std;string d[] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec","tam"};  string h[] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};  void fun1(string s){    int tmp ;    stringstream ss ;    ss << s ;    ss >> tmp ;    if(tmp < 13)    {        cout << d[tmp] << endl;    }else{        int high =  tmp / 13 ;        int low = tmp % 13 ;        cout << h[high-1] << " " << d[low] << endl;    }}void fun2(string s){    vector<string> vt ;    istringstream is(s);    string w;    while(is>> w)    {        vt.push_back(w);    }    if((int)vt.size() == 1)    {        for(int i = 0 ; i <= 13 ; i++)        {            if(d[i] == vt[0] )            {                cout << i << endl;                return ;            }        }    }else{        int n1 , n2 ;        for(int i = 0 ; i < 12 ; i++)        {            if(h[i] == vt[0] )            {                n1 = i+1 ;                break;            }        }        for(int i = 0 ; i <= 12 ; i++)        {            if(d[i] == vt[1] )            {                n2 = i ;                break;            }        }        printf("%d\n",n1*13+n2);    }}int main(){    //freopen("in.txt","r",stdin);    int n ;    int i ;    scanf("%d\n",&n);    string st;    while(n -- )    {        getline(cin , st);        if(st[0] >= '0' && st[0] <= '9')        {            fun1(st);        }else{            fun2(st);        }    }    return 0 ;}

16分代码如下

#include <stdio.h>#include <string>#include <vector>#include <iostream>#include <sstream>using namespace std;string d[] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec","tam"};  string h[] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};  void fun1(string s){    int tmp ;    stringstream ss ;    ss << s ;    ss >> tmp ;    if(tmp < 13)    {        cout << d[tmp] << endl;    }else{        int high =  tmp / 13 ;        int low = tmp % 13 ;        if(low == 0)            cout << h[high-1] << endl;        else            cout << h[high-1] << " " << d[low] << endl;    }}void fun2(string s){    vector<string> vt ;    istringstream is(s);    string w;    while(is>> w)    {        vt.push_back(w);    }    if((int)vt.size() == 1)    {        for(int i = 0 ; i <= 13 ; i++)        {            if(d[i] == vt[0] )            {                cout << i << endl;                return ;            }        }    }else{        int n1 , n2 ;        for(int i = 0 ; i < 12 ; i++)        {            if(h[i] == vt[0] )            {                n1 = i+1 ;                break;            }        }        for(int i = 0 ; i <= 12 ; i++)        {            if(d[i] == vt[1] )            {                n2 = i ;                break;            }        }        printf("%d\n",n1*13+n2);    }}int main(){    freopen("in.txt","r",stdin);    int n ;    int i ;    scanf("%d\n",&n);    string st;    while(n -- )    {        getline(cin , st);        if(st[0] >= '0' && st[0] <= '9')        {            fun1(st);        }else{            fun2(st);        }    }    return 0 ;}

AC代码如下:

#include <stdio.h>#include <string>#include <vector>#include <iostream>#include <sstream>using namespace std;string d[] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec","tam"};  string h[] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};  void fun1(string s){    int tmp ;    stringstream ss ;    ss << s ;    ss >> tmp ;    if(tmp < 13)    {        cout << d[tmp] << endl;    }else{        int high =  tmp / 13 ;        int low = tmp % 13 ;        if(low == 0)            cout << h[high-1] << endl;        else            cout << h[high-1] << " " << d[low] << endl;    }}void fun2(string s){    vector<string> vt ;    vt.clear();    istringstream is(s);    string w;    while(is>> w)    {        vt.push_back(w);    }    if((int)vt.size() == 1)    {        for(int i = 0 ; i <= 13 ; i++) // 小于13        {            if(d[i] == vt[0] )            {                cout << i << endl;                return ;            }        }        // 13的倍数 // 这里需要判断        for(int i = 0 ; i < 12 ; i++)        {            if(h[i] == vt[0] )            {                cout << (i+1)*13 << endl;                return ;            }        }    }else{        int n1 , n2 ;        for(int i = 0 ; i < 12 ; i++) // 高位        {            if(h[i] == vt[0] )            {                n1 = i+1 ;                break;            }        }        for(int i = 0 ; i <= 12 ; i++) // 低位        {            if(d[i] == vt[1] )            {                n2 = i ;                break;            }        }        printf("%d\n",n1*13+n2);    }}int main(){    //freopen("in.txt","r",stdin);    int n ;    int i ;    scanf("%d\n",&n);    string st;    while(n -- )    {        getline(cin , st);        if(st[0] >= '0' && st[0] <= '9')        {            fun1(st);        }else{            fun2(st);        }    }    return 0 ;}

第二次做的ac

#include <cstdio>#include <cmath>#include <sstream>#include <iostream>#include <cstring>#include <string>#include <vector>#include <stack>#include <queue>#include <map>#include <algorithm>#include <sstream>using namespace std;const int N = 10005;int main(){  //freopen("in.txt","r",stdin);  string earth[13] = {"", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};  string mars[13] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};  int n;  scanf("%d",&n);  getchar();  string tmp;  while(n -- > 0)  {    getline(cin,tmp);    //char s[10];    //gets(s);    //string tmp(s);    int len = tmp.size();    if(tmp[0] >='0' && tmp[0] <='9')    {      int num;      stringstream ss;      ss << tmp;      ss >> num;      if(num == 0)      {        cout << mars[0] << endl;      }else{        int one = num / 13;        int two = num % 13;        if(two == 0){          cout << mars[one] << endl;        }else{          if(one != 0)            cout << mars[one] << " ";          cout << earth[two] << endl;        }      }    }else{ //       if(len == 3)      {        if(tmp == "tret")          cout << "0" << endl;        else{          for(int i=1;i<13;i++){            if(tmp == earth[i])            {              cout << i << endl;            }          }          for(int i=1;i<13;i++){            if(tmp == mars[i])            {              cout << i * 13 << endl;            }          }        }      }else{        string one = tmp.substr(0,3);        string two = tmp.substr(4,3);        int num = 0;        for(int i=1;i<13;i++)        {          if(one == mars[i])            num += i * 13;        }        for(int i=0;i<13;i++)        {          if(two == earth[i])            num += i;        }        cout << num << endl;      }    }  } // end while  return 0;}

第三次ac

#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <iostream>#include <string>#include <vector>#include <queue>#include <algorithm>#include <sstream>#include <list>#include <stack> #include <map> #include <set> #include <iterator> #include <unordered_map>using namespace std;#define INF 0x7ffffffftypedef long long int LL;const int N = 105;string earth[] = {"zero", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};string mars[] = {"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};int main(){    //freopen("in.txt", "r" ,stdin);    int n;    while( scanf("%d", &n) != EOF)    {        getchar();        string s;        while(n--)        {            getline(cin,s);            if( s[0] >= '0' && s[0] <= '9' )            {                stringstream ss;                ss << s;                int val;                ss >> val;                if(val == 0)                {                    cout << mars[0] << endl;                }else if(val <= 12){                    cout << earth[val] << endl;                }else if( val % 13 == 0){                    cout << mars[val / 13] << endl;                }else{                    int shi = val / 13;                    int ge = val % 13;                    cout << mars[shi] << " " << earth[ge] << endl;                }             }else{                if(s == "tret"){                    cout << "0" << endl;                }else{                    if((int)s.size() == 3)                    {                        for(int i = 1;i<=12;i++)                        {                            if(earth[i] == s){                                cout << i << endl;                            }                        }                        for(int i=1;i<=12;i++)                        {                            if(mars[i] == s)                                cout << 13 *i << endl;                        }                    }else{                        string s1 = s.substr(0,3);                        string s2 = s.substr(4,6);                        int shi;                        int ge;                        for(int i = 1;i<=12;i++)                        {                            if(earth[i] == s2){                                ge = i;                                break;                            }                        }                        for(int i=1;i<=12;i++)                        {                            if(mars[i] == s1)                            {                                shi = i;                                break;                            }                        }                        cout << shi * 13 + ge << endl;                    }                }            }        }    }    return 0;}

总结

1、这题就考察了字符串和进制转换,注意题目输入的范围是[0,169),所以 最多两位(这里可能自己理解失误,导致浪费了好多时间,实际上也没有做出来);2、pat给出[答案错误] 那么一定是自己的结果有问题的,关于发现问题,应该根据题目输入输出 ,改变自己的输入输出(就是一个不断尝试的过程 ,这样可以帮助自己再进一步理解下 真正的输出是怎么样的)3、 确实要好好考虑输入输出
getchar() 吸收空格string str;getline(cin,str)char str[N]gets(str);string s(str);vector<string> words;string tmp;stringstream ss(str);while(ss >> tmp){    words.push_back(tmp);}

0 0
原创粉丝点击