1、给你一个01序列,HDLC协议处理的话,如果出现连续的5个1会补1个0。例如1111110,会变成11111010。
现在给你一个经过HDLC处理后的01序列,你需要找到HDLC处理之前的01序列。
例如给你11111010
你需要输出1111110
解析:本题是基于计算机网络中的HDLC协议进行设计的,以下是我所给出的解法:
#include<iostream>
using namespace std;
void Transform(char *str){//传入字符串str
if(str == NULL)//如果输入的字符串为空,
return ;
int count=0;//用于记录目前1的个数
int i=0;
int k=0;
while(str[i] != '\0'){//遍历字符串
if(count != 5)//使用count来计数1出现的次数
{
str[k++]=str[i];
if(str[i] == '1')
{
count++;
}
i++;
}
else
{
i++;
str[k++]=str[i++];
count=0;//count又重置为0
}
}
str[k++]='\0';
}
int main(){
char s[20];
cin>>s;;
char s1[20];
Transform(s);
cout<<s<<endl;
system("pause");
return 0;
}
2、字符串匹配问题
//从左到右遍历字符串,若遇到左括号则入栈,暂时置标记位为‘ ’
//遇到右括号时,若此时栈为空,则匹配失败,置标记位为?若此时栈不为空,出栈并置标记位为‘ ’
//遍历完后,若栈仍不为空,则置标记位为$,代表左括号匹配失败
#include<stdio.h>
int main()
{
char str[100];//存储读取到的字符串
char s[100];//存储读到的左括号
char ans[100];//存放标记位
while(scanf("%s",str)!=EOF)//输入字符串
{
int i=0;
int top=0;//栈顶指针
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='(')
{
s[top++]=i;
ans[i]=' ';
}
else if(str[i]==')')
{
if(top!=0)//栈不为空时
{
top--;//出栈
ans[i]=' ';
}
else//栈为空时,匹配失败
{
ans[i]='?';
}
}
else//其他字符时
{
ans[i]=' ';
}
}
if(top!=0)//栈不为空时,左括号匹配失败
{
top=top-1;
ans[s[top]]='$';
top--;
}
ans[i]='\0';
printf("%s\n",str);
printf("%s\n",ans);
}
return 0;
}