第四章 数据结构
ALDS1_3_A Stack
题目 我就不发了
这是网址
//我抄书的,自己尝试用stack写,然后忘了stack怎么用了…………zzz
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int top,S[105];
void push(int x){
S[++top]=x;
}
int pop(){
top--;
return S[top+1];
}
int ToInt(char *s){
int res=s[0]-'0';
for(int i=1;s[i]!=0;i++){
res = res*10+s[i]-'0';
}
return res;
}
int main(){
int a,b;
top=0;
char s[105];
while(scanf("%s",s)!=EOF){
if(s[0]=='+'){
a=pop();
b=pop();
push(a+b);
}else if(s[0]=='-'){
b=pop();
a=pop();
push(a-b);
}else if(s[0]=='*'){
a=pop();
b=pop();
push(a*b);
}else{
/*atoi()是stdlib.h中的函数,将字符串形式的数字转换成数值类型*/
// push(atoi(s));
/*这是我自己实现的*/
push(ToInt(s));
}
}
printf("%d\n",pop());
return 0;
}
//这是 书上的 stack实现 简介了很多,不是?
#include <iostream>
#include <cstdlib>
#include <stack>
using namespace std;
int main() {
stack<int> S;
int a,b,x;
string s;
while(cin>>s){
if(s[0]=='+'){
a=S.top();S.pop();
b=S.top();S.pop();
S.push(a+b);
}else if(s[0]=='-'){
b=S.top();S.pop();
a=S.top();S.pop();
S.push(a-b);
}else if(s[0]=='*'){
a=S.top();S.pop();
b=S.top();S.pop();
S.push(a*b);
}else{
S.push(atoi(s.c_str()));
}
}
cout<<S.top()<<endl;
return 0;
}
ALDS1_3_B Queue
//书上的 C语言的 ,没有用系统函数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 100005
typedef struct pp{
char name[100];
int t;
} P;
P Q[LEN];
int head,tail,n;
void enqueue(P x){
Q[tail]=x;
tail=(tail+1)%LEN;
}
P dequeue(){
P x=Q[head];
head=(head+1)%LEN;
return x;
}
int min(int a,int b){
return a<b?a:b;
}
int main(){
int elaps=0,c;
int i,q;
P u;
scanf("%d %d",&n,&q);
for(int i=1;i<=n;i++){
scanf("%s",Q[i].name);
scanf("%d",&Q[i].t);
}
head=1;tail=n+1;
while(head!=tail){
u=dequeue();
c=min(q,u.t);
u.t-=c;
elaps +=c;
if(u.t>0){
enqueue(u);
}else{
printf("%s %d\n",u.name,elaps);
}
}
return 0;
}
//C++模版实现方式
#include <iostream>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
int main() {
int n,q,t;
string name;
queue< pair<string,int> > Q;
cin>>n>>q;
for(int i=0;i<n;i++){
cin>>name>>t;
Q.push(make_pair(name,t));
}
pair<string,int> u;
int elaps = 0,a;
while(!Q.empty()){
u=Q.front();Q.pop();
a=min(u.second,q);
u.second-=a;
elaps+=a;
if(u.second>0){
Q.push(u);
}else{
cout<<u.first<<" "<<elaps<<endl;
}
}
return 0;
}
ALDS1_3_C Doubly Linked List
//双向链表 手撸……还是书上的……
/***
* ALDS1_3_C Doubly Linked List
***/
#include <cstdio>
#include <cstdlib>
#include <cstring>
struct Node{
int key;
Node *next,*prev;
};
Node *nil;
Node* listSearch(int key){
Node *cur = nil->next;
while(cur != nil && cur->key != key){
cur=cur->next;
}
return cur;
}
void init(){
nil=(Node *)malloc(sizeof(Node));
nil->next=nil;
nil->prev=nil;
}
void printList(){
Node *cur=nil->next;
int isf=0;
while(1){
if(cur==nil) break;
if(isf > 0) printf(" ");
printf("%d",cur->key);
isf = 1;
cur=cur->next;
}
printf("\n");
}
void deleteNode(Node *t){
if(t==nil) return;
t->prev->next=t->next;
t->next->prev=t->prev;
free(t);
}
void deleteFirst(){
deleteNode(nil->next);
}
void deleteLast(){
deleteNode(nil->prev);
}
void deleteKey(int key){
deleteNode(listSearch(key));
}
void insert(int key){
Node *x=(Node *)malloc(sizeof(Node));
x->key=key;
x->next=nil->next;
nil->next->prev=x;
nil->next=x;
x->prev=nil;
}
int main() {
int key,n,i;
int size=0;
char com[20];
int np=0,nd=0;
scanf("%d",&n);
init();
for(int i=0;i<n;i++){
scanf("%s%d",com,&key);
if(com[0]=='i'){
insert(key);np++;size++;
}else if(com[0]=='d'){
if(strlen(com)>6){
if(com[6]=='F') deleteFirst();
else if(com[6]=='L') deleteLast();
}else{
deleteKey(key);nd++;
}
size++;
}
}
printList();
return 0;
}
ALDS1_3_D Areas on the Cross-Section Diagram
//抄书……
#include <iostream>
#include <stack>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
stack<int> S1;
stack<pair <int,int> > S2;
char ch;
int sum=0;
for(int i=0;cin>>ch;i++){
if(ch=='\\') S1.push(i);
else if(ch=='/' && S1.size()>0){
int j=S1.top();S1.pop();
sum+=i-j;
int a=i-j;
while(S2.size()>0 && S2.top().first > j){
a+=S2.top().second;S2.pop();
}
S2.push(make_pair(j,a));
}
}
vector<int> ans;
while(S2.size()>0){
ans.push_back(S2.top().second);
S2.pop();
}
reverse(ans.begin(),ans.end());
cout<<sum<<endl;
cout<<ans.size();
for(int i=0;i<ans.size();i++){
cout<< " ";
cout<<ans[i];
}
cout<<endl;
return 0;
}