版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 字符最少修改次数47
2、2013 机试热身49去重排序49蛇形图案51数学手稿542009 机试计算和的数位Sum of digitDescriptionWrite a program which computes the digit number of sum of two integers a and b.InputThe 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
3、by a space(0<=a,b<=100000000).in a line.OutputFor each test case, print the number of digits of a + b.Sample Input35 71 991000 999Sample Output234#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、amp;b); sum=a+b;while(sum)an+;sum/=10;printf("%dn",an+);return 0;大写改小写CapitalizeDescriptionWrite a program which replace all the lower-case letters of a given text with the corresponding captital letters.InputA text including lower-case letters, periods, and space.OutputOutput The converte
5、d text.Sample Inpute to eastnormal university.Sample OutputE TO EASTNORMAL 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')printf("%c",stri-
6、32); else printf("%c",stri);printf("n");return 0;素数对Primes PairDescriptionWe 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 numbers faced each other form a pair.
7、Your task is to compute the number of pairs P suchthat both numbers in the pairs are prime.InputThe first line of input gives the number of cases, C (1 Each test case consists of an integer N in one line.C 100). C test cases follow.OutputFor each test case, output P . Sample Input414751Sample Output
8、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; while(j<=10005)primej=false;/不是素
9、数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",sum);return 0;求最大公约数和最小公
10、倍数and LCMDescriptionWrite a program which computes the greatest common divisor ( multiple (LCM) of given a and b (0 < a, b 44000).) and the least commonInputThe first line of input gives the number of cases, N(1 N 100). N test cases follow. Each test case contains two interger a and b separated b
11、y a single space in a line.OutputFor each test case, printand LCM separated by a single space in a line.Sample Input28 65000 3000Sample Output2 241000 15000#include<stdio.h>int getint(int a,int b);int t1,t2;t1=a; t2=b;=t1%t2;while(t1=t2; t2=!=0);=t1%t2;return t2;int main()int n; int a,b;while(
12、scanf("%d",&n)!=EOF)while(n-)scanf("%d%d",&a,&b);printf("%d %dn",getreturn 0;(a,b),a*b/(get(a,b);排序后求位置处的数Sort itDescriptionThere is a database,partychen want you to sort the databases data in the order from the least up to the greatest element,then do the q
13、uery: "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.InputThe standard input of the problem consists of two parts. At first, a database is written, and then there's a sequence of queries
14、. 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 written simply as well: in the first line of the sequence a number of queries
15、 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.OutputThe output should consist of K lines. In each line there should be an answer to the correspondingquery. The answer
16、 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 Input5712112371213325Sample Output1217123#include<stdio.h> #include<algorithm> using namespace std; int num100010;int pos105; int main()
17、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<=k;i+) printf("%dn",numposi);return 0;*路由器连接Hub Connection pl
18、anDescriptionPartychen 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 company must have access to the whole network, each hub must beaccessibl
19、e 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. partychen will provide you all necessary information about possible hub c
20、onnections. You are to help partychen to find the way toconnect hubs so that all above conditions are satisfied.InputThe first line of the input contains two integer numbers: N - the number of hubs in the network (2<= N <= 1000) and M - the number of possible hub connections (1 <= M <= 1
21、5000). 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 positive integer number that does not exceed 106. There will always be at least one
22、way to connect all hubs.OutputOutput the minimize cost of your hub connection plan.Sample Input4 61 2 112 4 1Sample Output3#include<stdio.h>#include<algorithm>using namespace std;struct Edge int a,b;int cost;E15010;int Tree1010;int findRoot(int x)if(Treex=-1)return x;elseint tmp=findRoot
23、(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("%d%d%d",&Ei.a,&Ei.b,&Ei.cost); sort(E+1,E+1+m,Cmp);/排序for(i=1;i<=n;i+)Tr
24、eei=-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 CompilerDescriptionAfter learnt the Principles of Compiler,partychen thought that he can solve a simple expression problem.So he give
25、 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?InputThe first line of input gives the number of cases, N(1 N 100). N test cases follow. The next N lines wi
26、ll each contain a string as described above.OutputFor each test case,if the expression is adapt to the EBNF above output “Good”,else output “Bad”.Sample Input3(x) (x+(x+x)()(x)Sample OutputGood Good Bad#include <cstdio>#include <cstring> #include <cstdlib> #include <vector> #
27、include <cmath> #include <iostream> #include <algorithm> #include <functional> #include <string> #include <map> #include <cctype>using namespace std;char ex110; int index; bool A();bool B();bool C(); bool A()if(exindex='x')index+;while(exindex='
28、') index+; return true;if(exindex='(')index+;while(exindex=' ') index+; if(B()&&exindex=')')index+;while(exindex=' ') index+; return true;return false;bool B()return A()&&C();bool C()while(exindex='+')index+;while(exindex=' ') index
29、+;/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'?"Good":"Bad");return 0;*连接Separate ConnectionsDescriptionPartychen are analyzing a communica
30、tions 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). Assuming a node cannot communicate with two nodes at once, return theumnumber of nodes that can communicate simultaneo
31、usly. If node i is communicating with node jthen node j is communicating with node i.InputThe first line of input gives the number of cases, N(1 N 100). N test cases follow.In 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.OutputFor
32、each test case , output theum number of nodes that can communicate simultaneouslySample Input25NYYYY YNNNN YNNNN YNNNN YNNNN 5 NYYYY YNNNN YNNNY YNNNYYNYYNSample Output24HintThe first test case:All communications must occur with node 0. Since node 0 can only communicate with 1 node at a time, the ou
33、tput 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 <cstring> #include <cstdlib> #include <vector> #include <cmath> #include <iostream> #include <algorithm&g
34、t; #include <functional> #include <string> #include <map> #include <queue>using namespace std;#define MAXN 250#define MAXE MAXN*MAXN*2 #define SET(a,b) memset(a,b,sizeof(a) deque<int> Q;bool gMAXNMAXN,inqueMAXN,inblossomMAXN;int matchMAXN,preMAXN,baseMAXN; int findances
35、tor(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 anc)while(u!=anc)int v=matchu; inblossombaseu=1; inblossombasev=1; v=prev;if(basev!=anc)prev=matchu; u=v;void contract(int u,i
36、nt 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(inblossombasei)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.
37、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!=-1&&prematchv!=-1)contract(u,v,n); else if(prev=-1)prev=u;if(matchv!=-1)Q.push_back(matchv),inquematchv=1; elseu=v; while(u!=-1)v=pr
38、eu;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&&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&q
39、uot;,&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); printf("%dn",ans*2);return 0;2010 机试ECNU 的含义e to 2009 ACM selective trialDescriptione to 2009 ACM selective trial. ACM is a long way to go, and it's not
40、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 important. Firstly you should be proud that you are a member of ECNU, because 'E' represents "Excellent", 'C' represents "Cheer", 'N
41、9; represents "Nice", 'U' represents "Ultimate". Second you should remember Impossible is nothing, because "Impossible" represents "I'm possible". Third for today you should keep ACM, because for you ACM represents "Accept More". Do you r
42、emember them clearly?Now we will give you a string either "E" ,"C", "N","U","Impossible" or"ACM", you need to tell mewhat does it means?InputThe first line of input gives the number of cases, N(1 N 10). N test cases follow. Each test consis
43、ts of a string which will be one of "E" ,"C", "N","U","Impossible" or"ACM".OutputTell me what does it means.Sample Input3EImpossible ACMSample OutputExcellent I'm possibleAccept More#include<stdio.h>#include<string.h>char st
44、r20;int main()int N; scanf("%d",&N); while(N-)scanf("%s",str);if(strcmp(str,"E")=0) printf("Excellentn");else if(strcmp(str,"C")=0) printf("Cheern");else if(strcmp(str,"N")=0) printf("Nicen");else if(strcmp(str,"
45、;U")=0) printf("Ultimaten");else if(strcmp(str,"Impossible")=0) printf("I'm possiblen");else if(strcmp(str,"ACM")=0) printf("Accept Moren");return 0;空瓶换啤酒Soda SurplerDescriptionTim is an absolutely obsessive soda drinker,he simply cannot get
46、 enough. Most annoyingly though, he almost never has any money, so his only obvious legal way to obtain more soda is to take the 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 t
47、he street.One day he was extra thirsty, so he actually drank sodas until he couldn't aford a new one.InputThree non-negative integers e,f, c, where e < 1000 equals the number of empty soda bottles in Tim's possession at the start of the day, f < 1000 the number of empty soda bottles fo
48、und during the day, and 1 < c < 2000 the number of empty bottles required to buy a new soda.OutputHow many sodas did Tim drink on his extra thirsty day?Sample Input9 0 35 5 2Sample Output49#include<stdio.h>#include<string.h> int main()int e,f,c; int t;int sum;int full,empty; while(
49、scanf("%d%d%d",&e,&f,&c)!=EOF)sum=0;empty=e+f;/空瓶数量while(empty>=c)/空瓶数量可换sum+=empty/c;/换的满瓶empty=empty/c+empty%c;/新的空瓶数量printf("%dn",sum);return 0;统计字符统计字符Description输入一行字符,分别统计其中 英文字母、空格、数字和其他字符的个数。Input输入一个整数 t,表示有几组数据接下来有 t 行,每行字符不超过 10000 个Hint 可能有空格之类的字符Output
50、对于每行字符输出其中1 英文字母(大小写都算)的个数2 数字的个数3 其他字符的个数Sample Input2q2 e2qweqrwwerr232424fwetetg=2342gdsg3.,/-=321Sample Outputcharacter:2 number:2 others:1 character:21 number:14 others:9 #include<stdio.h> #include<string.h> char str10010; int main()int t; int i;intn,on;scanf("%d",&t);
51、getchar();/清除上一个换行符while(t-)gets(str);int l=strlen(str);n=on=0; for(i=0;i<l;i+)if(stri>='0'&&stri<='9') nn+;else if(stri>='A'&&stri<='Z'|stri>='a'&&stri<='z') cn+;else on+;printf("character:%dn",cn
52、); printf("number:%dn",nn); printf("others:%dn",on);return 0;2010 机试热身粽子买三送一,买五送二端午节Description今天是端午节,ECNU 决定请大家吃粽子。恰好,今天超市为了迎合"端午节",推出了"端午大酬宾",即促销活动。严格的买三送一,买五送二。ECNU 想用现有的钱,买最多的粽子,但是他自己又算,所以希望你能帮帮他。Input输入第一行为一个数 N(1<=N<=100),表示测试数据的组数。每组测试数据有两个整数,A,B
53、(0<=A<=1000,0<B<10)表示 ECNU 有 A 元钱,每个粽子价格为 B元钱,超市推出了买 5 个送 2 个,和买 3 个送 1 个的活动。Output输出 ECNU 最多能买到的粽子数量。Sample Input210 322 3Sample Output49Hint:有两组测试数据:对于第一组测试数据:有 10 元钱,粽子 3 元一个,可以买 3 个,但是买 3 送 1,所以最后有 4 个。对于第二组测试数据:有 22 元钱,粽子 3 元一个,可以买 7 个,但是买 5 送 2,所以最后有 9 个。#include<stdio.h>#inc
54、lude<string.h> int main()int n; int a,b; int zn;int num; scanf("%d",&n); while(n-)scanf("%d%d",&a,&b);/输入钱数和粽子单价zn=a/b;/买了 zn 个num=zn;if(zn/5!=0)num+=zn/5*2; zn=zn%5;if(zn/3!=0)num+=zn/3;printf("%dn",num);return 0;工程流水线问题工程DescriptionCastor 在 ECNU 工厂工作
55、。总厂有一条生产线,现在生产流水线上排队的零件总数为 M。当前 Castor 开始加工第一个零件。流水线上的零件总是按顺序加工的。例如零件 i 必须是在零件 i+1 之前加工.现在 Castor 只需要再加工 K(K<=M)个零件就能休息了,Castor 想知道他还要工作多长时间才能休息.Input第一行为一个整数 T,表示测数数据的组数.对每组测试数据第一行有两个整数 M,K(1<=K<=M<=1000)然后一行有 M 个数ti(1<=ti<=10000)Outputi 个数字表示零件队列的第 i 个零件需要加工的时间为每组数据输出一行,每行只有一个整数表
56、示 Castor 还需要工作多长时间Sample Input23 25 2 33 11 2 3Sample Output71#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main()int T; int M,K; int i;int t1005; int sum;scanf("%d",&T); while(T-)sum=0; scanf("%d%d",&M,&K); f
57、or(i=0;i<M;i+)scanf("%d",&ti); for(i=0;i<K;i+)sum+=ti;printf("%dn",sum);return 0;2011 机试hello worldHello World!Description当开始学习程序语言,第一个程序肯定是在屏幕上输出一些字符,比如输出”Hello World!”。遇到输出的句子过长时,输出的句子由于换行将被屏幕截断。现在给你一些文本,文本的文法如下:TEXT(文本):= SENTENCE | SENTENCE SPACE TEXT SENTENCE(句子):=
58、 WORD SPACE SENTENCE | WORD END END(结束符):= '.', '?', '!'WORD(单词):= LETTER | LETTER WORD LETTER(字母):= 'a'.'z', 'A'.'Z'SPACE(空格):= ' '你的任务是把满足上述文法的文本分割成多行(每行文本的长度都不超过n)。并且满足如下条件:一、 输出的句子不能被截断。如:”Hi! 被截断,即不合法。二、 文本分割后保证行数最小。如:”Hi!度要求在n=20的情况下,可以分割为:”Hi!”“e to ECNU.”若被分割成”Hi!e”则认为e to ECNU. Have a nice day!”在每行文本长e to ECNU.”“Have a nice day!”,也可以被分割为:”Hi!e to ECNU.”Have a nice day!”此时认为
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 商户租房合同协议书2026年方法论
- 2026年关于轮胎安全培训内容高频考点
- 2026年法治安全培训内容核心要点
- 2026年线上观摩工作总结报告实操要点
- 2026年快餐门店运营管理合同
- 2026年食堂生产安全培训内容重点
- 福州市闽清县2025-2026学年第二学期四年级语文第六单元测试卷(部编版含答案)
- 朝阳市建平县2025-2026学年第二学期六年级语文第五单元测试卷部编版含答案
- 邵阳市武冈市2025-2026学年第二学期六年级语文第五单元测试卷部编版含答案
- 大庆市大同区2025-2026学年第二学期五年级语文第五单元测试卷(部编版含答案)
- 2026年济南历下区九年级中考数学一模考试试题(含答案)
- 2026新质生产力人才发展报告-
- 大脑卒中急救处理方案
- 广东省化工(危险化学品)企业安全隐患排查指导手册(精细化工企业专篇)
- 地铁工程扬尘防治专项施工方案
- 2026吉林大学第二医院合同制护士招聘50人考试参考试题及答案解析
- 2026年课件湘少版四年级英语下册全套测试卷-合集
- 急危重症患者的病情评估和护理
- 痛风PPT精品课件
- 宋浩线性代数笔记
- 缅甸土瓦锡、钨沙矿考察情况
评论
0/150
提交评论