Java Blue Bridge Cup basic exercises (3)


10. Convert decimal to hexadecimal

insert image description here

import java.util.Scanner;

//法一:100分
public class 十进制转十六进制_10 {
    
    
 public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
//		System.out.println(Integer.toBinaryString(n)); //二进制
//		System.out.println(Integer.toOctalString(n));//八进制
		System.out.println(Integer.toHexString(n).toUpperCase()); //十六进制
	}
}

11. Convert hexadecimal to decimal

insert image description here

import java.math.BigInteger;
import java.util.Scanner;

//法1: 100分
public class 十六进制转十进制_11 {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine();
		String Oct=new BigInteger(str, 16).toString(10);
		System.out.println(Oct);
	}
}

12. Convert hexadecimal to octal

insert image description here

import java.math.BigInteger;
import java.util.Scanner;
//法1: 100分
public class 十六进制转八进制_12  {
    
    
	/* 给定n个十六进制正整数,输出它们对应的八进制数。 */
	public static void main(String[] args) {
    
    
	   Scanner sc=new Scanner(System.in);
      //获取n
	   int n=sc.nextInt();
	   //定义数组来存放转换后的数
	   String []arr=new String[n];
	   //循环然后进行16进制转换为8进制
	   for(int i=0;i<n;i++) {
    
    
		   //获取16进制数(注意next和nextInt区别)
		   String sixteen=sc.next();
		   //由于数字可能会越界,所有使用BigInteger的api
		   //new BigInteger(number, from).toString(to);
		   //number:要转换的数     from:原数的进制      to:要转换成的进制
		   String eight=new BigInteger(sixteen, 16).toString(8);
		   //将结果存入arr中
		   arr[i]=eight;
	   }
	   //遍历数组并打印
	   for(int i=0;i<n;i++) {
    
    
		   System.out.println(arr[i]);
	   }
	}
}

13. Array sorting

insert image description here

import java.util.Scanner;

public class 数列排序_13 {
    
    
	public static void  main(String a[]) {
    
    
		Scanner in = new Scanner(System.in);
		int n=in.nextInt();
		int f[] =new int[n];
		for (int i = 0; i < n; i++) {
    
    
			f[i]=in.nextInt();
		}
		int b = 0;
		for(int i =0;i<n-1;i++) {
    
    
			for(int j = i+1;j<n;j++) {
    
    
				if(f[i]>f[j]) {
    
    
					b = f[i];
					f[i] = f[j];
					f[j] = b;
				}
			}
		}
		for(int i =0;i<n;i++) {
    
    
			System.out.printf("%d ",f[i]);
		}

	}

}

14. Time conversion

insert image description here

import java.util.Scanner;

public class 时间转换_14 {
    
    
	public static void  main(String a[]) {
    
    
		Scanner in = new Scanner(System.in);
		int n=in.nextInt();
		int H=n/3600;
		int M=(n-H*3600)/60;
		int S=(n-H*3600)-60*M;

		
		System.out.print(H+":"+M+":"+S);
	}
	
}

15. String comparison

insert image description here

import java.util.Scanner;

public class 字符串比较_15 {
    
    
	public static void  main(String a[]) {
    
    
		Scanner in = new Scanner(System.in);
		String []arr=new String[2];
	
		for (int i = 0; i < arr.length; i++) {
    
    
			 arr[i]=in.next();
		}
		if(arr[0].length()!=arr[1].length())
		{
    
    
			System.out.print("1");
		}else {
    
    
			if(arr[0].equals(arr[1]) ){
    
    
				System.out.print("2");
			}else if (arr[0].toUpperCase().equals(arr[1].toUpperCase())) {
    
    
				System.out.print("3");
			}else {
    
    
				System.out.print("4");
			}
			
		}
		
	}
}

16. Decompose prime factor

insert image description here

import java.util.Scanner;

public class 分解质因数_16 {
    
    
	//质数: 质数又称素数。一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数;否则称为合数。
	//0和1既不是质数也不是合数,最小的质数是2
    public static void main(String[] args) {
    
    
           Scanner in = new Scanner(System.in);
           int a = in.nextInt();
           int b = in.nextInt();
           for(;a<=b;a++){
    
    
        	   int temp = a;
        	   boolean flag = false;
        	   System.out.print(temp+"=");
        	   while(temp!=1){
    
    
        		   for(int i=2;i<=temp;i++){
    
    
        			   if(isPrime(i)&&temp%i==0){
    
    
        				   if(flag){
    
    
        					   System.out.print("*"+i); 
        				   }else{
    
    
        					   flag = true;
        					   System.out.print(i);
        				   }
        				   temp /= i; 
        				   break;
        			   }
        		   }
        	   }
        	   System.out.println();
           }
        }
        
        public static boolean isPrime(int n){
    
    
        	if(n<2)return false;
        	if(n==2)return true;
        	if(n%2==0)return false;
        	for(int i=3;i*i<=n;i+=2){
    
    
        		if(n%i==0){
    
    
        			return false;
        		}
        	}
        	return true;
        }
 
}

17. Matrix multiplication

insert image description here
insert image description here

import java.util.Scanner;
public class 矩阵乘法_17 {
    
    

    private static int N;//矩阵A的阶数
    private static int[][] matrix; //矩阵A

    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();
        int M = sc.nextInt(); //要求的幂数
        matrix = new int[N][N];  //原矩阵
        
        //输入矩阵数据									
        for (int i = 0; i < N; i++) {
    
    
            for (int j = 0; j < N; j++) {
    
    
                matrix[i][j] = sc.nextInt();
            }
        }
        
      //新矩阵
        int[][] newMatrix = new int[N][N];
      //幂数为0的情况
        if (M == 0) {
    
    
            for (int i = 0; i < N; i++) {
    
    
                newMatrix[i][i] = 1;
            }
        //幂数不为0的情况    
        } else {
    
    
            newMatrix = matrix;
            for (int i = 0; i < M - 1; i++) {
    
    
                newMatrix = matriMultiplication(newMatrix);
            }
        }
      //输出新矩阵
        for (int i = 0; i < N; i++) {
    
    
            for (int j = 0; j < N; j++) {
    
    
                System.out.print(newMatrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    private static int[][] matriMultiplication(int[][] matrixParam) {
    
    
        int[][] newMatrix = new int[N][N];
        for (int i = 0; i < N; i++) {
    
    
            for (int j = 0; j < N; j++) {
    
    
                for (int k = 0; k < N; k++) {
    
    
                    newMatrix[i][j] += matrixParam[i][k] * matrix[k][j];
                }
            }
        }
        return newMatrix;
    }
}



18. Rectangular area intersection

insert image description here
insert image description here

import java.util.Scanner;
public class 矩形面积交_18 {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		//获得四个点的坐标
		double x1 = sc.nextDouble(),y1=sc.nextDouble(),x2=sc.nextDouble(),y2 = sc.nextDouble();
		double x3 = sc.nextDouble(),y3=sc.nextDouble(),x4=sc.nextDouble(),y4 = sc.nextDouble();
		//关闭Scanner方法
		sc.close();
		//找出矩形一的最大最小值x,最大最小y
		double maxx = Math.max(x1,x2);
		double minx = Math.min(x1, x2);
		double maxy = Math.max(y1, y2);
		double miny =Math.min(y1, y2);
		//找出矩形二的最大最小值x,最大最小y
		double bigx = Math.max(x3,x4);
		double smax = Math.min(x3, x4);
		double bigy = Math.max(y3, y4);
		double smay =Math.min(y3, y4);
		//判断是否相离或者相切
		if(smax>=maxx||minx>=bigx ||smay>=maxy||miny>=bigy){
    
    
			System.out.println("0.00");
		}else{
    
    
			//找出相交的矩形的两个点,点1(xx,yy) 点2(x,y)
			double xx = Math.max(minx, smax),yy=Math.max(miny, smay);
			double x = Math.min(maxx, bigx),y=Math.min(maxy, bigy);
			System.out.println(String.format("%.2f", (Math.abs(x-xx)*Math.abs(y-yy))));
		}
	}
}

19. The price of perfection

insert image description here

package a;
import java.util.Scanner;
public class 完美的代价_19 {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		String str = sc.next();
		//将输入字符串转换为char数组
		char[] charArr = str.toCharArray();
		//求出现的次数为奇数的字母个数	    
		int[] num = new int[26];
		//记录每个字母出现的个数
		for(int i = 0; i < n; i++) {
    
    
			num[charArr[i] - 'a']++;
		}
		//出现次数为奇数的字母个数
		int count = 0;
		for(int i = 0; i < 26; i++) {
    
    
			if(num[i] != 0) {
    
    
				if(num[i] % 2 != 0) {
    
    
					count++;
				}
			}
		}
		//若个数大于等于2个,则直接返回Impossible
		if(count >= 2) {
    
    
			System.out.println("Impossible");
		}else{
    
    
			System.out.println(getCount(str));
		}
	}

	static int getCount(String str) {
    
    
		//1,2长度是无需交换
		if(str.length()==1 || str.length()==2) return 0;
		//取字符串第一个字符最后一次出现的位置
		int temp = str.lastIndexOf(str.charAt(0));
		if(temp==0){
    
    
			return str.length()/2 + getCount(str.substring(1,str.length()));
		} else {
    
    
			//0号位字符和已找到temp位置的字符都移除,然后进行递归        
			//String remoStr =  str.substring(1, temp) + str.substring(temp + 1,str.length()); //这么写不知道那组数据不对
			StringBuilder strBuilder = new StringBuilder(str);
			strBuilder.deleteCharAt(temp);
			strBuilder.deleteCharAt(0);        	
			//System.out.println(temp +".."+ remoStr);
			return str.length()-temp-1 + getCount(strBuilder.toString());
		}
	}
}




Guess you like

Origin blog.csdn.net/weixin_44531966/article/details/114760998