PAT甲级题解-1100. Mars Numbers (20)-字符串处理

  没什么好说的,注意字符串的处理,以及当数字是13的倍数时,只需高位叫法的单词。比如26,是“hel”,而不是“hel tret”。

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
#include <string>
#include <string.h>
using namespace std;
char mars1[13][20]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char mars2[13][20]={"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
map<char*,int>maps;

int main()
{
    int n;
    char str[20];
    int decade,digit;
    scanf("%d",&n);
    getchar();
    for(int i=0;i<n;i++){
        gets(str);
        if(‘0‘<=str[0]&&str[0]<=‘9‘){
            int num=atoi(str);
            if(num/13 && num%13==0){
                //注意整除的情况。。。
                printf("%s\n",mars2[num/13]);
            }
            else if(num/13){
                decade=num/13;
                digit=num%13;
                printf("%s %s\n",mars2[decade],mars1[digit]);
            }
            else{
                printf("%s\n",mars1[num]);
            }
        }
        else{
            int len=strlen(str);
            //如果是一个字符串的话,加上\0顶多4位。不过要注意也要比较下十位数
            if(len<5){
                digit=-1;
                for(int i=0;i<13;i++){
                    if(strcmp(str,mars1[i])==0){
                        digit=i;
                        break;
                    }
                }
                if(digit==-1){
                    for(int i=1;i<13;i++){
                        if(strcmp(str,mars2[i])==0){
                            decade=i;
                            break;
                        }
                    }
                    printf("%d\n",decade*13);
                }
                else
                    printf("%d\n",digit);
            }
            else{
                char tmp[20];
                int i;
                for(i=0;i<len && str[i]!=‘ ‘;i++){
                    tmp[i]=str[i];
                }
                tmp[i]=‘\0‘;
                //十位数
                for(int k=1;k<13;k++){
                    if(strcmp(tmp,mars2[k])==0){
                        decade=k;
                        break;
                    }
                }
                i++;
                int p=0;
                for(;i<len;i++,p++)
                    tmp[p]=str[i];
                tmp[p]=‘\0‘;
                //个位数
                for(int k=0;k<13;k++){
                    if(strcmp(tmp,mars1[k])==0){
                        digit=k;
                        break;
                    }
                }
                printf("%d\n",decade*13+digit);

            }
        }
    }
    return 0;
}

时间: 2024-10-10 20:12:27

PAT甲级题解-1100. Mars Numbers (20)-字符串处理的相关文章

PAT (Advanced Level) 1100. Mars Numbers (20)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; char a[20][6]={ "tret","jan"

1100. Mars Numbers (20)【字符串处理】——PAT (Advanced Level) Practise

题目信息 1100. Mars Numbers (20) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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,

1100. Mars Numbers (20)

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 dig

PAT甲题题解-1108. Finding Average (20)-字符串处理

求给出数的平均数,当然有些是不符合格式的,要输出该数不是合法的. 这里我写了函数来判断是否符合题目要求的数字,有点麻烦. #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> using namespace std; const int maxn=105; bool islegal(char*str){ int len=strlen(str); int p

1100 Mars Numbers(20 分)

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 dig

1100 Mars Numbers (20 分)

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 dig

PAT甲题题解-1073. Scientific Notation (20)-字符串处理

题意:给出科学计数法的格式的数字A,要求输出普通数字表示法,所有有效位都被保留,包括末尾的0. 分两种情况,一种E+,一种E-.具体情况具体分析╮(╯_╰)╭ #include <iostream> #include <cstdio> #include <algorithm> #include <string.h> #include <cmath> #define POSITIVE 1 #define NEGATIVE 2 using names

PAT甲级 1001. A+B Format (20)

题目原文: Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). Input Each input file contains one test case. Each case contains a pair of i

PAT 甲级 1001 A+B Format (20)(20 分)

1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). Input Each input file contains one test case. Each case