TZOJ 数据结构期末历年题目
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 数据结构期末历年题目的更多相关文章
- $NOIp$提高组历年题目复习
写在前面 一个简略的\(NOIp\)题高组历年题目复习记录.大部分都有单独写题解,但懒得放\(link\)了\(QwQ\).对于想的时候兜了圈子的题打上\(*\). \(NOIp2018\ [4/6] ...
- PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分)
PTA数据结构与算法题目集(中文) 7-43字符串关键字的散列映射 (25 分) 7-43 字符串关键字的散列映射 (25 分) 给定一系列由大写英文字母组成的字符串关键字和素数P,用移位法定义 ...
- PTA数据结构与算法题目集(中文) 7-42整型关键字的散列映射 (25 分)
PTA数据结构与算法题目集(中文) 7-42整型关键字的散列映射 (25 分) 7-42 整型关键字的散列映射 (25 分) 给定一系列整型关键字和素数P,用除留余数法定义的散列函数将关键字映射 ...
- PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分)
PTA数据结构与算法题目集(中文) 7-41PAT排名汇总 (25 分) 7-41 PAT排名汇总 (25 分) 计算机程序设计能力考试(Programming Ability Test,简称P ...
- PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分)
PTA数据结构与算法题目集(中文) 7-40奥运排行榜 (25 分) 7-40 奥运排行榜 (25 分) 每年奥运会各大媒体都会公布一个排行榜,但是细心的读者发现,不同国家的排行榜略有不同.比如 ...
- PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分)
PTA数据结构与算法题目集(中文) 7-39魔法优惠券 (25 分) 7-39 魔法优惠券 (25 分) 在火星上有个魔法商店,提供魔法优惠券.每个优惠劵上印有一个整数面值K,表示若你在购买某商 ...
- PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分)
PTA数据结构与算法题目集(中文) 7-38寻找大富翁 (25 分) 7-38 寻找大富翁 (25 分) 胡润研究院的调查显示,截至2017年底,中国个人资产超过1亿元的高净值人群达15万人.假 ...
- PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分)
PTA数据结构与算法题目集(中文) 7-37 模拟EXCEL排序 (25 分) 7-37 模拟EXCEL排序 (25 分) Excel可以对一组纪录按任意指定列排序.现请编写程序实现类似功能. ...
- PTA数据结构与算法题目集(中文) 7-36 社交网络图中结点的“重要性”计算 (30 分)
PTA数据结构与算法题目集(中文) 7-36 社交网络图中结点的“重要性”计算 (30 分) 7-36 社交网络图中结点的“重要性”计算 (30 分) 在社交网络中,个人或单位(结点)之间通过某 ...
随机推荐
- day14-函数
1.定义函数 一个函数就是封闭一个功能def 函数名(): 函数代码注意:函数名不要用默认的关键字.否则会将默认关键字函数覆盖掉. 命名规则与变量相同,使用字母.数字.下划线组成,不能以数字开关 2. ...
- DevExpress控件TExtLookupComboBox实现多列模糊匹配输入的方法
本方案不需要修改控件源码,是完美解决支持多列模糊匹配快速输入的最佳方案!! 1.把列的Properties属性设置为ExtLookupComboBox. Properties.Incrementa ...
- 尚硅谷springboot学习12-profile
一个项目对应不同的环境可以会有不同的配置,如开发,测试,生产环境使用不同的端口,这时可以设置profile变换不同的环境 通过spring.profiles.active切换环境 1.多Profile ...
- 卸载数据盘、更改Inodes
更改inodes 会格式化数据库,记得先备份 1.fuser -m -v /dev/vdb查看哪些进程正在占用数据库 2.然后kill 掉进程 3.umount /data1/ 4.mkfs.ext ...
- python模块sys
#!/bin/env python #-*- encoding=utf8 -*- import sys if __name__=="__main__": # 在解释器启动后, ar ...
- Python中fileinput模块使用方法
fileinput模块提供处理一个或多个文本文件的功能,可以通过使用for循环来读取一个或多个文本文件的所有行.python2.7文档关于fileinput介绍:fileinput fileinp ...
- python实现排序算法一:快速排序
##快速排序算法##基本思想:分治法,将数组分为大于和小于该值的两部分数据,然后在两部分数据中进行递归排序,直到只有一个数据结束## step1: 取数组第一个元素为key值,设置两个变量,i = 0 ...
- js版RSA算法
// RSA, a suite of routines for performing RSA public-key computations in// JavaScript.//// Requires ...
- Jupyter notebook 文件路径
Jupyter notebook 文件路径 1. 默认工作路径:C:\Users\think 2. 修改工作路径: C:\Users\think\.jupyter路径下,无配置文件 打开命令提示符:( ...
- WilliamChart各种图表效果实现大全《IT蓝豹》
WilliamChart各种图表效果实现大全,有水平线条表格,有柱状表格等.由LineFragment,BarFragment,StackedFragment,SandboxFragment几个fra ...