版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、2009机试2计算和的数位2大写改小写3素数对4求最大公约数和最小公倍数6排序后求位置处的数7*路由器连接8*编译原理10*分开连接132010机试17ecnu的含义17空瓶换啤酒18统计字符202010机试热身21粽子买三送一,买五送二21工程流水线问题222011机试24hello world24special judge26查询成绩282011机试热身30贪吃蛇30仰望星空34*编辑距离362012机试38字母排序38幸运数39十六进制的加法42电话号码簿合并排序42*五子棋43*正则表达式匹配452013机试46斐波那契数列的素数个数46*将a字符变成b字符最少修改次数472013机试
2、热身49去重排序49蛇形图案51数学手稿542009机试计算和的数位sum of digit description write a program which computes the digit number of sum of two integers a and b. input the first line of input gives the number of cases, n(1 n 100). n test cases follow.each test case consists of two integers a and b which are separeted by a
3、 space in a line. (0<=a,b<=100000000). output for each test case, print the number of digits of a + b. sample input 35 71 991000 999 sample output 234 #include<stdio.h>int main()int n;int a,b;int sum;while(scanf("%d",&n)!=eof)while(n-)int an=0;scanf("%d%d",&a,
4、&b);sum=a+b;while(sum)an+;sum/=10;printf("%dn",an+);return 0;大写改小写capitalize description write a program which replace all the lower-case letters of a given text with the corresponding captital letters. input a text including lower-case letters, periods, and space.output output the con
5、verted text. sample input welcome to east china normal university. sample output welcome to east china normal university. #include<stdio.h>#include<string.h>char str1000;int main()int l;while(gets(str)l=strlen(str);int i;for(i=0;i<l;i+)if(stri>='a'&&stri<='z&
6、#39;)printf("%c",stri-32);elseprintf("%c",stri);printf("n");return 0;素数对primes pair description we arrange the numbers between 1 and n (1 <= n <= 10000) in increasing order and decreasing order like this: 1 2 3 4 5 6 7 8 9 . . . nn . . . 9 8 7 6 5 4 3 2 1two numbe
7、rs faced each other form a pair. your task is to compute the number of pairs p such that both numbers in the pairs are prime. input the first line of input gives the number of cases, c (1 c 100). c test cases follow.each test case consists of an integer n in one line. output for each test case, outp
8、ut p . sample input 414751 sample output 0226 #include<stdio.h>#include<string.h>bool prime10005;void init()int i;int j;prime0=prime1=false;/不是素数prime2=true;/是素数for(i=3;i<=10005;i+=2)primei=true;/是素数primei+1=false;/不是素数除0和2之外的偶数都不是素数for(i=3;i<=10005;i+=2)if(primei=true)/是素数j=i+i;wh
9、ile(j<=10005)primej=false;/不是素数j+=i;int main()int c;int n;init();/初始化while(scanf("%d",&c)!=eof)while(c-)scanf("%d",&n);int sum=0;int i;for(i=2;i<=n/2;i+)if(primei=true&&primen+1-i=true)sum+;sum*=2;if(n%2=1)/n为奇数if(primen/2+1=true)sum+=1;printf("%dn"
10、;,sum);return 0;求最大公约数和最小公倍数gcd and lcm description write a program which computes the greatest common divisor (gcd) and the least common multiple (lcm) of given a and b (0 < a, b 44000). input the first line of input gives the number of cases, n(1 n 100). n test cases follow.each test case conta
11、ins two interger a and b separated by a single space in a line. output for each test case, print gcd and lcm separated by a single space in a line. sample input 28 65000 3000 sample output 2 241000 15000 #include<stdio.h>int getgcd(int a,int b)int gcd;int t1,t2;t1=a;t2=b;gcd=t1%t2;while(gcd!=0
12、)t1=t2;t2=gcd;gcd=t1%t2;return t2;int main()int n;int a,b;while(scanf("%d",&n)!=eof)while(n-)scanf("%d%d",&a,&b);printf("%d %dn",getgcd(a,b),a*b/(getgcd(a,b);return 0;排序后求位置处的数sort it description there is a database,partychen want you to sort the databases d
13、ata in the order from the least up to the greatest element,then do the query: "which element is i-th by its value?"- with i being a natural number in a range from 1 to n.it should be able to process quickly queries like this. input the standard input of the problem consists of two parts. a
14、t first, a database is written, and then there's a sequence of queries. the format of database is very simple: in the first line there's a number n (1<=n<=100000), in the next n lines there are numbers of the database one in each line in an arbitrary order. a sequence of queries is wri
15、tten simply as well: in the first line of the sequence a number of queries k (1 <= k <= 100) is written, and in the next k lines there are queries one in each line. the query "which element is i-th by its value?" is coded by the number i. output the output should consist of k lines.
16、in each line there should be an answer to the corresponding query. the answer to the query "i" is an element from the database, which is i-th by its value (in the order from the least up to the greatest element). sample input 5712112371213325 sample output 1217123#include<stdio.h>#in
17、clude<algorithm>using namespace std;int num100010;int pos105;int main()int n;int i;int k;while(scanf("%d",&n)!=eof)for(i=1;i<=n;i+)scanf("%d",&numi);scanf("%d",&k);for(i=1;i<=k;i+)scanf("%d",&posi);sort(num+1,num+1+n);for(i=1;i<=
18、k;i+)printf("%dn",numposi);return 0;*路由器连接hub connection plan description partychen is working as system administrator and is planning to establish a new network in his company. there will be n hubs in the company, they can be connected to each other using cables. since each worker of the
19、company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs). since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the cost is minimal.
20、 partychen will provide you all necessary information about possible hub connections. you are to help partychen to find the way to connect hubs so that all above conditions are satisfied. input the first line of the input contains two integer numbers: n - the number of hubs in the network (2 <= n
21、 <= 1000) and m - the number of possible hub connections (1 <= m <= 15000). all hubs are numbered from 1 to n. the following m lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable cost required to connect them. cost is a positi
22、ve integer number that does not exceed 106. there will always be at least one way to connect all hubs. output output the minimize cost of your hub connection plan. sample input 4 61 2 11 3 11 4 22 3 13 4 12 4 1 sample output 3#include<stdio.h>#include<algorithm>using namespace std;struct
23、 edgeint a,b;int cost;e15010;int tree1010;int findroot(int x)if(treex=-1)return x;elseint tmp=findroot(treex);treex=tmp;return tmp;bool cmp(edge a,edge b)return a.cost<b.cost;int main()int n;int m;int i;while(scanf("%d",&n)!=eof)scanf("%d",&m);for(i=1;i<=m;i+)scanf(
24、"%d%d%d",&ei.a,&ei.b,&ei.cost);sort(e+1,e+1+m,cmp);/排序for(i=1;i<=n;i+)treei=-1;int ans=0;for(i=1;i<=m;i+)int a=findroot(ei.a);int b=findroot(ei.b);if(a!=b)treea=b;ans+=ei.cost;printf("%dn",ans);return 0;*编译原理principles of compiler description after learnt the pr
25、inciples of compiler,partychen thought that he can solve a simple expression problem.so he give you strings of less than 100 characters which strictly adhere to the following grammar (given in ebnf): a:= '(' b')'|'x'. b:=ac. c:='+'a.can you solve them too? input the f
26、irst line of input gives the number of cases, n(1 n 100). n test cases follow.the next n lines will each contain a string as described above. output for each test case,if the expression is adapt to the ebnf above output “good”,else output “bad”. sample input 3(x)(x+(x+x)()(x) sample output goodgoodb
27、ad #include <cstdio>#include <cstring>#include <cstdlib>#include <vector>#include <cmath>#include <iostream>#include <algorithm>#include <functional>#include <string>#include <map>#include <cctype> using namespace std;char ex110;int i
28、ndex;bool a();bool b();bool c();bool a() if(exindex='x') index+; while(exindex=' ') index+; return true; if(exindex='(') index+; while(exindex=' ') index+; if(b()&&exindex=')') index+; while(exindex=' ') index+; return true; return false;bool b
29、() return a()&&c();bool c() while(exindex='+') index+; while(exindex=' ') index+; /return a(); if (!a() return false; return true;int main() int n; scanf("%d",&n); getchar(); while(n-) gets(ex); index=0; printf("%sn",a()&&exindex='0'?&q
30、uot;good":"bad"); return 0;*分开连接separate connections description partychen are analyzing a communications network with at most 18 nodes. character in a matrix i,j (i,j both 0-based,as matrixij) denotes whether nodes i and j can communicate ('y' for yes, 'n' for no). as
31、suming a node cannot communicate with two nodes at once, return the maximum number of nodes that can communicate simultaneously. if node i is communicating with node j then node j is communicating with node i. input the first line of input gives the number of cases, n(1 n 100). n test cases follow.i
32、n each test case,the first line is the number of nodes m(1 m 18),then there are a grid by m*m describled the matrix. output for each test case , output the maximum number of nodes that can communicate simultaneously sample input 25nyyyyynnnnynnnnynnnnynnnn5nyyyyynnnnynnnyynnnyynyyn sample output 24h
33、intthe first test case: all communications must occur with node 0. since node 0 can only communicate with 1 node at a time, the output value is 2.the second test case: in this setup, we can let node 0 communicate with node 1, and node 3 communicate with node 4. #include <cstdio>#include <cs
34、tring>#include <cstdlib>#include <vector>#include <cmath>#include <iostream>#include <algorithm>#include <functional>#include <string>#include <map>#include <queue>using namespace std;#define maxn 250#define maxe maxn*maxn*2#define set(a,b) me
35、mset(a,b,sizeof(a)deque<int> q;bool gmaxnmaxn,inquemaxn,inblossommaxn;int matchmaxn,premaxn,basemaxn;int findancestor(int u,int v) bool inpathmaxn= false; while(1) u=baseu; inpathu=true; if(matchu=-1)break; u=prematchu; while(1) v=basev; if(inpathv)return v; v=prematchv; void reset(int u,int a
36、nc) while(u!=anc) int v=matchu; inblossombaseu=1; inblossombasev=1; v=prev; if(basev!=anc)prev=matchu; u=v; void contract(int u,int v,int n) int anc=findancestor(u,v); set(inblossom,0); reset(u,anc); reset(v,anc); if(baseu!=anc)preu=v; if(basev!=anc)prev=u; for(int i=1; i<=n; i+) if(inblossombase
37、i) basei=anc; if(!inquei) q.push_back(i); inquei=1; bool dfs(int s,int n) for(int i=0; i<=n; i+)prei=-1,inquei=0,basei=i; q.clear(); q.push_back(s); inques=1; while(!q.empty() int u=q.front(); q.pop_front(); for(int v=1; v<=n; v+) if(guv&&basev!=baseu&&matchu!=v) if(v=s|(matchv
38、!=-1&&prematchv!=-1)contract(u,v,n); else if(prev=-1) prev=u; if(matchv!=-1)q.push_back(matchv),inquematchv=1; else u=v; while(u!=-1) v=preu; int w=matchv; matchu=v; matchv=u; u=w; return true; return false;int solve(int n) set(match,-1); int ans=0; for(int i=1; i<=n; i+) if(matchi=-1&
39、;&dfs(i,n) ans+; return ans;int main() int ans; int n,m; char tmp30; scanf("%d",&n); while(n-) ans=0; memset(g,0,sizeof(g); scanf("%d",&m); for(int i=1;i<=m;i+) scanf("%s",tmp+1); for(int j=1;j<=m;j+) if(tmpj='y') gij=gji=1; ans=solve(m); print
40、f("%dn",ans*2); return 0;2010机试ecnu的含义welcome to 2009 acm selective trial description welcome to 2009 acm selective trial. acm is a long way to go, and it's not just a match. so what you need to do for now is do your best! and as members of acm lab, we are going to teach you something
41、important. firstly you should be proud that you are a member of ecnu, because 'e' represents "excellent", 'c' represents "cheer", 'n' represents "nice", 'u' represents "ultimate". second you should remember impossible is nothing
42、, because "impossible" represents "i'm possible". third for today you should keep acm, because for you acm represents "accept more".do you remember them clearly?now we will give you a string either "e" ,"c", "n","u","impo
43、ssible" or"acm", you need to tell me what does it means? input the first line of input gives the number of cases, n(1 n 10). n test cases follow.each test consists of a string which will be one of "e" ,"c", "n","u","impossible" or"
44、;acm". output tell me what does it means.sample input 3eimpossibleacm sample output excellenti'm possibleaccept more #include<stdio.h>#include<string.h>char str20;int main()int n;scanf("%d",&n);while(n-)scanf("%s",str);if(strcmp(str,"e")=0)print
45、f("excellentn");else if(strcmp(str,"c")=0)printf("cheern");else if(strcmp(str,"n")=0)printf("nicen");else if(strcmp(str,"u")=0)printf("ultimaten");else if(strcmp(str,"impossible")=0)printf("i'm possiblen");
46、else if(strcmp(str,"acm")=0)printf("accept moren");return 0;空瓶换啤酒soda surpler description tim is an absolutely obsessive soda drinker,he simply cannot get enough. most annoyinglythough, he almost never has any money, so his only obvious legal way to obtain more soda is to take th
47、e money he gets when he recycles empty soda bottles to buy new ones. in addition to the empty bottles resulting from his own consumption he sometimes find empty bottles in the street. one day he was extra thirsty, so he actually drank sodas until he couldn't aford a new one. input three non-nega
48、tive integers e,f, c, where e < 1000 equals the number of empty sodabottles in tim's possession at the start of the day, f < 1000 the number of empty sodabottles found during the day, and 1 < c < 2000 the number of empty bottles required tobuy a new soda. output how many sodas did ti
49、m drink on his extra thirsty day? sample input 9 0 3 5 5 2 sample output 49 #include<stdio.h>#include<string.h>int main()int e,f,c;int t;int sum;int full,empty;while(scanf("%d%d%d",&e,&f,&c)!=eof)sum=0; empty=e+f;/空瓶数量while(empty>=c)/空瓶数量可换sum+=empty/c;/换的满瓶empty
50、=empty/c+empty%c;/新的空瓶数量printf("%dn",sum);return 0;统计字符统计字符 description 输入一行字符,分别统计其中 英文字母、空格、数字和其他字符的个数。 input 输入一个整数t,表示有几组数据接下来有t行,每行字符不超过10000个hint 可能有空格之类的字符 output 对于每行字符输出其中1英文字母(大小写都算)的个数 2数字的个数3其他字符的个数 sample input 2q2 e2qweqrwwerr232424fwetetg=2342gdsg3.,/-=321sample output chara
51、cter:2number:2others:1character:21number:14others:9 #include<stdio.h>#include<string.h>char str10010;int main()int t;int i;int cn,nn,on;scanf("%d",&t);getchar();/清除上一个换行符while(t-)gets(str);int l=strlen(str);cn=nn=on=0;for(i=0;i<l;i+)if(stri>='0'&&stri&
52、lt;='9')nn+;else if(stri>='a'&&stri<='z'|stri>='a'&&stri<='z')cn+;elseon+;printf("character:%dn",cn);printf("number:%dn",nn);printf("others:%dn",on);return 0;2010机试热身粽子买三送一,买五送二端午节快乐 description 今天是端午节,
53、ecnu决定请大家吃粽子。恰好,今天超市为了迎合"端午节",推出了"端午大酬宾",即促销活动。严格的买三送一,买五送二。ecnu想用现有的钱,买最多的粽子,但是他自己又不会算,所以希望你能帮帮他。 input 输入第一行为一个数n(1<=n<=100),表示测试数据的组数。每组测试数据有两个整数,a,b (0<=a<=1000,0<b<10)表示ecnu有a元钱,每个粽子价格为b元钱,超市推出了买5个送2个,和买3个送1个的活动。 output 输出ecnu最多能买到的粽子数量。 sample input 210 322 3sample output 49hint:有两组测试数据:对于第一组测试数据:有10元钱,粽子3元一个,可以买3个,但是买3送1,所以最后有4个。对于第二组测试数据:有22元钱,粽子3元一个,可以买7个,但是买5送2,所以最后有9个。 #include<stdio.h>#include<string.h>int main()int n;int a,b;int zn;int num;scanf("%d",&n);while(n-) scanf("%d%d",&a,&b);/输入钱数
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论