蓝桥杯练习
大部分程序来自查阅网络资源总结。
1.阶乘计算
package Chapter02;
import java.math.BigInteger;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
BigInteger n = BigInteger.valueOf(1);
for (int i = 1; i <=m; i++) {
n = n.multiply(BigInteger.valueOf(i));
}
System.out.println(n);
}
}
2.高精度加法
package Chapter02;
import java.math.BigInteger;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
BigInteger a = scanner.nextBigInteger();
BigInteger b = scanner.nextBigInteger();
BigInteger c= a.add(b);
System.out.println(c);
}
}
3.Huffuman树
package Chapter02;
import java.util.Arrays;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
scanner.close();
int sum = 0;
for (int j = 0; j < n - 1; j++) {
Arrays.sort(arr);
arr[j] = arr[j]+arr[j+1];
sum = sum+arr[j];
for (int i = j+2; i < n; i++) {
arr[i-1] = arr[i];
}
arr[n-1] = 0;
}
System.out.println(sum);
}
}
4.报时助手
package Chapter02;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int h = scanner.nextInt();
int m = scanner.nextInt();
String[] time = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
String[] time2 = {" "," ","twenty","thirty","forty","fifty"};
if (h>20){
int b = h%10;
System.out.print("twenty"+" "+time[b]+" ");
}else if (h==20){
System.out.print("twenty"+" ");
}else {
System.out.print(time[h]+" ");
}
if (m>20){
int a = m/10;
int b = m%10;
System.out.print(time2[a]+" "+time[b]);
}else if (m==20){
System.out.print("twenty");
}else if (m>0 && m<20){
System.out.print(time[m]);
}else if (m==0){
System.out.print("o'clock");
}
}
}
5.龟兔赛跑预测
package Chapter02;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int v1 = scanner.nextInt();
int v2 = scanner.nextInt();
int t = scanner.nextInt();
int s = scanner.nextInt();
int l = scanner.nextInt();
scanner.close();
int r1 = 0;
int t1 = 0;
int time = 0;
boolean flag = false;
while (true){
time++;
r1 +=v1;
t1 +=v2;
if (r1>=l || t1>=l) {
break;
}
if (r1-t1 >=t){
for (int i = 0; i < s; i++) {
t1 +=v2;
time++;
if (t1>=l){
flag=true;
break;
}
}
if (flag)
break;
}
}
if (r1>t1) {
System.out.println("R");
}else if (r1<t1){
System.out.println("T");
}else {
System.out.println("D");
}
System.out.println(time);
}
}
6.芯片测试
package Chapter02;
import java.util.Scanner;
public class Demo06 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] a = new int[n][n];
int[] b = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = scanner.nextInt();
if (a[i][j]==0){
b[j] = b[j]+1;
}
}
}
for (int i = 0; i < n; i++) {
if (b[i]<=n/2){
System.out.print(i+1+" ");
}
}
}
}
7.FJ的字符串
package Chapter02;
import java.util.Scanner;
public class Demo07 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n =scanner.nextInt();
System.out.println(FJstr(n));
}
public static String FJstr(int num){
if (num==1){
return "A";
}
return FJstr(num-1)+(char)('A'+(num-1))+FJstr(num-1);
}
}
8.Sine之舞
package Chapter02;
import java.util.Scanner;
public class Demo08 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
System.out.println(SN(n));
}
private static String SN(int n){
if(n==1){
return AN(1,1)+"+"+n;
}
else {
return "("+SN(n-1)+")"+AN(1,n)+"+"+(n-(n-1));
}
}
private static String AN(int m,int n){
if (m==n){
return "sin("+n+")";
}
else {
if (m%2!=0){
return "sin("+m+"-"+AN(m+1,n)+")";
}else {
return "sin("+m+"+"+AN(m+1,n)+")";
}
}
}
}
9.字符串对比
package Chapter02;
import java.util.Scanner;
public class Demo09 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str1 = scanner.next();
String str2 = scanner.next();
if (str1.length() != str2.length()){
System.out.println(1);
}else {
if (str1.equals(str2)){
System.out.println(2);
}else if (str1.equalsIgnoreCase(str2)){
System.out.println(3);
}else {
System.out.println(4);
}
}
}
}
10.数的读法
package Chapter02;
import java.util.Scanner;
public class Demo10 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String str = scanner.next();
scanner.close();
String[] num = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
String[] unit = {"","","shi","bai","qian","wan","shi","bai","qian","yi","shi"};
int n = str.length();
for (int i = 0; i < n; i++) {
int x = str.charAt(i)-'0';
if (x==0){
if ((n-i)==5 || (n-i)==9){
System.out.print(String.format("%s ",unit[n-i]));
}
if ((n-i)!=1 && str.charAt(i+1)!='0'){
System.out.print(String.format("%s ","ling"));
}
}
else if ((n==2 || n==6 || n==10) && x==1 && i==0) {
System.out.print(String.format("%s ","shi"));
}
else {
System.out.print(String.format("%s %s ",num[x],unit[n-i]));
}
}
}
}
11.时间转换
package Chapter02;
import java.util.Scanner;
public class Demo11 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t =scanner.nextInt();
int h = t/3600;
int m = t%3600/60;
int s = t%3600%60;
System.out.println(h+":"+m+":"+s);
}
}
12.矩形面积交
package Chapter02;
import java.util.Scanner;
public class Demo12 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
double x3 = scanner.nextDouble();
double y3 = scanner.nextDouble();
double x4 = scanner.nextDouble();
double y4 = scanner.nextDouble();
double width = 0,high = 0,sum = 0;
width = Math.min(Math.max(x1,x2),Math.max(x3,x4))-Math.max(Math.min(x1,x2),Math.min(x3,x4));
high = Math.min(Math.max(y1,y2),Math.max(y3,y4))-Math.max(Math.min(y1,y2),Math.min(y3,y4));
sum = width*high;
if (width<=0 || high<=0){
System.out.println(String.format("%.2f",0.00));
}else {
System.out.println(String.format("%.2f",sum));
}
}
}
13.矩阵乘法
package Chapter02;
import java.util.Scanner;
public class Demo13 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] arr = new int[n][n];
int[][] arr2 = new int[n][n];
int[][] arr3 = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr2[i][j]=arr[i][j] = scanner.nextInt();
arr3[i][j] = 0;
}
}
scanner.close();
if (m==0){
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i==j){
System.out.print(1+" ");
}else {
System.out.print(0+" ");
}
}
System.out.println();
}
}
else {
for (int k = 1; k < m; k++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int w = 0;
for (int l = 0; l < n; l++) {
w += arr[i][l]*arr2[l][j];
}
arr3[i][j] = w;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = arr3[i][j];
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
}
14.回形取数
package Chapter02;
import java.util.Scanner;
public class Demo14 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int[][] arr = new int[m][n];
for (int i = 0; i <m ; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = scanner.nextInt();
}
}
scanner.close();
int sum = 0;
int i=0,j=0;
int[][] flag = new int[m][n];
while (sum<n*m){
while (i<m && flag[i][j]!=1){
System.out.print(arr[i][j]+" ");
flag[i][j] = 1;
i++;sum++;
}
i--;j++;
while (j<n && flag[i][j]!=1){
System.out.print(arr[i][j]+" ");
flag[i][j] = 1;
j++;sum++;
}
j--;i--;
while (i>=0 && flag[i][j]!=1){
System.out.print(arr[i][j]+" ");
flag[i][j] = 1;
i--;sum++;
}
i++;j--;
while (j>=0 && flag[i][j]!=1){
System.out.print(arr[i][j]+" ");
flag[i][j] = 1;
j--;sum++;
}
j++;i++;
}
}
}
15.完美的代价
package Chapter02;
import java.util.Scanner;
public class Demo15 {
private static int count=0;
private static boolean haveOnlyOneMiddle=true;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String s = sc.next();
char[] charArr = s.toCharArray();
if(palindrome(charArr,0,N-1)) {
System.out.println(count);
}
else {
System.out.println("Impossible");
}
}
private static boolean palindrome(char[] charArr, int a, int b) {
if(a>=b) {
return true;
}
for(int i=b;i>a;i--) {
if(charArr[a]==charArr[i]) {
exchangeTo(charArr,i,b);
count+=b-i;
return palindrome(charArr,a+1,b-1);
}
}
if(haveOnlyOneMiddle) {
haveOnlyOneMiddle=false;
count+=charArr.length/2-a;
return palindrome(charArr,a+1,b);
}
return false;
}
private static void exchangeTo(char[] charArr, int i, int b) {
char temp;
for(int k=i;k<b;k++) {
temp=charArr[k];
charArr[k]=charArr[k+1];
charArr[k+1]=temp;
}
}
}
16.印章
package Chapter01;
import java.util.Scanner;
public class Demo01 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
double p = 1.0/n;
double[][] arr = new double[m+1][n+1];
for (int i = 1; i <=m; i++) {
for (int j = 1; j<=n ; j++) {
if (i<j){
arr[i][j] = 0;
}else if (j==1){
arr[i][j] = Math.pow(p,i-1);
}else {
arr[i][j] = arr[i-1][j]*(j*p)+arr[i-1][j-1]*((n-(j-1))*p);
}
}
}
System.out.println(String.format("%.4f",arr[m][n]));
}
}
17.拿金币
package Chapter01;
import java.util.Scanner;
public class Demo02 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[][] arr = new int[n+1][n+1];
for (int i = 1; i <=n ; i++) {
for (int j = 1; j <=n ; j++) {
arr[i][j] = scanner.nextInt();
}
}
scanner.close();
int[][] dp = new int[n+1][n+1];
dp[1][1] = arr[1][1];
for (int i = 1; i <=n ; i++) {
for (int j = 1; j <=n ; j++) {
dp[i][j] = Math.max(dp[i-1][j],dp[i][j-1])+arr[i][j];
}
}
System.out.println(dp[n][n]);
}
}
18.kAc给糖果你吃
package Chapter01;
import java.util.Arrays;
import java.util.Scanner;
public class Demo03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[] A = new int[n];
for (int i = 0; i < n; i++) {
A[i] = scanner.nextInt();
}
scanner.close();
Arrays.sort(A);
long sum = 0;
for (int i = n-1; i >n-1-m; i--) {
sum += A[i];
}
System.out.println(sum);
}
}
19.最大分解
package Chapter01;
import java.util.Scanner;
public class Demo04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
for (int i = n-1; i >0 ; i--) {
if (n%i==0){
sum += i;
n=i;
}
}
System.out.println(sum);
}
}
20.藏匿的刺客
package Chapter01;
import java.util.Arrays;
import java.util.Scanner;
public class Demo05 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
person[] person = new person[n];
for (int i = 0; i <n; i++) {
person[i] = new person(scanner.nextInt(),scanner.nextInt(),1);
}
scanner.close();
Arrays.sort(person);
int max = person[n-1].N;
int[] axis = new int[max+1];
for (int i = 0; i < n; i++) {
int s = person[i].M;
int t = person[i].N;
int count = sum(axis,s,t);
person[i].c -= count;
while (person[i].c>0){
if (axis[t]==0){
axis[t]=1;
person[i].c--;
t--;
}else {
t--;
}
}
}
System.out.println(sum(axis,0,max));
}
private static int sum(int[] axis,int s,int t){
int sum=0;
for (int i = s; i <=t; i++) {
sum +=axis[i];
}
return sum;
}
private static class person implements Comparable<person>{
int M;
int N;
int c;
public person(int m, int n,int c) {
M = m;
N = n;
this.c = c;
}
@Override
public int compareTo(person o) {
int x = this.N-o.N;
int y = this.M-o.M;
return x!=0? x:y;
}
}
}
|