user-0 提问: pat火星数字 测试点1和4已经对0和13的倍数处理里还是通不过

回复:
user: user-0
content: 找到我的原因了 有个for里的数组个数是13，还有就是我不知道strcmp第一个参数的指针可以选位置

user: user-1
content: #include<stdio.h> #include<stdlib.h> #include<string.h> #include <ctype.h>    int find_index(const char **str, int len, const char *src) {     int i;      for (i = 0; i  < len; i++)         if (str[i][0] != '\0' && strstr(src, str[i]))                 break;     if (i >= len)         return -1;      return i; }  int main() {     const char *ones[13] = {         "tret", "jan", "feb", "mar", "apr",         "may", "jun", "jly", "aug", "sep",         "oct", "nov", "dec"     };     const char *tens[13] = {         "", "tam", "hel", "maa", "huh",         "tou", "kes", "hei", "elo", "syy",         "lok","mer","jou"     };     char str[32], *pstr;     int n, i = 0, num, quot, ret;       scanf("%d",&n);     getchar();    //for '\n'      while (i < n) {         fgets(str, 32, stdin);          if (isdigit(str[0])) {             num = atoi(str);             //printf("num = %d\n", num);             if (num >= 169 || num < 0) {                 fprintf(stderr, "Input error!\n");                 continue;             }             if ((quot = num / 13))                 printf("%s ", tens[quot]);             printf("%s\n", ones[num % 13]);             continue;         }         //puts(str);         num = 0;         pstr = str;         ret = find_index(tens, 13, pstr);         if (ret > 0) {             num = ret * 13;             pstr = strstr(str, " ");         }         if (pstr != NULL) {             ret = find_index(ones, 13, pstr);             num += ret;         }         printf("%d\n", num);         i++;     }      return 0; } 用这个代码吧，上面的代码还有点bug。

user: user-1
content: #include<stdio.h> #include<stdlib.h> #include<string.h> #include <ctype.h>    int find_index(const char **str, int len, const char *src) {     int i;      for (i = 0; i  < len; i++)         if (str[i][0] != '\0' && strstr(src, str[i]))                 break;     if (i >= len)         return -1;      return i; }  int main() {     const char *c[13] = { "tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};     const char *d[13] = {"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou" };     char str[32], *pstr;     int n, i = 0, num, remainder, quot, ret;       scanf("%d",&n);     getchar();    //for '\n'      while (i < n) {         fgets(str, 32, stdin);          if (isdigit(str[0])) {             num = atoi(str);             printf("num = %d\n", num);             if (num >= 169 || num < 0) {                 fprintf(stderr, "Input error!\n");                 continue;             }             if ((quot = num / 13))                 printf("%s ", d[quot]);             printf("%s\n", c[num % 13]);             continue;         }         //puts(str);         num = 0;         pstr = str;         ret = find_index(d, 13, pstr);         if (ret > 0)             num = ret * 13;         //printf("ret1 = %d\n", ret);         pstr = strstr(str, " ");         if (pstr != NULL) {             ret = find_index(c, 13, pstr+1);             //printf("ret2 = %d\n", ret);             num += ret;         }         printf("%d\n", num);         i++;     }  #if 0     int n,t1,t2,len, k1, k2;     const char *c[13] = { "tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};     const char *d[13] = {"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou" };      int k = 0,kk=0,sum = 0;;     char a[32],tmp[32];     scanf("%d",&n);     getchar();     for (int i = 0; i < n; i++)     {         fgets(a,10,stdin);         len = strlen(a);          if (len == 2 && a[0] == '0') printf("tret\n");         else if (a[0] > '0' &&a[0] <= '9')         {             int t = atoi(a);             if (t <= 12)printf("%s\n",c[t]);             else if (t % 13 == 0) printf("%s\n",d[t/13]);             else printf("%s %s\n",d[t/13], c[t % 13]);         }         else         {             if (len == 4 || len == 9) printf("0");             else if (len < 4)             {                 a[3] = '\0';                 for (int i = 0; i < 13; i++)                 {                     if (strcmp(a, c[i]) == 0)                         printf("%d\n",i);                 }                 for (int i = 0; i < 13; i++)                 {                     if (strcmp(a, d[i]) == 0)                         printf("%d\n", i*13);                 }             }             else             {                 for (k1 = 0; k1 < 13; k1++)                 //计算高三位                 {                     if (strncmp(a, d[k1], 3) == 0)                     {                         sum = k1 * 13;                     }                 }                 tmp[3] = '\0';                 for ( k2 = 0; k2 < 12; k2++)                     //计算低三位                 {                     strncpy(tmp, a + 4, 3);                     if (strcmp(c[k2], tmp) == 0&&c[k2]!="tret")                     {                         sum = sum + k2;                     }                 }                 printf("%d\n", sum);                 sum = 0;             }         }     } while (1); #endif     return 0; } 供参考～