1、2023机试2计算和的数位2大写改小写3素数对4求最大公约数和最小公倍数6排序后求位置处的数7*路由器连接8*编译原理10*分开连接132023机试17ECNU的含义17空瓶换啤酒18记录字符202023机试热身21粽子买三送一,买五送二21工程流水线问题222023机试24hello world24Special judge26查询成绩282023机试热身30贪吃蛇30仰望星空34*编辑距离362023机试38字母排序38幸运数39十六进制的加法42电话号码簿合并排序42*五子棋43*正则表达式匹配452023机试46斐波那契数列的素数个数46*将a字符变成b字符最少修改次数472023机试
2、热身49去重排序49蛇形图案51数学手稿542023机试计算和的数位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=). Output For each test case, print the number of digits of a + b. Sample Input 35 71 991000 999 Sample Output 234 #includeint main()int n;int a,b;int sum;while(scanf(%d,&n)!=EOF)while(n-)int an=0;scanf(%d%d,&a,&b);sum=a+b;while(sum)an+;sum/=10;printf(%dn,an+);return 0;大写改小写
4、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 converted text. Sample Input welcome to east china normal university. Sample Output
5、 WELCOME TO EAST CHINA NORMAL UNIVERSITY. #include#includechar str1000;int main()int l;while(gets(str)l=strlen(str);int i;for(i=0;i=a&stri=z)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
6、 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. 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
7、 follow.Each test case consists of an integer N in one line. Output For each test case, output P . Sample Input 414751 Sample Output 0226 #include#includebool 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之外的偶数
8、都不是素数for(i=3;i=10005;i+=2)if(primei=true)/是素数j=i+i;while(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,sum);r
9、eturn 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 contains two inter
10、ger 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 #includeint getgcd(int a,int b)int gcd;int t1,t2;t1=a;t2=b;gcd=t1%t2;while(gcd!=0)t1=t2;t2=gcd;gcd=t1%t2;retu
11、rn 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 data in the order from the least up to the greatest element,then do the query
12、: 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. At first, a database is written, and then theres a sequence of queries. The format of dat
13、abase is very simple: in the first line theres 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 K (1 = K = 100) is written, and
14、 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. In each line there should be an answer to the corresponding query. The answer to the query i is an element from the database,
15、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#includeusing 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
16、;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 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 oth
17、er using cables. Since each worker of the 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
18、 hub connection, that the cost is minimal. 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 - th
19、e number of hubs in the network (2 = N = 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 conn
20、ect them. cost is a positive 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#includeusing namespace std;struct Edgei
21、nt 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.costb.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,Cm
22、p);/排序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 Principles of Compiler,partychen thought that he can solve a simple expression problem.So he
23、 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 first 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 descr
24、ibed 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 GoodGoodBad #include #include #include #include #include #include #include #include #include #include #include using namespace std;char ex1
25、10;int index;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() return A()&C();bool C() while(exindex=+) index+; while(exin
26、dex= ) 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?Good:Bad); return 0;*分开连接Separate Connections Description Partychen are analyzing a communications network with at most 18 nodes. Character
27、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 the maximum number of nodes that can communicate simultaneously. If node i is communicating with node j then node j is
28、 communicating with node i. Input The 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. Output For each test case , output the maximum number of nodes th
29、at can communicate simultaneously Sample Input 25NYYYYYNNNNYNNNNYNNNNYNNNN5NYYYYYNNNNYNNNYYNNNYYNYYN Sample Output 24HintThe 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,
30、 we can let node 0 communicate with node 1, and node 3 communicate with node 4. #include #include #include #include #include #include #include #include #include #include #include using namespace std;#define MAXN 250#define MAXE MAXN*MAXN*2#define SET(a,b) memset(a,b,sizeof(a)deque Q;bool gMAXNMAXN,i
31、nqueMAXN,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 anc) while(u!=anc) int v=matchu; inblossombaseu=1; inb
32、lossombasev=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(inblossombasei) basei=anc; if(!inquei) Q.push_back(i); inquei=1; bool
33、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!=-1&prematchv!=-1)contract(u,v,n); else if(prev=-1) prev=u; if(matchv!=-1)Q.push_b
34、ack(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&dfs(i,n) ans+; return ans;int main() int ans; int n,m; char tmp30; scanf(%d,&n); while(n-) ans=0; m
35、emset(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); printf(%dn,ans*2); return 0;2023机试ECNU的含义Welcome to 2023 ACM selective trial Description Welcome to 2023 ACM selective trial. ACM is a long way to go, and its not just a ma
36、tch. 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 represents Nice, U represents Ultimate. Second you should re
37、member Impossible is nothing, because Impossible represents Im 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,Impossible orACM, 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 orACM. Output Tell me what does it means.Sample Input 3EImpossibleACM Sample Output ExcellentIm possibleAccept More #include#inclu
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100