7-42 实验7_7_连续子序列 (100 分)

已知两个由正整数组成的无序序列A、B,每个序列的元素个数未知,但至少有一个元素。你的任务是判断序列B是否是序列A的连续子序列。假设B是“1 9 2 4 18”,A是“33 64 1 9 2 4 18 7”,B是A的连续子序列;假设B是“1 9 2 4 18”,A是“33 1 9 64 2 4 18 7”,B不是A的连续子序列。

输入格式:

依次输入两个乱序的正整数序列A、B,序列中元素个数未知,但每个序列至少有一个元素,并以输入“-1”结束(-1不算序列中元素),每个序列占一行。两个数列的长度均不超过1000。

输入保证合法,且所有整数均可用int存储。

输出格式:

如果序列B是序列A的连续子序列,则输出“ListB is the sub sequence of ListA.”,否则输出“ListB is not the sub sequence of ListA.”。

输入样例:

5 4 3 2 1 -1
3 2 1 -1

输出样例:

ListB is the sub sequence of ListA.

代码如下:

#include<stdio.h>
int main()
{
    int arr1[1000] = { 0 };
    int arr2[1000] = { 0 };
    int i, n;
//输入
    for (i = 0;; i++)
    {
        scanf("%d", &n);
        if (n != -1)
            arr1[i] = n;
        else
            break;
    }
    int sz1 = i;

    for (i = 0;; i++)
    {
        scanf("%d", &n);
        if (n != -1)
            arr2[i] = n;
        else
            break;
    }
    int sz2 = i;
    int s = 0,f = 0,x = 0;
//操作
    if (sz2 > sz1)
    {
        printf("ListB is not the sub sequence of ListA.\n");
        return 0;
    }
    
       for (i = 0; i < sz1; i++)
        {
            if (sz1 - i < sz2)
            {
                printf("ListB is not the sub sequence of ListA.\n");
                return 0;
            }
            if (arr1[i] == arr2[0])
            {

                for (s = 0, f = i; s < sz2; f++, s++)
                {
                    if (arr1[f] != arr2[s])
                        break;
                }
                if (s == sz2)
                {
                    printf("ListB is the sub sequence of ListA.\n");
                    return 0;
                }
            }
        }
        printf("ListB is not the sub sequence of ListA.\n");
        return 0;
}

 麻烦

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值