您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 创业/孵化 > 蓝桥杯经典例题(Java语言实现)
1.Fibonacci数列·importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannersca=newScanner(System.in);inti=sca.nextInt();int[]s=newint[i+1];s[1]=s[2]=1;for(intj=3;j=i;j++){s[j]=(s[j-1]+s[j-2])%10007;}System.out.println(s[i]);}}2.圆的面积importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannersca=newScanner(System.in);intr=sca.nextInt();doublePI=Math.PI;doubleS=PI*r*r;Stringresult=String.format(%.7f,S);//四舍五入System.out.println(result);result=String.format(%.8f,S).substring(0,(S+).indexOf(.)+8);//不进行四舍五入,现取9位小数,再截取8位System.out.println(result);}}3.序列求和importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannersca=newScanner(System.in);longn=sca.nextInt();longresult=(1+n)*n/2;System.out.println(result);}}递归实现:importjava.util.Scanner;publicclassMain{staticintsum=0;publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannersca=newScanner(System.in);intn=sca.nextInt();System.out.println(f(n));}privatestaticintf(intn){//TODOAuto-generatedmethodstubif(n==1)return1;returnn+f(n-1);}}4.闰年判断当以下情况之一满足时,这一年是闰年:1.年份是4的倍数而不是100的倍数;2.年份是400的倍数。其他的年份都不是闰年。样例输入2013样例输出no样例输入2016样例输出yes数据规模与约定1990=y=2050。importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannersc=newScanner(System.in);intyear=sc.nextInt();if(year%4==0&&year%100!=0)System.out.println(yes);elseif(year%400==0)System.out.println(yes);elseSystem.out.println(no);}}5.01字串importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubfor(inti=0;i2;i++){for(intj=0;j2;j++){for(intk=0;k2;k++){for(intl=0;l2;l++){for(intm=0;m2;m++){StringBufferstr=newStringBuffer();System.out.println(str.append(i).append(j).append(k).append(l).append(m));}}}}}}}6.字母图形importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);intn=sc.nextInt();intm=sc.nextInt();char[]cha=newchar[m+1];for(inti=1;i=m;i++){cha[i]=(char)('A'+i-1);}for(inti=1;i=n;i++){for(intj=1;j=m;j++){System.out.print(cha[j]);if(i=j){cha[j]=(char)(cha[j]+1);}else{cha[j]=(char)(cha[j]-1);}}System.out.println();}}}6.数列特征importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannersc=newScanner(System.in);intn=sc.nextInt();int[]a=newint[n];for(inti=0;ia.length;i++){a[i]=sc.nextInt();}java.util.Arrays.sort(a);System.out.println(a[n-1]);System.out.println(a[0]);intsum=0;for(inti=0;ia.length;i++){sum+=a[i];}System.out.println(sum);}}7.查找整数importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannersc=newScanner(System.in);intn=sc.nextInt();int[]a=newint[n];for(inti=0;ia.length;i++){a[i]=sc.nextInt();}intkey=sc.nextInt();intjie=-2;for(inti=0;ia.length;i++){if(key==a[i]){jie=i;break;}}System.out.println(jie+1)}8.杨辉三角importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannersc=newScanner(System.in);intn=sc.nextInt();longt1=System.currentTimeMillis();int[][]a=newint[n][n];for(inti=0;in;i++){for(intj=0;ji+1;j++){if(i==0||i==j||j==0)a[i][j]=1;elsea[i][j]=a[i-1][j]+a[i-1][j-1];System.out.print(a[i][j]+);}System.out.println();}longt2=System.currentTimeMillis();}}9.特殊的数字importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstublongt1=System.currentTimeMillis();for(intn=100;n999;n++){Stringstr=String.valueOf(n);char[]ch=str.toCharArray();int[]in=newint[ch.length];for(inti=0;i3;i++){in[i]=Integer.parseInt(ch[i]+);}if(in[0]*in[0]*in[0]+in[1]*in[1]*in[1]+in[2]*in[2]*in[2]==n){System.out.println(n);}}longt2=System.currentTimeMillis();//System.out.println(t2-t1);}}10.回文数importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstublongt1=System.currentTimeMillis();for(inti=1;i=9;i++){for(intj=0;j=9;j++){StringBufferstr=newStringBuffer();str=str.append(i).append(j).append(j).append(i);System.out.println(str);}}longt2=System.currentTimeMillis();//System.out.println(t2-t1);}}11.特殊回文数importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){//TODOAuto-generatedmethodstubScannersca=newScanner(System.in);intin=sca.nextInt();for(inti=1;i10;i++){for(intj=0;j10;j++){for(intk=0;k10;k++){if(i*2+j*2+k==in){System.out.println(i*10000+j*1000+k*100+j*10+i);}}}}for(inti=1;i10;i++){for(intj=0;j10;j++){for(intk=0;k10;k++){if(i*2+j*2+k*2==in){System.out.println(i*100000+j*10000+k*1000+k*100+j*10+i);}}}}}}12.十进制转十六进制importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannersca=newScanner(System.in);intin=sca.nextInt();Stringstr=Integer.toHexString(in).toUpperCase();System.out.println(str);StringBuffers=newStringBuffer();while(in0){intt=in%16;switch(t){case0:case1:case2:case3:case4:case5:case6:case7:case8:case9:s.append(t+);break;case10:s.append('A');break;case11:s.append('B');br
本文标题:蓝桥杯经典例题(Java语言实现)
链接地址:https://www.777doc.com/doc-5063679 .html