xdoj-字符串-556,为什么字符不能被正常读入

目录

题目

代码

测试用例

[the input](#the input)

[the correct output](#the correct output)

问题发现过程阐述

如果把line16中的数组大小11换成line17中的10

[case 1](#case 1)

[case 2](#case 2)

[case 3](#case 3)

如果数组开成11

case4

代码分析

问题描述

Question1

Question2


题目

题目:连续数字字符串提取

问题描述

输入一个字符串,将连续的数字字符串放到另一个二维数组中

输入格式

输入一个含连续数字的待提取字符串。

输出格式

输出提取出来的数字字符串,每个连续数字字符串占一行。

样例输入

1234abc7654321[][]79869hewl98765

样例输出

1234

7654321

98765

样例说明

79869不连续,不输出。

评测用例规模与约定

提取出来的连续数字字符串长度不超过20,输入字符串内至多含有10个连续字符串,

时间限制1s,内存限制256KB。(注:1287视为整体,不属于连续数字字符串,不可被看

为是12与87两个连续数字字符串。单独的数字如1,视为连续字符串,需要加以输出)

代码

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

short continuity_judge(char * s,short n);
int main()
{
    char s[211] = "";
    gets(s);

    typedef struct
    {
        char nums[21];
        short total;
    }STRINGS;
    STRINGS strings[11];
    //STRINGS strings[10];
    short index = 0,i = 0;

    //extraction
    for(char *p = s;*p;p++)
    {
        while(*p && isdigit(*p))
        {
            strings[index].nums[i++] = *p++;
        }
        if(i){
            strings[index].total = i;
            strings[index].nums[i] = '\0';
            i = 0;
            index++;
        }
    }

    //judge and output
    for(i = 0;i < index;i++)
    {
        if(continuity_judge(strings[i].nums,strings[i].total)){
            puts(strings[i].nums);
        }
    }
    return 0;
}
short continuity_judge(char * s,short n)
{
    if(n == 1)
        return 1;
    short deta = s[1] - s[0];
    if(deta != 1 && deta != -1 )
        return 0;
    for(int i = 1;i < n-1;i++)
    {
        if(s[i+1] - s[i] != deta)
            return 0;
    }
    return 1;
}

测试用例

the input

42193@2__123nxoq81x01u3&123xq0981d80192j81028!@93121238ydiqw123jdw1cu012

the correct output

2

123

01

3

123

123

1

012

问题发现过程阐述

问题在line16,line17

如果把line16中的数组大小11换成line17中的10

case 1

输入

42193@2__123nxoq81x01u3&123xq0981d80192j81028!@93121238ydiqw123jdw1cu012

输出就变成了

2

123

01

3

123

123

1

0

最后一项少了12

并且通过调试,这一项在strings数组最后一项的nums字符串里确实被存成了0,证明12未被读入

case 2

如果输入少进一个字符

2193@2__123nxoq81x01u3&123xq0981d80192j81028!@93121238ydiqw123jdw1cu012

输出就

2

123

01

3

123

123

1

01

多读进来一个

case 3

再少输入进一个字符

193@2__123nxoq81x01u3&123xq0981d80192j81028!@93121238ydiqw123jdw1cu012

输出

2

123

01

3

123

123

1

012

正常了

如果数组开成11

case4

输入

42193@2__123nxoq81x01u3&123xq0981d80192j81028!@93121238ydiqw123jdw1cu012

输出

2

123

01

3

123

123

1

012

正常了

代码分析

仔细解读代码后发现其实strings[]数组开得有问题,这个输入中有14个数字串,数组开得不够大

问题描述

Question1

为什么数组开到11就正常了,明明有14个数字串,按理来说即使改成11,后面三个应该无法读入才对

Question2

为什么在数组开到10时会出现这种最后几个字符没被读入,改变输入序列长度就能读进去的情况,

这看着反倒像是s[]数组开得不够大,但实际是s[]开得足够大了

相关推荐
呆瑜nuage9 分钟前
数据结构——堆
数据结构
蓝澈112115 分钟前
弗洛伊德(Floyd)算法-各个顶点之间的最短路径问题
java·数据结构·动态规划
zl_dfq18 分钟前
数据结构 之 【堆】(堆的概念及结构、大根堆的实现、向上调整法、向下调整法)(C语言实现)
数据结构
127_127_12721 分钟前
2025 FJCPC 复建 VP
数据结构·图论·模拟·ad-hoc·分治·转化
闪电麦坤9524 分钟前
数据结构:二维数组(2D Arrays)
数据结构·算法
凌肖战37 分钟前
力扣网C语言编程题:快慢指针来解决 “寻找重复数”
c语言·算法·leetcode
埃菲尔铁塔_CV算法1 小时前
基于 TOF 图像高频信息恢复 RGB 图像的原理、应用与实现
人工智能·深度学习·数码相机·算法·目标检测·计算机视觉
NAGNIP2 小时前
一文搞懂FlashAttention怎么提升速度的?
人工智能·算法
Codebee2 小时前
OneCode图生代码技术深度解析:从可视化设计到注解驱动实现的全链路架构
css·人工智能·算法