已知两个由正整数组成的无序序列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.
-1该怎么控制输入进数组呢?
-1 也是数字 正常进就行 合法性判断你可以自己加一下
#include<stdio.h>
#define Max 1000
int ISsun(int a[], int b[],int i,int j) {
int k,p,q;
for (k = 0; k<i;k++) {
for (p = k, q=0;(a[p]==b[q])&&(q<j);p++, q++);
if (q >= j && k<i)
return 1;
}
return -1;
}
int main(){
int a[Max],b[Max],temp;
int i,j;
for(i =0;i<Max;i++){
scanf("%d",&temp);
if(temp == -1){
break;
}
else a[i]=temp;
}
for(j =0;j<Max;j++){
scanf("%d",&temp);
if(temp == -1){
break;
}
else b[j]=temp;
}
if(ISsun(a,b,i,j)==1){
printf("ListB is the sub sequence of ListA.");
}
return 0;
}
感谢大佬指教