您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > CCF认证模拟考试题目参考答案(Java)
模拟练习试题参考答案(Java)为了帮助大家熟悉CCF软件能力认证考试的操作方式与答题环境,了解试题的大致难度,做好考前的准备,故在此提供试题的参考答案。Java程序是灵活的,为了解决同一个问题,即使结果相同,程序的内容也不一定是完全一致的,仅供各位在练习时参考。1.出现次数最多的数importjava.util.*;publicclassMain{publicstaticvoidmain(String[]args){newMain().run();}publicvoidrun(){Scannerfin=newScanner(System.in);intN=fin.nextInt();int[]count=newint[10001];for(inti=0;iN;++i){++count[fin.nextInt()];}intmaxCount=-1;intresult=0;for(inti=1;i=10000;++i){if(count[i]maxCount){maxCount=count[i];result=i;}}System.out.println(result);}}2.ISBN号码importjava.io.BufferedReader;importjava.io.InputStreamReader;publicclassMain{publicstaticvoidmain(Stringargs[]){BufferedReaderbin=newBufferedReader(newInputStreamReader(System.in));try{intsum=0;charcc='0';Stringisbn_0=bin.readLine();Stringisbn=isbn_0.replace(-,);for(inti=0;i9;i++){intii=(int)isbn.charAt(i)-48;sum+=ii*(i+1);}sum=sum%11;if(sum==10)cc='X';//elsecc=(char)(sum+48);if(cc==isbn.charAt(9))System.out.println(Right);else{isbn_0=isbn_0.substring(0,12)+cc;System.out.println(isbn_0);}}catch(Exceptione){e.printStackTrace();}}}3.最大的矩形importjava.util.*;publicclassMain{publicstaticvoidmain(String[]args){newMain().run();}publicvoidrun(){Scannerfin=newScanner(System.in);intN=fin.nextInt();int[]height=newint[N];for(inti=0;iN;++i)height[i]=fin.nextInt();intresult=0;for(inti=0;iN;++i){intwidth=1;for(intj=i-1;j=0;--j){if(height[j]height[i])break;++width;}for(intj=i+1;jN;++j){if(height[j]height[i])break;++width;}intarea=width*height[i];result=Math.max(result,area);}System.out.println(result);}}4.有趣的数importjava.util.*;publicclassMain{publicstaticvoidmain(String[]args){newMain().run();}publicvoidrun(){Scannerfin=newScanner(System.in);intN=fin.nextInt();long[]count=newlong[8];count[6]=0;count[7]=1;longmod=1000000007;for(inti=2;i=N;++i){long[]newCount=newlong[8];newCount[0]=(count[0]*2+count[1]+count[3])%mod;newCount[1]=(count[1]*2+count[2]+count[5])%mod;newCount[2]=(count[2]+count[6])%mod;newCount[3]=(count[3]*2+count[4]+count[5])%mod;newCount[4]=(count[4]+count[7])%mod;newCount[5]=(count[5]*2+count[6]+count[7])%mod;newCount[6]=0;newCount[7]=1;count=newCount;}System.out.println(count[0]);}}5.I’mstuck!importjava.util.*;publicclassMain{publicstaticvoidmain(String[]args){newMain().run();}publicvoidrun(){Scannerfin=newScanner(System.in);intR=fin.nextInt();intC=fin.nextInt();fin.nextLine();int[][]board=newint[R+2][C+2];introwStart=0,colStart=0,rowEnd=0,colEnd=0;for(inti=1;i=R;++i){Stringline=fin.nextLine();for(intj=1;j=C;++j){charc=line.charAt(j-1);switch(c){case'#':break;case'-':board[i][j]=5;break;case'|':board[i][j]=0xA;break;case'+':case'S':case'T':board[i][j]=0xF;break;case'.':board[i][j]=0x8;break;default:break;}if(c=='S'){rowStart=i;colStart=j;}elseif(c=='T'){rowEnd=i;colEnd=j;}}}int[]dr=newint[]{0,-1,0,1};int[]dc=newint[]{1,0,-1,0};//Scan1:findallcellswhichcanreachTboolean[][]visited=newboolean[R+2][C+2];boolean[][]canReachT=newboolean[R+2][C+2];initVisited(visited);canReachT[rowEnd][colEnd]=true;visited[rowEnd][colEnd]=true;QueueIntegerqueue=newLinkedListInteger();queue.add(rowEnd);queue.add(colEnd);while(!queue.isEmpty()){intr=queue.remove();intc=queue.remove();for(inti=0;i4;++i){intnr=r+dr[i];intnc=c+dc[i];if(visited[nr][nc])continue;if((board[nr][nc]&(1((i+2)%4)))!=0){canReachT[nr][nc]=true;queue.add(nr);queue.add(nc);visited[nr][nc]=true;}}}/*for(inti=1;i=R;++i){for(intj=1;j=C;++j){if(canReachT[i][j]){System.out.println(i=+i+,j=+j);}}}*/if(!canReachT[rowStart][colStart]){System.out.println(I'mstuck!);return;}//Scan2:getresultboolean[][]rCanReach=newboolean[R+2][C+2];initVisited(visited);queue.clear();visited[rowStart][colStart]=true;rCanReach[rowStart][colStart]=true;queue.add(rowStart);queue.add(colStart);while(!queue.isEmpty()){intr=queue.remove();intc=queue.remove();for(inti=0;i4;++i){if((board[r][c]&(1i))==0)continue;intnr=r+dr[i];intnc=c+dc[i];if(visited[nr][nc])continue;if(board[nr][nc]==0)continue;rCanReach[nr][nc]=true;queue.add(nr);queue.add(nc);visited[nr][nc]=true;}}intresult=0;for(inti=1;i=R;++i){for(intj=1;j=C;++j){/*if(rCanReach[i][j]){System.out.println(i=+i+,j=+j);}*/if(rCanReach[i][j]&&(!canReachT[i][j]))++result;}}System.out.println(result);}privatevoidinitVisited(boolean[][]visited){intR=visited.length-2;intC=visited[0].length-2;for(inti=0;i=R+1;++i){visited[i][0]=true;visited[i][C+1]=true;}for(intj=0;j=C+1;++j){visited[0][j]=true;visited[R+1][j]=true;}for(inti=1;i=R;++i){for(intj=1;j=C;++j){visited[i][j]=false;}}}}
本文标题:CCF认证模拟考试题目参考答案(Java)
链接地址:https://www.777doc.com/doc-6469968 .html