oj数据结构系列.docx
- 文档编号:7319804
- 上传时间:2023-01-22
- 格式:DOCX
- 页数:89
- 大小:42.62KB
oj数据结构系列.docx
《oj数据结构系列.docx》由会员分享,可在线阅读,更多相关《oj数据结构系列.docx(89页珍藏版)》请在冰豆网上搜索。
oj数据结构系列
栈和队列
F-传说中的数据结构
TimeLimit:
1000MSMemoryLimit:
65536KB64bitIOFormat:
%lld&%llu
SubmitStatusPracticeSDUTOJ2556
Description
在大学里学习了一个学期了,大家大都对所学的专业有了基本的了解。
许多同学也已经知道了到大二要开一门课叫做《数据结构》,那么今天给你们提前讲一下一个最简单的数据结构:
栈。
栈的基本操作有3种:
push,pop,top。
例如,给你一个数列:
1234
push:
向栈中加入一个数,比如push5,数列就变成12345。
pop:
从栈中删除最后面的数,比如pop,数列就变成123。
(数列变化,但是不输出。
如果栈是空的,即不能pop操作,那就输出error,但是接下来的操作还是要继续的)。
top:
找出栈最后面的数,比如top,你就要输出4。
(如果栈中没有数的话,即不能top操作,那就输出empty)。
然后,你们可以看出来了吧,其实栈就是一个先进后出(越先进去的元素越后面出来)的数据结构,很简单吧,下面要检验下你们的学习效果了。
Input
输入包含多组测试数据.
每组数据的第一行为一个整数T(1<=T<=1000),接下来T行为对栈的操作。
Output
如果操作是top,那么输出最后面的数,如果栈中没有数的话,那就输出“empty”(不含引号)。
如果操作是pop且栈是空的,那么输出“error”(不含引号)。
在每组测试数据的最后多加一次换行。
SampleInput
8
push1
push2
push3
push4
top
pop
top
pop
3
push1
pop
top
SampleOutput
4
3
empty
#include
#include
intmain()
{
intn,i,a;
intstack[1000];
chars[1000];
while(scanf("%d",&n)!
=EOF)
{
inttop=0;
for(i=0;i { scanf("%s",s); if(strcmp(s,"push")==0) { scanf("%d",&a); stack[top++]=a; } else if(strcmp(s,"pop")==0) { if(top==0) { printf("error\n"); } else stack[top--]; } else if(strcmp(s,"top")==0) { if(top==0) { printf("empty\n"); } else printf("%d\n",stack[top-1]); } } printf("\n"); } return0; } B-数据结构实验之栈二: 一般算术表达式转换成后缀式 TimeLimit: 1000MSMemoryLimit: 65536KB64bitIOFormat: %lld&%llu SubmitStatusPracticeSDUTOJ2132 Description 对于一个基于二元运算符的算术表达式,转换为对应的后缀式,并输出之。 Input 输入一个算术表达式,以‘#’字符作为结束标志。 Output 输出该表达式转换所得到的后缀式。 SampleInput a*b+(c-d/e)*f# SampleOutput ab*cde/-f*+ #include intchange(charc) { if(c=='+'||c=='-')return1; if(c=='*'||c=='/')return2; #include intchange(charc) { if(c=='+'||c=='-')return1; if(c=='*'||c=='/')return2; if(c=='(')return3; if(c==')')return4; return0; } intmain() { inttop=0; charc=0,stack[200]; while(scanf("%c",&c),c! ='#') { if(c>='a'&&c<='z') { printf("%c",c); } else { if(top==0) { top++; stack[top]=c; } else if(change(c)>=change(stack[top])) { if(change(c)==4) { while(stack[top]! ='(') { printf("%c",stack[top--]); } top--; } else { top++; stack[top]=c; } } else { if(stack[top]! ='(') { printf("%c",stack[top]); stack[top]=c; } else { top++; stack[top]=c; } } } } while(top! =0) { printf("%c",stack[top]); top--; } printf("\n"); return0; } C-数据结构实验之栈三: 后缀式求值 TimeLimit: 1000MSMemoryLimit: 65536KB64bitIOFormat: %lld&%llu SubmitStatusPracticeSDUTOJ2133 Description 对于一个基于二元运算符的后缀表示式(基本操作数都是一位正整数),求其代表的算术表达式的值。 Input 输入一个算术表达式的后缀式字符串,以‘#’作为结束标志。 Output 求该后缀式所对应的算术表达式的值,并输出之。 SampleInput 59*684/-3*+# SampleOutput 57 Hint 基本操作数都是一位正整数! #include #include intstack[100]; intmain() { inttop=-1; charch; while(scanf("%c",&ch),ch! ='#') { if(ch>='0'&&ch<='9') stack[++top]=ch-'0'; else { inta=stack[top--]; intb=stack[top]; if(ch=='+') stack[top]=b+a; elseif(ch=='-') stack[top]=b-a; elseif(ch=='*') stack[top]=b*a; elseif(ch=='/') stack[top]=b/a; } } printf("%d\n",stack[0]); return0; } D-数据结构实验之栈四: 括号匹配 TimeLimit: 1000MSMemoryLimit: 65536KB64bitIOFormat: %lld&%llu SubmitStatusPracticeSDUTOJ2134 Description 给你一串字符,不超过50个字符,可能包括括号、数字、字母、标点符号、空格,你的任务是检查这一串字符中的(),[],{}是否匹配。 Input 输入数据有多组,处理到文件结束。 Output 如果匹配就输出“yes”,不匹配输出“no” SampleInput sin(20+10) {[}] SampleOutput yes no Hint #include structkuohao { charst[60]; inttop; }s; intpeidui(chars1[]) { inti=0; s.top=0; while(s1[i]! ='\0') { switch(s1[i]) { case'{': case'[': case'(': s.st[s.top]=s1[i];s.top++;break; case'}': if(s.top>0&&s.st[s.top-1]=='{')s.top--; elsereturn0;break; case']': if(s.top>0&&s.st[s.top-1]=='[')s.top--; elsereturn0;break; case')': if(s.top>0&&s.st[s.top-1]=='(')s.top--; elsereturn0;break; }i++; } if(s.top==0)return1; elsereturn0; } intmain() { charstr[55]; while(scanf("%s",str)! =EOF) { if(peidui(str))printf("yes\n"); elseprintf("no\n"); }return0; } E-数据结构实验之队列一: 排队买饭 TimeLimit: 1000MSMemoryLimit: 65536KB64bitIOFormat: %lld&%llu SubmitStatusPracticeSDUTOJ2135 Description 中午买饭的人特多,食堂真是太拥挤了,买个饭费劲,理工大的小孩还是很聪明的,直接奔政通超市,哈哈,确实,政通超市里面也卖饭,有好几种菜,做的比食堂好吃多了,价格也不比食堂贵,并且买菜就送豆浆,吸引了不少童鞋。 所以有时吧,人还是很多的,排队是免不了的,悲剧的是超市只有两个收银窗口。 问题是这样的: 开始有两队人在排队,现在咱们只研究第一队,现在我们给每个人一个编号,保证编号各不相同,排在前面的人买完饭就走了,有些人挑完饭就排在后面等待付款,还有一些人比较聪明,看到另一个队人比较少,直接离开这个队到另一个队去了。 我要问的是队的总人数和某个位置上人的编号。 Input 首先输入一个整数m(m<10000),代表当前有m个人,第二行输入m个数,代表每个人的编号,第三行输入一个整数n(n<10000),代表队列变动和询问一共n次,以后n行,JOINX表示编号为X(保证与以前的编号不同)的人加入;LEAVEY表示第Y(Y小于当前队列长度)个位置上的人离队;ASKZ(Z小于当前队列长度)表示询问第Z个位置上的人的编号;FINISHD表示有D个人买完饭离开了;LENGTH表示询问队列的长度。 保证所有数据在int范围内. Output 对每个询问输出相应的答案,每个答案占一行。 SampleInput 3 123 6 JOIN4 ASK2 LEAVE2 LENGTH FINISH2 LENGTH SampleOutput 2 3 1 #include voidmain() { inta[10005],b[10005],i,m,n,x,y,z,d,j,q; charch[10]; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&a[i]); } scanf("%d",&m); for(i=1;i<=m;i++) { scanf("%s",&ch); if(ch[0]=='J') { scanf("%d",&x); n+=1; a[n]=x; } if(ch[0]=='A') { scanf("%d",&z); printf("%d\n",a[z]); } if(ch[0]=='F') { scanf("%d",&d); for(j=1,q=d+1;q<=n;j++,q++) { b[j]=a[q]; } n=n-d; for(j=1;j<=n;j++) { a[j]=b[j]; } } if(ch[0]=='L'&&ch[2]=='N') { printf("%d\n",n); } if(ch[0]=='L'&&ch[2]=='A') { scanf("%d",&y); for(j=1,q=1;q<=n;q++,j++) { if(q! =y) { b[j]=a[q]; } else { j--; } } n--; for(j=1;j<=n;j++) { a[j]=b[j]; } } } } H-士兵队列训练问题 TimeLimit: 1000MSMemoryLimit: 32768KB64bitIOFormat: %I64d&%I64u SubmitStatusPracticeHDU1276 Description 某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下: 从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。 。 。 ,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。 Input 本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。 Output 共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。 SampleInput 22040 SampleOutput 171911937 #include voidmain() { intn,i,j,a[5001],b[5001],m,q,f,w,e; while(scanf("%d",&m)! =EOF) { for(q=1;q<=m;q++) { scanf("%d",&n); for(i=1;i<=n;i++) a[i]=i; f=2; while(n>3) { if(f==2) { w=n/2;j=1; for(i=1;i<=n;i++) { if(i%2==0)a[i]=0; f=3; } for(i=1;i<=n;i++) { if(a[i]! =0) { b[j]=a[i];j++; } } for(i=1;i<=j-1;i++)a[i]=b[i]; n=n-w; } else { w=n/3;j=1; for(i=1;i<=n;i++) { if(i%3==0)a[i]=0; f=2; } for(i=1;i<=n;i++) { if(a[i]! =0) { b[j]=a[i];j++; } } for(i=1;i<=j-1;i++)a[i]=b[i]; n=n-w; } } for(i=1;i<=n;i++) { if(i! =1)printf(""); printf("%d",a[i]); } printf("\n"); } } G-愚人节的礼物 TimeLimit: 1000MSMemoryLimit: 32768KB64bitIOFormat: %I64d&%I64u SubmitStatusPracticeHDU1870 Description 四月一日快到了,Vayko想了个愚人的好办法――送礼物。 嘿嘿,不要想的太好,这礼物可没那么简单,Vayko为了愚人,准备了一堆盒子,其中有一个盒子里面装了礼物。 盒子里面可以再放零个或者多个盒子。 假设放礼物的盒子里不再放其他盒子。 用()表示一个盒子,B表示礼物,Vayko想让你帮她算出愚人指数,即最少需要拆多少个盒子才能拿到礼物。 Input 本题目包含多组测试,请处理到文件结束。 每组测试包含一个长度不大于1000,只包含'(',')'和'B'三种字符的字符串,代表Vayko设计的礼物透视图。 你可以假设,每个透视图画的都是合法的。 Output 对于每组测试,请在一行里面输出愚人指数。 SampleInput ((((B)()))())(B) SampleOutput 41 #include #include intmain() { chars[1001]; inti,n,num; while(scanf("%s",&s)! =EOF) { num=0; n=strlen(s); for(i=0;i { if(s[i]=='(') num++; if(s[i]==')') { if(s[i-1]=='B') {printf("%d\n",num);break;} if(s[i-1]! ='B') {num--;continue;} } } } } Description 输入一个十进制整数,将其转换成对应的R(2<=R<=9)进制数,并输出。 Input 第一行输入需要转换的十进制数; 第二行输入R。 Output 输出转换所得的R进制数。 SampleInput 1279 8 SampleOutput 2377 #include #definemaxsize100 inttop=0; intpush(intstack[],intx) { if(top>maxsize) return0; stack[top++]=x; return1; } intpop(intstack[]) { top--; returnstack[top]; } intstackempty() { if(top==0) return1; else return0; } intmain() { intn,r,s[100],m; scanf("%d\n%d",&n,&r); while(n! =0) { push(s,n%r); n/=r; } while(! stackempty()) { m=pop(s); printf("%d",m); } printf("\n"); return0; } 排序 ABCDE A-DesignT-Shirt TimeLimit: 1000MSMemoryLimit: 32768KB64bitIOFormat: %I64d&%I64u SubmitStatusPracticeHDU1031 Description SoonafterhedecidedtodesignaT-shirtforourAlgorithmBoardonFree-CityBBS,XKAfoundthathewastrappedbyallkindsofsuggestionsfromeveryoneontheboard.Itisindeedamission-impossibletohaveeverybodyperfectlysatisfied.Sohetookapolltocollectpeople'sopinions.Herearewhatheobtained: NpeoplevotedforMdesignelements(suchastheACM-ICPClogo,bignamesincomputerscience,well-knowngraphs,etc.).Everyoneassignedeachelementanumberofsatisfaction.However,XKAcanonlyputK(<=M)elementsintohisdesign.HeneedsyoutopickforhimtheKelementssuchthatthetotalnumberofsatisfactionismaximized. Input Theinputconsistsofmultipletestcases.Foreachcase,thefirstlinecontainsthreepositiveintegersN,MandKwhereNisthenumberofpeople,Misthenumberofdesignelements,andKisthenumberofelementsXKAwillputintohisdesign.ThenNlinesfollow,eachcontainsMnumbers.Thej-thnumberinthei-thlinerepresentsthei-thperson'ssatisfactiononthej-thelement. Output Foreachtestcase,printinonelinetheindicesoftheKelementsyouwouldsuggestXKAtotakeintoconsiderationsothatthetotalnumberofsatisfactionismaximized.Iftherearemorethanonesolutions,youmustoutputtheonewithminimalindices.Theindicesstartfrom1
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- oj 数据结构 系列