# 数据结构、算法及线性表的总结

## 疑难问题及解决方案

### 7-1 jmu-ds-栈与队列-stack、queue与string小综合 (5分)

#include<string>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
int main() {
string line;
char x;
int i, j;
stack<char> s;
queue<char> q;
cin >> line;
cin >> x;
for (i = 0;line[i] != '\0';i++) {
s.push(line[i]);
}
cout << s.size() << " ";
cout << s.top() << endl;
while (!s.empty()) {
cout << s.top();
if (s.top() != x) {
q.push(s.top());
}
s.pop();
}
cout << "\n" << q.size() << " " << q.front() << " " << q.back() << "\n";
while (!q.empty()) {
cout << q.front();
q.pop();
}
return 0;
}


### 7-7 银行业务队列简单模拟 (25分)

#include<iostream>
#include<queue>
using namespace std;
int main(){
queue<int> q1,q2;
int n;
cin>>n;
while(n--){
int m;
cin>>m;
if(m%2)q1.push(m);
else q2.push(m);
}
while(!q1.empty()){
int cnt=2,i=0;
while(cnt--&&!q1.empty()){
if(i++)cout<<" ";
cout<<q1.front();
q1.pop();
}
if(!q2.empty()){
cout<<" "<<q2.front()<<" ";
q2.pop();
}
}
int i=0;
while(!q2.empty()){
if(i++)cout<<" ";
cout<<q2.front();
q2.pop();
}
return 0;
}


### 7-1 jmu-ds-最长数字序列 (25分)

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int main(){
string s;
getline(cin,s);
int maxindex = 0,maxlen = 0;
int len = 0;
if(s.length()==0){
cout << "NULL";
return 0;
}
for(int i = 0;i < s.length();i++){
if(s[i]>='0'&&s[i]<='9'){
len++;
if(s[i+1]=='\0'){
maxlen = len;
maxindex = i-len+1;
}
}
else{
if(len >= maxlen){
maxlen = len;
maxindex = i-len;
}
len = 0;
}
}
cout<<maxindex<<" "<<maxlen;
return 0;
}


posted @ 2020-03-28 18:04  hhs(黄鸿森)  阅读(298)  评论(0编辑  收藏  举报