A.数据结构练习题――线性表操作

线性表的基本操作

1.在某个位置p插入val,复杂度O(p)

2.在某个位置p删除val,复杂度O(p)

3.查找某个位置p的值,复杂度O(p)

4.清除链表,复杂度O(链表长)

 #include<iostream>
#include<string>
#include<list>
using namespace std;
int main()
{
int n,p,x;
string s;
list<int>li;
list<int>::iterator it;
while(cin>>s)
{
if(s=="exit")break;
else if(s=="clear")li.clear();
else if(s=="insert")
{
cin>>n;
for(int i=;i<n;i++)
{
cin>>p>>x;
it=li.begin();
while(--p)it++;
li.insert(it,x);
}
}
else
{
cin>>p;
it=li.begin();
while(--p)it++;
cout<<*it<<endl;
if(s=="delete")li.erase(it);
}
}
return ;
}

 B.数据结构练习题――合并表

线性表的基本操作

先把第一排每个数放入链表,然后把b中的每个数插入链表,复杂度O(n+m)

 #include<iostream>
#include<list>
using namespace std;
int main()
{
int t,n,m;
list<int>li;
list<int>::iterator it;
cin>>t;
while(t--)
{
li.clear();
cin>>n;
for(int i=,a;i<n;i++)cin>>a,li.push_back(a);
cin>>m;
it=li.begin();
for(int i=,b;i<m;i++)
{
cin>>b;
while(it!=li.end()&&*it<=b)it++;
li.insert(it,b);
}
cout<<n+m;
for(auto X:li)
cout<<" "<<X;
cout<<endl;
}
return ;
}

C.数据结构―线性表插入删

线性表的基本操作

1.在第pos个位置前插入连续m个数,由于是插入在前面,所以找到第pos个位置的地址it,然后不断插入,复杂度O(m)

2.删除第pos个位置开始连续m个数,若到表尾则删除到结尾,这里erase第pos个位置的地址it后,需要讲,复杂度O(m)

 #include<stdio.h>
#include<list>
using namespace std;
int main()
{
int t,n,p,pos,m,x;
char s[];
scanf("%d",&t);
while(t--)
{
list<int>l;
list<int>::iterator it;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&x),l.push_back(x);
scanf("%d",&p);
for(int i=;i<p;i++)
{
scanf("%s",s);
if(s[]=='i')
{
scanf("%d%d",&pos,&m);
it=l.begin();
while(--pos>)it++;//这里>0考虑到pos=0的情况
while(m--)
scanf("%d",&x),l.insert(it,x);
}
if(s[]=='d')
{
scanf("%d%d",&pos,&m);
it=l.begin();
while(--pos)it++;
while(m--&&it!=l.end())l.erase(it++);
}
}
int k=;
for(it=l.begin();it!=l.end();it++)
{
if(k++)printf(" ");
printf("%d",*it);
}
printf("\n");
}
return ;
}

E.数据结构练习题――栈

栈的基本操作

1.把元素val压入栈,复杂度O(1)

2.弹出栈顶元素,若栈为空输出None,否则输出栈顶元素val,复杂度O(1)

3.输出栈顶元素,若栈为空输出None,否则输出栈顶元素val,复杂度O(1)

4.清空栈,复杂度O(栈中元素数)

 #include<iostream>
#include<string>
#include<stack>
using namespace std;
int main()
{
int a;
string s;
stack<int>st;
while(cin>>s)
{
if(s=="exit")break;
if(s=="push")cin>>a,st.push(a);
if(s=="top")
{
if(st.empty())cout<<"None"<<endl;
else cout<<st.top()<<endl;
}
if(s=="pop")
{
if(st.empty())cout<<"None"<<endl;
else cout<<st.top()<<endl,st.pop();
}
if(s=="clear")
while(!st.empty())st.pop();
}
return ;
}

F.数据结构练习题----队列的基本操作

队列基本操作

1.值val入队,复杂度O(1)

2.出队,若队列为空,输出None,否则输出队头元素val,复杂度O(1)

3.输出队列中所有元素,若队列为空,输出None,否则输出所有元素,需要借助一个临时队列p用于存输出前的队列,复杂度O(原队列长)

 #include<iostream>
#include<queue>
#include<string>
using namespace std;
int main()
{
int a;
string s;
queue<int>q,p;
while(cin>>s)
{
if(s=="exit")break;
if(s=="enq")cin>>a,q.push(a);
if(s=="printq")
{
if(q.empty()){cout<<"None"<<endl;continue;}
int k=;
p=q;
while(!q.empty())
{
if(k++)cout<<" ";
cout<<q.front(),q.pop();
}
cout<<endl;
q=p;
}
if(s=="deq")
{
if(q.empty())cout<<"None"<<endl;
else cout<<q.front()<<endl,q.pop();
}
}
return ;
}

 G.简单排序

给你n个数让你排个序,复杂度O(nlogn)

 #include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t,n,a[];
cin>>t;
while(t--)
{
cin>>n;
for(int i=;i<n;i++)cin>>a[i];
sort(a,a+n);
for(int i=;i<n;i++)cout<<a[i]<<(i==n-?"\n":" ");
}
return ;
}

H.快速排序

给你n个数让你排序,注意用C++提交,复杂度O(nlogn)

 #include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int t,n,a[];
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)scanf("%d",&a[i]);
sort(a,a+n);
for(int i=;i<n;i++)printf("%d%c",a[i],i==n-?'\n':' ');
}
return ;
}

 I.二分查找

二分查找,注意C++提交,复杂度O(logn)

lowber_bound(a,a+n,x);

函数的作用是返回>=x的第一个位置,若没有返回最后一个位置

第一个为二分开始的位置,第二个为二分结束的位置,注意左闭右开,第三个为查找的值val

 #include<stdio.h>
#include<algorithm>
using namespace std;
int a[];
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<n;i++)scanf("%d",&a[i]);
scanf("%d",&m);
int pos=lower_bound(a,a+n,m)-a;
if(pos==n||a[pos]!=m)printf("None\n");
else printf("%d\n",pos+);
}
return ;
}

J.图的遍历

图的深度优先和图的广度优先,需要有一个基本的认识,复杂度O(2n)

 #include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int n,m,vis[],G[][];
void dfs(int u)
{
if(vis[u])return;
printf("%d ",u);
vis[u]=;
for(int i=;i<=n;i++)if(!vis[i]&&G[u][i])dfs(i);
}
void bfs(int u)
{
queue<int>q;
q.push(u);
while(!q.empty())
{
u=q.front();q.pop();
if(vis[u])continue;
printf("%d ",u);
vis[u]=;
for(int i=;i<=n;i++)if(!vis[i]&&G[u][i])q.push(i);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(G,,sizeof(G));
for(int i=,u,v;i<m;i++)
{
scanf("%d%d",&u,&v);
G[u][v]=G[v][u]=;
}
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)if(!vis[i])dfs(i);
printf("\n");
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)if(!vis[i])bfs(i);
printf("\n");
}
return ;
}

K.二叉树遍历

这题给你的是层序,而且只有1000个节点,我们知道2^10-1=1024,所以最多10层,我们可以用数组建树

然后你需要知道如何先序中序后序,复杂度O(3*k),k为节点数

 #include<stdio.h>
int a[],k;
void P(int u,int flag)
{
if(u>k||a[u]==)return;
if(flag==)printf(" %d",a[u]);//先序
P(u*,flag);//左儿子
if(flag==)printf(" %d",a[u]);//中序
P(u*+,flag);//右儿子
if(flag==)printf(" %d",a[u]);//后序
}
int main()
{
int t,x;
scanf("%d",&t);
while(t--)
{
k=;
while(scanf("%d",&x),x!=-)a[k++]=x;
for(int i=;i<=;i++)
P(,i),printf("\n");
}
return ;
}

TZOJ 数据结构期末历年题目的更多相关文章

  1. $NOIp$提高组历年题目复习

    写在前面 一个简略的\(NOIp\)题高组历年题目复习记录.大部分都有单独写题解,但懒得放\(link\)了\(QwQ\).对于想的时候兜了圈子的题打上\(*\). \(NOIp2018\ [4/6] ...

  2. PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分)

    PTA数据结构与算法题目集(中文)  7-43字符串关键字的散列映射 (25 分) 7-43 字符串关键字的散列映射 (25 分)   给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义 ...

  3. PTA数据结构与算法题目集(中文) 7-42整型关键字的散列映射 (25 分)

    PTA数据结构与算法题目集(中文)  7-42整型关键字的散列映射 (25 分) 7-42 整型关键字的散列映射 (25 分)   给定一系列整型关键字和素数P,用除留余数法定义的散列函数将关键字映射 ...

  4. PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分)

    PTA数据结构与算法题目集(中文)  7-41PAT排名汇总 (25 分) 7-41 PAT排名汇总 (25 分)   计算机程序设计能力考试(Programming Ability Test,简称P ...

  5. PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分)

    PTA数据结构与算法题目集(中文)  7-40奥运排行榜 (25 分) 7-40 奥运排行榜 (25 分)   每年奥运会各大媒体都会公布一个排行榜,但是细心的读者发现,不同国家的排行榜略有不同.比如 ...

  6. PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分)

    PTA数据结构与算法题目集(中文)  7-39魔法优惠券 (25 分) 7-39 魔法优惠券 (25 分)   在火星上有个魔法商店,提供魔法优惠券.每个优惠劵上印有一个整数面值K,表示若你在购买某商 ...

  7. PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分)

    PTA数据结构与算法题目集(中文)  7-38寻找大富翁 (25 分) 7-38 寻找大富翁 (25 分)   胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假 ...

  8. PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)

    PTA数据结构与算法题目集(中文)  7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分)   Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...

  9. PTA数据结构与算法题目集(中文) 7-36 社交网络图中结点的“重要性”计算 (30 分)

    PTA数据结构与算法题目集(中文)  7-36 社交网络图中结点的“重要性”计算 (30 分) 7-36 社交网络图中结点的“重要性”计算 (30 分)   在社交网络中,个人或单位(结点)之间通过某 ...

随机推荐

  1. Leetcode 题解 First Missing Positive

    Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0]  ...

  2. Javascript 对象的创建和属性的判定

    1. 创建对象的方法: 直接使用new 对Object对象进行操作,即对Object 对象进行实例化 <!DOCTYPE html> <html lang="en" ...

  3. HTTPS好文推荐

    认真看完这几篇文章,HTTPS相关内容应该就能大概了解了. 1.https(ssl)协议以及wireshark抓包分析与解密 2.数字证书原理 3.也许,这样理解HTTPS更容易 4.SSL/TLS原 ...

  4. 使用py2exe转换python文件为可执行程序

    py2exe可以将python脚本转换成在Windows上的可独立执行.exe程序的工具.可以让Python脚本在没有安装python工具的Windows系统上运行,方便脚本共享. 操作环境 pyth ...

  5. Open Tools API :IDE Main Menus

    http://www.davidghoyle.co.uk/WordPress/?p=777 http://www.davidghoyle.co.uk/WordPress/?page_id=1110 h ...

  6. 在前台页面写java代码,导入java的包

  7. tensorflow 之tensorboard 对比不同超参数训练结果

    我们通常使用tensorboard 统计我们的accurate ,loss等,并绘制曲线,通常是使用一次训练中的, 但是,机器学习中通常要对比不同的 ‘超参数’给模型训练和预测能力的不同这时候如何整合 ...

  8. js基础-函数基础

    js 先对函数进行解析 然后在执行函数 定义一个函数 实现求两个数的乘 function mult(a,b){ return a*b; } mult(1,3) 计算1 - n 的和 封装成函数 fun ...

  9. webpack 学习

    ·1.https://www.2cto.com/kf/201711/696035.html 2. http://blog.csdn.net/x550392236/article/details/784 ...

  10. echarts 树图

    1 事件:事件绑定,事件命名统一挂载到require('echarts/config').EVENT(非模块化为echarts.config.EVENT)命名空间下,建议使用此命名空间作为事件名引用, ...