《acmjava答案》由会员分享,可在线阅读,更多相关《acmjava答案(8页珍藏版)》请在金锄头文库上搜索。
1、acmjava 答案【篇一: java-acm 详解(6) 栈 有答案版 全国软件 设计大赛 参考试题】txt 堆栈是一种特殊的线性结构,后进先出,只能对栈顶元素操作, 典型的操作入栈和出站。下面通过例子介绍基本用法。题目: train problem problem description as the new term comes, the ignatius train station is very busy nowadays. a lot of student want to get back to school by train(because the trains in the i
2、gnatius train station is the fastest all over the world AvA). but here comes a problem, there is only one railway where all the trains stop. so all the trains come in from one side and get out from the other side. for this problem, if train a gets into the railway first, and then train b gets into t
3、he railway before train a leaves, train a cant leave until train b leaves. the pictures below figure out the problem.now the problem for you is, there are at most 9 trains in the station, all the trains has an id(numbered from 1 to n), the trains get into the railway in an order o1, your task is to
4、determine whether the trains can get out in an order o2. input the input contains several test cases. each test case consists of an integer, the number of trains, and two strings, the order of the trains come in:o1, and the order of the trains leave:o2. the input is terminated by the end of file. mo
5、re details in the sample input.outputthe output contains a string no. if you cant exchange o2 to o1, or you should output a line contains yes., and then output your way in exchanging the order(you should output in for a train getting into the railway, and out for a train getting out of the railway).
6、 print a line contains finish after each test case. more details in the sample output.sample input3 123 3213 123 312 sample outputyes.inin in out out out finish no.finish题目翻译:火车站非常繁忙,火车 a 先进站,如果火车 b 在火车 a 出站之前进站,则或者 a 只能等或者 b 出站之后再出站(后进先 出)。题目要求有 n(n 最大为 9,编号从 1 到 n) 列火车,给出火车 进站的序列,并给出火车出站的序列,让你判断这个
7、要求能否实现, 如果能实现写出进站 (in) 、出站 (out) 的操作序列。解题思路:基本原则就是后进先出,但是对于特定的火车来说是先 进后出。所以首先考虑入栈的序列,在入栈的过程根据出栈序列判 断是否需要出栈,如果不需要出栈,则继续进栈,如果需要出栈则 进行出栈操作。如果要出栈的元素不在栈顶则表示序列不合法。下 面以入栈序列 1234 和出栈序列 1423 进行分析。考虑入栈: 1 进栈,栈顶元素为 1; 考虑出栈:因为栈顶元素与出栈序列中的第一个元素相同, 1 出栈, 出栈后栈中无元素; 考虑出栈:栈中无元素,所以不用出栈; 考虑入栈: 2 进栈,栈顶元素为 2;考虑出栈:因为栈顶元素是
8、 2,而要出栈的元素是 4,所以不能出栈; 考虑入栈: 3 进栈,栈顶元素为 3,栈中包括 2 和 3; 考虑出栈:因为栈顶元素是 3,而要出栈的元素是 4,所以不能出栈; 考虑入栈: 4 进栈,栈顶元素为 4,栈中包括 2、 3 和 4; 考虑出栈:因为栈顶元素是 4,而要出栈的元素是 4,所以 4 出栈, 出栈后栈中元素为 2 和 3,3 为栈顶元素。考虑出栈:因为栈顶元素是 3,而要出栈的元素是 2,所以不能出栈; 考虑入栈:所有元素已经入栈,所以不能出栈; 无法入栈也无法出栈,而栈中有元素,所以序列不合法。下面是参考代码:/* train problem i*/public stati
9、c string test2(int n,string s1,string s2) /记录操作结果stringbuffer sb = new stringbuffer();/ 表示栈int a=new int9;/ 表示栈顶元素int index=-1;/ 入栈元素序号int index1=0;/ 出栈元素序号int index2=0;/ 把第一个元素放到栈中 index+;aindex=s1.charat(index1);index1+; sb.append(inn);/ 只要栈中有元素,则处理while(index1s1.length() | index2s2.length() / 如果栈
10、顶元素是 与 s2 中的元素一致,则出栈if(index-1 s2.charat(index2)=aindex) index-;sb.append(outn); index2+;else if(index1=s1.length() /不能实现【篇二:杭电 acm 一小部分题目答案】2000 ascii 码排序hdu ac me hdoj 2017 字符串统计 hdu quicksum hdu 首字母大写【篇三: acm 部分练习题目答案】a + b problemtime limit: 2000/1000 ms (java/others) memory limit:65536/32768 k
11、(java/others) total submission(s): 100972 accepted submission(s): 33404problem descriptioncalculate a + b.inputeach line will contain two integers a and b. process to end of file.outputfor each case, output a + b in one line.sample input1 1sample output2# includestdio.hint main()int x,y,s; while(sca
12、nf(%d %d,x,y)!=eof)s=x+y; printf(%dn,s); return 0;sum problemtime limit: 1000/500 ms (java/others) memory limit:65536/32768 k (java/others) total submission(s): 85964 accepted submission(s): 19422 problem descriptionhey, welcome to hdoj(hangzhou dianzi university online judge). in this problem, your
13、 task is to calculate sum(n) = 1 + 2 + 3 + . + n.inputthe input will consist of a series of integers n, one integer per line.outputfor each case, output sum(n) in one line, followed by a blank line. you may assume the result will be in the range of 32-bit signed integer.sample input1100sample output
14、15050# includestdio.hint main()int n;long int s;while(scanf(%d,n)!=eof) s=0;while(n0)s=s+n;n-;printf(%ldnn,s);return 0;a + b problem iitime limit: 2000/1000 ms (java/others) memory limit: 65536/32768 k (java/others) total submission(s): 58216 accepted submission(s): 10500problem descriptioni have a
15、very simple problem for you. given two integers a and b, your job is to calculate the sum of a + b.inputthe first line of the input contains an integer t(1=t=20) which means the number of test cases. then t lines follow, each line consists of two positive integers, a and b. notice that the integers are very large, that means you should not process them by using 32-bit integer. you may assume the length of each integer will not exceed 1000.outputfor each test case, you should output two lines. the first line is case #:, # means the number o