对于PTA exercise 3-3 Write a function expand(s1,s2) 问题的一种解法与思考

题干:

Write a function expand(s1,s2) that expands shorthand notations like a-z in the string s1 into the equivalent complete list abc...xyz in s2. Allow for letters of either case and digits, and be prepared to handle cases like a-b-c and a-z0-9 and -a-z and a--z. Arrange that a leading or trailing - is taken literally.

Please be attention to the SAMPLE INPUT. It may help you a lot. You just need to think about and try to solve these several conditions as the question says.

函数接口定义:

void expand(char [], char []);

裁判测试程序样例:

#include <stdio.h>
#include <assert.h>
const int N = 10001;

void expand(char [], char []);

int main()
{
//    freopen("0.in", "r", stdin);
//    freopen("0.out", "w", stdout);

    char s1[N], s2[N * 30];

    while(scanf("%s", s1) != EOF) {
        expand(s1, s2);
        printf("%s\n", s2);
    }

    return 0;
}

/* 请在这里填写答案 */

SAMPLE INPUT:

a-z
A-A
0-9
c--z
-a-c
a-cf
a-c-f

SAMPLE OUTPUT:

abcdefghijklmnopqrstuvwxyz
A
0123456789
c--z
-abc
abcf
abcdef

我的解法:(萌新一个,写的很烂)

#include <ctype.h>

void expand(char s1[], char s2[])
{
    int i = 0,j = 0;
    int tmp = 0;
    for(i = 0;s1[i];i++)
    {
        if(s1[i] != '-')//非'-'直接输出
            s2[j++] = s1[i];
        else
        {
         if(i)//判断首位是否为‘-‘
         {
             if((islower(s1[i-1]) && islower(s1[i+1]))||
                (isupper(s1[i-1]) && isupper(s1[i+1]))||
                (isdigit(s1[i-1]) && isdigit(s1[i+1])))//判断‘-’两端类型
             {
                 if(s1[i - 1] != s1[i + 1])//判断是否为同一字符
                 {
                     tmp = s1[i + 1] - s1[i - 1];
                     if(tmp >= 0)
                     for(int k = 1;k < tmp;k++)
                         s2[j++] = s1[i - 1] + k;
                     else
                         s2[j++] = '-';
                 }
                 else//相同字符只输出一个
                     i++;
             }
             else//'-'两端字符类型不同直接输出'-'
                 s2[j++] = '-';
         }
            else
                s2[j++] = '-';
        }
    }
        s2[j] = '\0';//结束数组
}

存在的坑:

除了样例给出的输入输出类型外,还需注意a-z0-9A--(-在尾),以及c-a倒叙排列原样打印(坑了我最长时间的一个)。

思考:

由于PTA测试过程中,测试点1始终不通过,而且没有任何提示(提示呢?提示救一下啊),令我苦恼很长时间:

 这时CSDN上一篇文章给了我思路:

如何测出PAT 测试点的测试数据?_where,dyouwannago的博客-CSDN博客_pta测试点怎么看

最终我决定使用以上思路,测试PTA的测试点数据:

//判断点1有3个数据(不同)以及一个‘\0’

void expand(char s1[],char s2[])
{
   if(s1[0] == (s1[2] - 1))
       s2[0] = '\0';
    else
        while(1);
}

通过修改if条件以及善用ctype.h头文件中的islower,isupper,isdigit等函数以及对s1数组值的相等判断,最终确定判断点1有3个不同数据以及一个‘\0,左右两端数据类型相同都是字母或数字,中间数据为‘-’

最终确定判断点一为c-a类型的倒序测试数据。

当然,测试PTA数据的方式不止这一种思路,但是其他的我暂时看不懂,姑且附在这里,方便日后学习:

什么是对数器?对数器的作用是什么?_Bean冷的心的博客-CSDN博客_对数器

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值