




已阅读5页,还剩137页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
哈尔滨工程大学ACM测试题及答案1045:DescriptionLittle Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.This is an example of one of her creations: D / / B E / / A C G / / FTo record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.However, doing the reconstruction by hand, soon turned out to be tedious.So now she asks you to write a program that does the job for her!InputThe input will contain one or more test cases.Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)Input is terminated by end of file.OutputFor each test case, recover Valentines binary tree and print one line containing the trees postorder traversal (left subtree, right subtree, root).Sample InputDBACEGF ABCDEFGBCAD CBADSample OutputACBFGEDCDAB #include#includechar pre30,in30;void recover(int pi,int pj,int ii,int ij) if(pi+1pj) return; int i=ii; while(ini!=prepi) i+; for(int j=pi; jpj-1; j+) prej=prej+1; prepj-1=ini; recover(pi,pi+i-ii,ii,i); recover(pi+i-ii,pj-1,i+1,ij); int main() while(scanf(%s%s,pre,in)!=EOF) int len=strlen(pre); recover(0,len,0,len); puts(pre); 1003: DescriptionEach employee of a bureaucracy has a job description - a few paragraphs that describe the responsibilities of the job. The employees job description, combined with other factors, such as seniority, is used to determine his or her salary.The Hay Point system frees the Human Resources department from having to make an intelligent judgement as to the value of the employee; the job description is merely scanned for words and phrases that indicate responsibility. In particular, job descriptions that indicate control over a large budget or management over a large number of people yield high Hay Point scores.You are to implement a simplified Hay Point system. You will be given a Hay Point dictionary and a number of job descriptions. For each job description you are to compute the salary associated with the job, according to the system.InputThe first line of input contains 2 positive integers: m = 1000, the number of words in the Hay Point dictionary, and n = 100, the number of job descriptions. m lines follow; each contains a word (a string of up to 16 lower-case letters) and a dollar value (a real number between 0 and 1,000,000). Following the dictionary are the n job descriptions. Each job description consists of one or more lines of text; for your convenience the text has been converted to lower case and has no characters other than letters, numbers, and spaces. Each job description is terminated by a line containing a period.OutputFor each job description, output the corresponding salary computed as the sum of the Hay Point values for all words that appear in the description. Words that do not appear in the dictionary have a value of 0.Sample Input7 2administer 100000spending 200000manage 50000responsibility 25000expertise 100skill 50money 75000the incumbent will administer the spending of kindergarden milk moneyand exercise responsibility for making change he or she will shareresponsibility for the task of managing the money with the assistantwhose skill and expertise shall ensure the successful spending exercise.this individual must have the skill to perform a heart transplant andexpertise in rocket science.Sample Output700150150#include#includestruct char word17; long int value; a1001;int m,n;char b17,ch;int main() while(scanf(%d%d,&m,&n)!=EOF) for(int i=0; im; i+) scanf(%s%ld,ai.word,&ai.value); getchar(); for(int k=0; kn; k+) ch=getchar(); long int sum=0; while(ch!=.) int j=0; while(ch!= &ch!=n) bj+=ch; ch=getchar(); bj=0; /puts(b); for(int i=0; im; i+) if(strcmp(ai.word,b)=0) sum+=ai.value; /printf(%sn,b); /printf(%dn,sum); ch=getchar(); printf(%dn,sum); 1000: DescriptionCalculate a + bInputThe input will consist of a series of pairs of integers a and b,separated by a space, one pair of integers per line.OutputFor each pair of input integers a and b you should output the sum of a and b in one line,and with one line of output for each line in input.Sample Input1 5Sample Output6 #include int main() /write your codes int a, b; while(scanf(%d%d, &a, &b) !=EOF) printf(%dn,a+b); return 0; 1227: DescriptionGive you a number, print characters like this:1*2.*.3*.*.*.*.*InputThe input contains multiple test cases. Each case consist of a integer n.OutputFor each test case, print the character table like the examples.Sample Input12345Sample Output*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.*.* #includebool flag;char a10001000,ch;int n;void fun(int x,int y) if(flag) ch=*; else ch=.; for(int j=x; j=x+y-1; j+) axj=ch; ax+y-1j=ch; ajx=ch; ajx+y-1=ch; flag=!flag; int main() / freopen(in.txt,r,stdin); /freopen(out.txt,w,stdout); while(scanf(%d,&n)!=EOF) if(n%2) flag=true; else flag=false; int m=n; for(int i=0; i=n; i+) fun(i,2*m-1); m-; for(int i=0; i2*n-1; i+) for(int j=0; j2*n-1; j+) printf(%c,aij); printf(n); 1078: DescriptionThe fight goes on, whether to store numbers starting with their most significant digit or their least significant digit. Sometimes this is also called the Endian War. The battleground dates far back into the early days of computer science. Joe Stoy, in his (by the way excellent) book Denotational Semantics, tells following story:The decision which way round the digits run is, of course, mathematically trivial. Indeed, one early British computer had numbers running from right to left (because the spot on an oscilloscope tube runs from left to right, but in serial logic the least significant digits are dealt with first). Turing used to mystify audiences at public lectures when, quite by accident, he would slip into this mode even for decimal arithmetic, and write things like 73+42=16. The next version of the machine was made more conventional simply by crossing the x-deflection wires: this, however, worried the engineers, whose waveforms were all backwards. That problem was in turn solved by providing a little window so that the engineers (who tended to be behind the computer anyway) could view the oscilloscope screen from the back.C. Strachey - private communication.You will play the role of the audience and judge on the truth value of Turings equations.InputThe input contains several test cases. Each specifies on a single line a Turing equation. A Turing equation has the form a+b=c, where a, b, c are numbers made up of the digits 0, ., 9. Each number will consist of at most 7 digits. This includes possible leading or trailing zeros. The equation 0+0=0 will finish the input and has to be processed, too. The equations will not contain any spaces.OutputFor each test case generate a line containing the word True or the word False, if the equation is true or false, respectively, in Turings interpretation, i.e. the numbers being read backwards.Sample Input73+42=165+8=1310+20=300001000+000200=000301234+5=12391+0=07000+8000=510+0=0Sample OutputTrueFalseTrueTrueFalseFalseTrueTrue #include#include#includeint k;char s110,s210,s310,ch;int fun(char *s) int j=0; char s210; int len=strlen(s); for(int i=len-1; i=0; i-) s2j+=si; s2j=0; return atoi(s2); int main() while(1) k=0; ch=getchar(); while(ch!=+) s1k+=ch; ch=getchar(); s1k=0; k=0; ch=getchar(); while(ch!=) s2k+=ch; ch=getchar(); s2k=0; scanf(%s,&s3); getchar(); if(fun(s1)+fun(s2)=fun(s3) printf(Truen); else printf(Falsen); if(strcmp(s1,0)=0&strcmp(s2,0)=0&strcmp(s3,0)=0) break; 1049: DescriptionQueues and Priority Queues are data structures which are known to most computer scientists. The Team Queue, however, is not so well known, though it occurs often in everyday life. At lunch time the queue in front of the Mensa is a team queue, for example.In a team queue each element belongs to a team. If an element enters the queue, it first searches the queue from head to tail to check if some of its teammates (elements of the same team) are already in the queue. If yes, it enters the queue right behind them. If not, it enters the queue at the tail and becomes the new last element (bad luck). Dequeuing is done like in normal queues: elements are processed from head to tail in the order they appear in the team queue.Your task is to write a program that simulates such a team queue.InputThe input will contain one or more test cases. Each test case begins with the number of teams t (1=t=1000). Then t team descriptions follow, each one consisting of the number of elements belonging to the team and the elements themselves. Elements are integers in the range 0 - 999999. A team may consist of up to 1000 elements.Finally, a list of commands follows. There are three different kinds of commands: ENQUEUE x - enter element x into the team queue DEQUEUE - process the first element and remove it from the queue STOP - end of test caseThe input will be terminated by a value of 0 for t.For each test case, first print a line saying Scenario #k, where k is the number of the test case. Then, for each DEQUEUE command, print the element which is dequeued on a single line. Print a blank line after each test case, even after the last one.OutputSample Input23 101 102 1033 201 202 203ENQUEUE 101ENQUEUE 201ENQUEUE 102ENQUEUE 202ENQUEUE 103ENQUEUE 203DEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP25 259001 259002 259003 259004 2590056 260001 260002 260003 260004 260005 260006ENQUEUE 259001ENQUEUE 260001ENQUEUE 259002ENQUEUE 259003ENQUEUE 259004ENQUEUE 259005DEQUEUEDEQUEUEENQUEUE 260002ENQUEUE 260003DEQUEUEDEQUEUEDEQUEUEDEQUEUESTOP0Sample OutputScenario #1101102103201202203Scenario #2259001259002259003259004259005260001 #include#include#include#includeusing namespace std;int team1000000;list *pos1001;list list Q;void input_team(int t) int i,j,temp,n; memset(team,-1,sizeof(team); for(i=0;i1001;i+)posi=NULL; for(i=0;it;i+) scanf(%d,&n); for(j=0;jn;j+) scanf(%d,&temp); teamtemp=i; Q.clear();void go_last(int num) list tQ; tQ.push_back(num); Q.push_back(tQ);void ENQUEUE() list tQ; /? int num; scanf(%d,&num); if(teamnum=-1)/插到全队最后 go_last(num); else if(posteamnum=NULL)/插到全队最后 go_last(num); posteamnum=&(Q.back();/! else(*posteamnum).push_back(num); /插到小队最后 void DEQUEUE() int num=(Q.front().front(); printf(%dn,num); (Q.front().pop_front(); if(Q.front().empty()/! Q.pop_front(); posteamnum=NULL; void workout() char order10; while(1) scanf(%s,order); switch(order0) caseE:ENQUEUE();break; caseD:DEQUEUE();break; caseS:printf(n);return; int main() int t,num=1; while(1) scanf(%d,&t); if(t=0)break; input_team(t); printf(Scenario #%dn,num+); workout(); return 0;1203: DescriptionGiven an array of integers AN, you are asked to decide the shortest array of integers BM, such that the following two conditions hold.For all integers 0 = i N, there exists an integer 0 = j M, such that Ai = BjFor all integers 0 = i j M, we have Bi BjNotice that for each array A a unique array B exists.InputThe input consists of several test cases. For each test case, an integer N (1 = N = 100) is given, followed by N integers A0, A1, ., AN - 1 in a line. A line containing only a zero indicates the end of input.OutputFor each test case in the input, output the array B in one line. There should be exactly one space between the numbers, and there should be no initial or trailing spaces.Sample Input8 1 2 3 4 5 6 7 88 8 7 6 5 4 3 2 18 1 3 2 3 1 2 3 10Sample Output1 2 3 4 5 6 7 81 2 3 4 5 6 7 81 2 3SourceSHI, Xiaohan #include#include#includeusing namespace std;int cmp(const void *a,const void *b) return *(int *)a-*(int *)b; int a101,n;int main() while(scanf(%d,&n)!=EOF&n!=0) for(int i=0; in; i+) scanf(%d,&ai); qsort(a,n,sizeof(a0),cmp); set intset; set:iterator si,sj; for(int i=0; in; i+) intset.insert(ai); sj=-intset.end(); for(si=intset.begin(); si!=sj; si+) printf(%d ,*si); printf(%d,*si); printf(n); 1096: DescriptionMr Cheng is a collector of old Chinese porcelain, more specifically late 15th century Feng dynasty vases. The art of vase-making at this time followed very strict artistic rules. There was a limited number of accepted styles, each defined by its shape and decoration. More specifically, there were 36 vase shapes and 36 different patterns of decoration - in all 1296 different styles.For a collector, the obvious goal is to own a sample of each of the 1296 styles. Mr Cheng however, like so many other collectors, could never afford a complete collection, and instead concentrates on some shapes and some decorations. As symmetry between shape and decoration was one of the main aesthetical paradigms of the Feng dynasty, Mr Cheng wants to have a full collection of all combinations of k shapes and k decorations, for as large a k as possible. However, he has discovered that determining this k for a given collection is not always trivial. This means that his collection might actually be better than he thinks. Can you help him?InputOn the first line of the input, there is a single positive integer n, telling the number of test scenarios to follow. Each test scenario begins with a line containing a single positive integer m = 100, the number of vases in the collection. Then follow m lines, one per vase, each with a pair of numbers, si and di, separated by a single space, where si (0 si = 36) indicates the shape of Mr Chengs ith vase, and di (0 di = 36) indicates its decoration.OutputFor each test scenario, output one line containing the maximum k, such that there are k shapes and k decorations for which Mr Che
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- vb考试试题及答案解释
- tq考试试题及答案
- 禁燃禁放烟花爆竹课件
- 禁毒知识教育培训记录课件
- DB61T 519-2011 粮食注氮控氧储藏系统 设计、施工与验收规范
- 山东省潍坊市示范初中2025年数学高三上期末学业质量监测试题
- 内蒙古包头市2025年数学高三第一学期期末学业水平测试试题
- 装配基础知识培训
- 2025年文理综试题及答案
- 国家公务员考试试题及答案
- GA/T 2159-2024法庭科学资金数据清洗规程
- 2025年职工职业技能竞赛(物业管理师)参考试题(附答案)
- 成人肠造口护理要点与实践课件
- 会务服务面试题及答案
- 2025年人教版小学四年级数学上册全册单元检测试卷(全套版)
- 2025年体育与健康教材教法考试模拟试卷及答案
- 2025年江西省高职单招文化统一考试真题及答案(网络版)
- 小学生学习习惯养成教育课件
- 《医疗机构重大事故隐患判定清单(试行)》知识培训
- 水行政处罚培训课件
- 劳务临时用工合同
评论
0/150
提交评论