当前位置:文档之家› 华东师范大学计算机机试真题

华东师范大学计算机机试真题

2009机试 (2)计算和的数位 (2)大写改小写 (3)素数对 (4)求最大公约数和最小公倍数 (6)排序后求位置处的数 (7)*路由器连接 (8)*编译原理 (10)*分开连接 (13)2010机试 (17)ECNU的含义 (17)空瓶换啤酒 (18)统计字符 (20)2010机试热身 (21)粽子买三送一,买五送二 (21)工程流水线问题 (22)2011机试 (24)hello world (24)Special judge (26)查询成绩 (28)2011机试热身 (30)贪吃蛇 (30)仰望星空 (34)*编辑距离 (36)2012机试 (38)字母排序 (38)幸运数 (39)十六进制的加法 (42)电话号码簿合并排序 (42)*五子棋 (43)*正则表达式匹配 (45)2013机试 (46)斐波那契数列的素数个数 (46)*将a字符变成b字符最少修改次数 (47)2013机试热身 (49)去重排序 (49)蛇形图案 (51)数学手稿 (54)2009机试计算和的数位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 by a space in a line. (0<=a,b<=100000000).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,&b);sum=a+b;while(sum){an++;sum/=10;}printf("%d\n",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 converted text.Sample Inputwelcome to east china normal university.Sample OutputWELCOME TO EAST CHINA NORMAL UNIVERSITY.#include<stdio.h>#include<string.h>char str[1000];int main(){int l;while(gets(str)){l=strlen(str);int i;for(i=0;i<l;i++){if(str[i]>='a'&&str[i]<='z')printf("%c",str[i]-32);elseprintf("%c",str[i]);}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. Your task is to compute the number of pairs P such that both numbers in the pairs are prime.InputThe 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.OutputFor each test case, output P .Sample Input414751Sample Output226#include<stdio.h>#include<string.h>bool prime[10005];void init(){int i;int j;prime[0]=prime[1]=false;//不是素数prime[2]=true;//是素数for(i=3;i<=10005;i+=2){prime[i]=true;//是素数prime[i+1]=false;//不是素数除0和2之外的偶数都不是素数}for(i=3;i<=10005;i+=2){if(prime[i]==true)//是素数{j=i+i;while(j<=10005){prime[j]=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(prime[i]==true&&prime[n+1-i]==true)sum++;}sum*=2;if(n%2==1)//n为奇数{if(prime[n/2+1]==true)sum+=1;printf("%d\n",sum);}}return 0;}求最大公约数和最小公倍数GCD and LCMDescriptionWrite a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b (0 < a, b ≤44000).InputThe 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 by a single space in a line.OutputFor each test case, print GCD and LCM separated by a single space in a line.Sample Input28 65000 3000Sample Output2 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){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 %d\n",getgcd(a,b),a*b/(getgcd(a,b)));}}return 0;}排序后求位置处的数Sort it…DescriptionThere is a database,partychen want you to sort the database’s data 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.InputThe standard input of the problem consists of two parts. At 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 written 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.OutputThe 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, which is i-th by its value (in the order from the least up to the greatest element).Sample Input512112371213325Sample Output1217123#include<stdio.h>#include<algorithm>using namespace std;int num[100010];int pos[105];int main(){int n;int i;int k;while(scanf("%d",&n)!=EOF){for(i=1;i<=n;i++)scanf("%d",&num[i]);scanf("%d",&k);for(i=1;i<=k;i++)scanf("%d",&pos[i]);sort(num+1,num+1+n);for(i=1;i<=k;i++)printf("%d\n",num[pos[i]]);}return 0;}*路由器连接Hub Connection planDescriptionPartychen 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 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. 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.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 <= 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 positive integer number that does not exceed 106. There will always be at least one way to connect all hubs.OutputOutput the minimize cost of your hub connection plan.Sample Input4 61 2 11 3 11 4 22 3 13 4 12 4 1Sample Output3#include<stdio.h>#include<algorithm>using namespace std;struct Edge{int a,b;int cost;}E[15010];int Tree[1010];int findRoot(int x){if(Tree[x]==-1)return x;else{int tmp=findRoot(Tree[x]);Tree[x]=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",&E[i].a,&E[i].b,&E[i].cost); sort(E+1,E+1+m,Cmp);//排序for(i=1;i<=n;i++)Tree[i]=-1;int ans=0;for(i=1;i<=m;i++){int a=findRoot(E[i].a);int b=findRoot(E[i].b);if(a!=b){Tree[a]=b;ans+=E[i].cost;}}printf("%d\n",ans);}return 0;}*编译原理Principles of CompilerDescriptionAfter learnt the Principles 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?InputThe 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 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 OutputGoodGoodBad#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 ex[110];int index;bool A();bool B();bool C();bool A(){if(ex[index]=='x'){index++;while(ex[index]==' ') index++;return true;}if(ex[index]=='('){index++;while(ex[index]==' ') index++;if(B()&&ex[index]==')'){index++;while(ex[index]==' ') index++;return true;}}return false;}bool B(){return A()&&C();}bool C(){while(ex[index]=='+'){index++;while(ex[index]==' ') index++;//return A();if (!A())return false;}return true;}int main(){int N;scanf("%d",&N);getchar();while(N--){gets(ex);index=0;printf("%s\n",A()&&ex[index]=='\0'?"Good":"Bad");}return 0;}*分开连接Separate ConnectionsDescriptionPartychen are analyzing a communications network with at most 18 nodes. Character in a matrix i,j (i,j both 0-based,as matrix[i][j]) 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 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 each test case , output the maximum number of nodes that can communicate simultaneouslySample Input25NYYYYYNNNNYNNNNYNNNNYNNNN5NYYYYYNNNNYNNNYYNNNYYNYYNSample Output2HintThe 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 <cstring>#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) memset(a,b,sizeof(a))deque<int> Q;bool g[MAXN][MAXN],inque[MAXN],inblossom[MAXN];int match[MAXN],pre[MAXN],base[MAXN];int findancestor(int u,int v){bool inpath[MAXN]= {false};while(1){u=base[u];inpath[u]=true;if(match[u]==-1)break;u=pre[match[u]];}while(1){v=base[v];if(inpath[v])return v;v=pre[match[v]];}void reset(int u,int anc){while(u!=anc){int v=match[u];inblossom[base[u]]=1;inblossom[base[v]]=1;v=pre[v];if(base[v]!=anc)pre[v]=match[u];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(base[u]!=anc)pre[u]=v;if(base[v]!=anc)pre[v]=u;for(int i=1; i<=n; i++)if(inblossom[base[i]]){base[i]=anc;if(!inque[i]){Q.push_back(i);inque[i]=1;}}}bool dfs(int S,int n){for(int i=0; i<=n; i++)pre[i]=-1,inque[i]=0,base[i]=i;Q.clear();Q.push_back(S);inque[S]=1;while(!Q.empty()){int u=Q.front();Q.pop_front();for(int v=1; v<=n; v++){if(g[u][v]&&base[v]!=base[u]&&match[u]!=v){if(v==S||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n);else if(pre[v]==-1){pre[v]=u;if(match[v]!=-1)Q.push_back(match[v]),inque[match[v]]=1;else{u=v;while(u!=-1){v=pre[u];int w=match[v];match[u]=v;match[v]=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(match[i]==-1&&dfs(i,n))ans++;return ans;}int main(){int ans;int n,m;char tmp[30];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(tmp[j]=='Y'){g[i][j]=g[j][i]=1;}}}ans=solve(m);printf("%d\n",ans*2);}return 0;}2010机试ECNU的含义Welcome to 2009 ACM selective trialDescriptionWelcome 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 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, 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","Impossible" or"ACM", you need to tell me what does it means?InputThe 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"ACM".OutputTell me what does it means.Sample Input3EImpossibleACMSample OutputExcellentI'm possibleAccept More#include<stdio.h>#include<string.h>char str[20];int main(){int N;scanf("%d",&N);while(N--){scanf("%s",str);if(strcmp(str,"E")==0)printf("Excellent\n");else if(strcmp(str,"C")==0)printf("Cheer\n");else if(strcmp(str,"N")==0)printf("Nice\n");else if(strcmp(str,"U")==0)printf("Ultimate\n");else if(strcmp(str,"Impossible")==0) printf("I'm possible\n");else if(strcmp(str,"ACM")==0) printf("Accept More\n");}return 0;}空瓶换啤酒Soda SurplerDescriptionTim is an absolutely obsessive soda drinker,he simply cannot get 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 the 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 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.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(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("%d\n",sum);}return 0;}统计字符统计字符Description输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。

相关主题