• 蓝桥题库基础练习11-20


    基础练习题库BASIC11-20

    试题编号 试题名称 关键字
    BASIC-11 十六进制转十进制 进制转换 字符处理 判断
    BASIC-12 十六进制转八进制 进制转换 字符 循环
    BASIC-13 数列排序 数组 排序
    BASIC-14 时间转换 取余 数字字符混合输出
    BASIC-15 字符串对比 字符串 大小写
    BASIC-16 分解质因数 质数分解 循环
    BASIC-17 矩阵乘法 二维数组 循环 矩阵
    BASIC-18 矩形面积交 判断 线段交
    BASIC-19 完美的代价 贪心算法
    BASIC-20 数的读法 判断 函数

    基础题真的很基础,有些题目一眼看过去就觉得很简单,感觉都没有做的必要。

    但是,但是也许越简单越容易埋坑。


    十六进制转十进制

    问题描述

    从键盘输入一个不超过8位的正的十六进制数字符串,将它转换为正的十进制数后输出。
      注:十六进制数中的10~15分别用大写的英文字母A、B、C、D、E、F表示。

    数据规模与约定

    样例输入 样例输入
    FFFF 65535

    Java源代码

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] agrs) {
    		Scanner sc = new Scanner(System.in);
    		String num = sc.nextLine();
    		sc.close();
    		System.out.println(Long.parseLong(num, 16));
    	}
    }
    

    十六进制转八进制

    问题描述

    给定n个十六进制正整数,输出它们对应的八进制数。

    数据规模与约定

    输入格式 输出格式
    输入的第一行为一个正整数n (1<=n<=10)。
      接下来n行,每行一个由09、大写字母AF组成的字符串,表示要转换的十六进制正整数,每个十六进制数长度不超过100000。
    输出n行,每行为输入对应的八进制正整数。

    提示】
      先将十六进制数转换成某进制数,再由某进制数转换成八进制。


    Java源代码 ---- Error

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String args[]) {
    
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		sc.nextLine();
    		String[] str =  new String[n];
    		
    		for(int i=0;i<n;i++)
    			str[i] = sc.nextLine();
    		sc.close();
    		
    		for(int i=0;i<str.length;i++)
    			System.out.println(Integer.toOctalString(Integer.valueOf(str[i],16)));
    	}
    }
    

    这里不能偷鸡取巧了!老老实实写算法。


    数列排序

    问题描述

    给定一个长度为n的数列,将这个数列按从小到大的顺序排列。1<=n<=200


    数据规模与约定

    输入格式 输出格式
    第一行为一个整数n。
      第二行包含n个整数,为待排序的数,每个整数的绝对值小于10000。
    输出一行,按从小到大的顺序输出排序后的数列。

    官方测试用例

    序号 输入 输出
    1 5
    8 3 6 4 9
    3 4 6 8 9

    Java源代码

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int[] num = new int[n];
    
    		for (int i = 0; i < n; i++) {
    			num[i] = sc.nextInt();
    		}
    		
    		num = insertSort(num);
    		for (int i = 0; i < n; i++) {
    			System.out.print(num[i] + " ");
    		}
    	}
    
    	public static int[] insertSort(int[] data) {
    		int temp;
    		for (int i = 1; i < data.length; i++) {
    			temp = data[i];// 待插入数据
    			int j;
    			for (j = i - 1; j >= 0; j--) {
    				// 判断是否大于temp,大于则后移一位
    				if (data[j] > temp) {
    					data[j + 1] = data[j];
    				} else {
    					break;
    				}
    			}
    			data[j + 1] = temp;
    		}
    		return data;
    	}
    }
    

    时间转换

    问题描述

    给定一个以秒为单位的时间t,要求用“<H>:<M>:<S>”的格式来表示这个时间。<H>表示时间,<M>表示分钟,而<S>表示秒,它们都是整数且没有前导的“0”。例如,若t=0,则应输出是“0:0:0”;若t=3661,则输出“1:1:1”。


    数据规模与约定

    0<=t<=86399

    输入格式 输出格式
    输入只有一行,是一个整数t。 输出只有一行,是以“::”的格式所表示的时间,不包括引号。

    测试用例

    序号 输入 输出
    1 0 0:0:0
    2 5436 1:30:36

    Java源代码

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String args[]) {
    		Scanner sc = new Scanner(System.in);
    		int time = sc.nextInt();
    		System.out.printf("%d:%d:%d", time/3600, time%3600/60,time%60);
    		
    	}
    }
    

    字符串对比

    问题描述

     给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一:
      1:两个字符串长度不等。比如 Beijing 和 Hebei
      2:两个字符串不仅长度相等,而且相应位置上的字符完全一致(区分大小写),比如 Beijing 和 Beijing
      3:两个字符串长度相等,相应位置上的字符仅在不区分大小写的前提下才能达到完全一致(也就是说,它并不满足情况2)。比如 beijing 和 BEIjing
      4:两个字符串长度相等,但是即使是不区分大小写也不能使这两个字符串一致。比如 Beijing 和 Nanjing
      编程判断输入的两个字符串之间的关系属于这四类中的哪一类,给出所属的类的编号。


    数据规模与约定

    1 <= n <= 1000。

    输入格式 输出格式
    包括两行,每行都是一个字符串 仅有一个数字,表明这两个字符串的关系编号

    测试用例

    序号 输入 输出
    1 BEIjing
    beiJing
    3

    Java源代码

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String args[]) {
    		Scanner sc = new Scanner(System.in);
    		String str1 = sc.nextLine();
    		String str2 = sc.nextLine();
    		if(str1.equals(str2)){
    			System.out.println("2");
    		}else if(str1.equalsIgnoreCase(str2)){
    			System.out.println("3");
    		}else if(str1.length()==str2.length()){
    			System.out.println("4");
    		}else {
    			System.out.println("1");
    		}
    	}
    }
    

    分解质因数

    问题描述

    求出区间[a,b]中所有整数的质因数分解。

    数据规模与约定

     2<=a<=b<=10000

    输入格式 输出格式
    输入两个整数a,b。 每行输出一个数的分解,形如k=a1a2a3...(a1<=a2<=a3...,k也是从小到大的)(具体可看样例)

    测试用例

    序号 输入 输出
    1 3 10 3=3
    4=22
    5=5
    6=2
    3
    7=7
    8=222
    9=33
    10=2
    5

    提示:先筛出所有素数,然后再分解。


    Java源代码

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int a = sc.nextInt();
    		int b = sc.nextInt();
    		sc.close();
    		
    		for (int i = a; i <= b; i++) {
    			if (isPrime(i))
    				System.out.println(i + "=" + i);
    			else {
    				if(i>=2)
    					System.out.println(resolve(i));
    			}
    		}
    	}
    
    	public static boolean isPrime(int num) {
    		if (num < 2)
    			return false;
    		for (int i = 2; i <= Math.sqrt(num); i++) {
    			if (num % i == 0)
    				return false;
    		}
    		return true;
    	}
    
    	public static String resolve(int n) {
    		String strResult = n + "=";
    		for (int i = 2; i <= n; i++) {
    			if (n % i == 0) {
    				strResult += i + "*";
    				n = n / i;
    				i--;
    			}
    		}
    		return strResult.substring(0, strResult.length() - 1);
    	}
    }
    
    

    矩阵乘法

    问题描述

    给定一个N阶矩阵A,输出A的M次幂(M是非负整数)
      例如:
      A =
      1 2
      3 4
      A的2次幂
      7 10
      15 22


    数据规模与约定

    输入格式 输出格式
    第一行是一个正整数N、M(1<=N<=30, 0<=M<=5),表示矩阵A的阶数和要求的幂数
      接下来N行,每行N个绝对值不超过10的非负整数,描述矩阵A的值
    输出共N行,每行N个整数,表示A的M次幂所对应的矩阵。相邻的数之间用一个空格隔开

    测试用例

    序号 输入 输出
    1 2 2
    1 2
    3 4
    7 10
    15 22

    Java源代码

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		int m = sc.nextInt();
    
    		int[][] arry = new int[n][n];
    		int[][] result = new int[n][n];
    		for (int i = 0; i < n; i++) {
    			for (int j = 0; j < n; j++) {
    				arry[i][j] = sc.nextInt();
    				if (i == j)
    					result[i][j] = 1;
    				else
    					result[i][j] = 0;
    			}
    		}
    		sc.close();
    
    		if (m >= 1) {
    			result = arry.clone();
    			//输入及输出:E	A	A*A	 A*A*A
    			for (int i = 0; i < m - 1; i++) {
    				result = matrix(result, arry);
    				//System.out.println("运算1次");
    
    			}
    
    		}
    
    		for (int i = 0; i < n; i++) {
    			for (int j = 0; j < n; j++) {
    				System.out.print(result[i][j] + " ");
    			}
    			System.out.println();
    		}
    
    	}
    
    	public static int[][] matrix(int[][] arr1, int[][] arr2) {
    		int length = arr1[0].length;
    		int[][] result = new int[length][length];
    		for (int i = 0; i < length; i++) {
    			for (int j = 0; j < length; j++) {
    				for (int k = 0; k < length; k++) {
    					result[i][j] += arr1[i][k] * arr2[k][j];
    				}
    			}
    		}
    		return result;
    	}
    
    }
    

    矩形面积交

    问题描述

    ​ 平面上有两个矩形,它们的边平行于直角坐标系的X轴或Y轴。对于每个矩形,我们给出它的一对相对顶点的坐标,请你编程算出两个矩形的交的面积。

    数据规模与约定

    输入格式 输出格式
    输入仅包含两行,每行描述一个矩形。
      在每行中,给出矩形的一对相对顶点的坐标,每个点的坐标都用两个绝对值不超过10^7的实数表示。
    输出仅包含一个实数,为交的面积,保留到小数后两位。

    测试用例

    序号 输入 输出
    1 1 1 3 3
    2 2 4 4
    1.00

    Java源代码

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		double A_x = sc.nextDouble();
    		double A_y = sc.nextDouble();
    		double B_x = sc.nextDouble(); 
    		double B_y = sc.nextDouble();
    		double C_x = sc.nextDouble();
    		double C_y = sc.nextDouble();
    		double D_x = sc.nextDouble();
    		double D_y = sc.nextDouble();
    		sc.close();
    		
    		//找到4个点中最接近的两个点。
    		double x1 = Math.max(Math.min(A_x, B_x), Math.min(C_x, D_x));//从小到大x轴的第2个坐标  
    		double y1 = Math.max(Math.min(A_y, B_y), Math.min(C_y, D_y));//从小到大y轴的第2个坐标  
    		double x2 = Math.min(Math.max(A_x, B_x), Math.max(C_x, D_x));//从小到大x轴的第3个坐标
    		double y2 = Math.min(Math.max(A_y, B_y), Math.max(C_y, D_y));//从小到大y轴的第3个坐标
    		//若相交,点1 为左下角,点2为右上角
    		if(x2-x1>0 && y2-y1>0){
    			System.out.printf("%.2f",(x2-x1)*(y2-y1));
    		}else
    			System.out.printf("%.2f",0.0);
    		
    
    	}
    
    }
    

    完美的代价

    问题描述

    回文串,是一种特殊的字符串,它从左往右读和从右往左读是一样的。小龙龙认为回文串才是完美的。现在给你一个串,它不一定是回文的,请你计算最少的交换次数使得该串变成一个完美的回文串。
      交换的定义是:交换两个相邻的字符
      例如mamad
      第一次交换 ad : mamda
      第二次交换 md : madma
      第三次交换 ma : madam (回文!完美!)

    数据规模与约定

    N <= 8000

    输入格式 输出格式
    第一行是一个整数N,表示接下来的字符串的长度
      第二行是一个字符串,长度为N.只包含小写字母
    如果可能,输出最少的交换次数。
      否则输出Impossible

    测试用例

    序号 输入 输出
    1 1 1 3 3
    2 2 4 4
    1.00

    Java源代码

    import java.util.Scanner;
    
    public class Main {
    	public static void main(String args[]) {
    
    		Scanner sc = new Scanner(System.in);
    		int n = sc.nextInt();
    		sc.nextLine();
    		String str = sc.nextLine();
    		sc.close();
    
    		if(Isimprosable(str))
    			System.out.print(changedTimes(str));
    		else {
    			System.out.print("Impossible");
    		}
    	}
    
    	public static boolean Isimprosable(String str) {
    
    		int[] count = new int[26];
    		int jishu = 0;
    
    		for (int i = 0; i < str.length(); i++) {
    			count[str.charAt(i) - 'a']++;
    		}
    
    		for (int i = 0; i < count.length; i++) {
    			if (count[i] % 2 != 0)
    				jishu++;
    		}
    
    		if (jishu > 1)
    			return false;
    		else
    			return true;
    	}
    
    	public static int changedTimes(String str) {
    		//两个字母以下的字符串必须相同,所以不用换位置
    		if (str.length() <= 2)
    			return 0;
    
    		int MoveTimes = 0;
    		// 找首字母配对的最后一次字母
    		int lastOneIdx = str.lastIndexOf(str.charAt(0));
    		if (lastOneIdx == 0) {
    			// 如果首字母最后一次出现还是在首字母,说明该字母是无匹配,需要移动至字符串中间
    			MoveTimes = str.length() / 2;
    			// 除去首字母继续递归调整
    			String new_str = str.substring(1, str.length());// 下标0的首字母不要
    			return MoveTimes + changedTimes(new_str);
    		} else {
    			MoveTimes = (str.length() - 1) - lastOneIdx;// 最后一个字母的下标减去匹配字母的下标得到移动次数
    			// 除去首字母和匹配字母继续递归
    			StringBuilder strBuffer = new StringBuilder(str);
    			strBuffer.deleteCharAt(lastOneIdx);
    			strBuffer.deleteCharAt(0);
    			return MoveTimes + changedTimes(strBuffer.toString());
    		}
    
    	}
    }
    

    数的读法

    问题描述

     Tom教授正在给研究生讲授一门关于基因的课程,有一件事情让他颇为头疼:一条染色体上有成千上万个碱基对,它们从0开始编号,到几百万,几千万,甚至上亿。
      比如说,在对学生讲解第1234567009号位置上的碱基时,光看着数字是很难准确的念出来的。
      所以,他迫切地需要一个系统,然后当他输入12 3456 7009时,会给出相应的念法:
      十二亿三千四百五十六万七千零九
      用汉语拼音表示为
      shi er yi san qian si bai wu shi liu wan qi qian ling jiu
      这样他只需要照着念就可以了。
      你的任务是帮他设计这样一个系统:给定一个阿拉伯数字串,你帮他按照中文读写的规范转为汉语拼音字串,相邻的两个音节用一个空格符格开。
      注意必须严格按照规范,比如说“10010”读作“yi wan ling yi shi”而不是“yi wan ling shi”,“100000”读作“shi wan”而不是“yi shi wan”,“2000”读作“er qian”而不是“liang qian”。 
    

    数据规模与约定

    不超过2,000,000,000

    输入格式 输出格式
    有一个数字串,数值大小不超过2,000,000,000。 是一个由小写英文字母,逗号和空格组成的字符串,表示该数的英文读法。

    测试用例

    序号 输入 输出
    1 1234567009 shi er yi san qian si bai wu shi liu wan qi qian ling jiu

    Java源代码

    import java.util.Scanner;
    
    public class Main {
     
     
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int n = sc.nextInt();
            sc.close();
            
            String[] dict_num = {"ling ", "yi ", "er ", "san ", "si ", "wu ", "liu ", "qi ", "ba ", "jiu "};
            String[] dict_wei = {"", "shi ", "bai ", "qian ", "wan ", "shi ", "bai ", "qian ", "yi ", "shi ", "bai "};
            String numStr = n + "";//{1,2,3,4,5,6,7,0,0,9}
            String read = new String();
            boolean b = true;
            int i, j;
    
            for (i = 0; i < numStr.length(); i++) {
            
                for (j = 0; j <= 9; j++)
                    if (numStr.charAt(i) == j + '0')
                        break;
                if ((numStr.length() + 2) % 4 == 0 && i == 0 && j == 1) {
                	read += dict_wei[numStr.length()- 1 - i ];
                } else if (i != numStr.length() - 1 && j == 0) {
                    if (b) {
                    	read += dict_num[j];
                        b = false;
                    }
                } else if (i == numStr.length() - 1 && j == 0) {
                    if (!b)
                    	read = read.substring(0, read.length() - 5);// 如果最后一个也为0,则去掉之前添加的
                    // ling
                } else {
                	read += dict_num[j] + dict_wei[numStr.length() - i - 1];
                    b = true;
                }
            }
            System.out.println(read);
        }
    }
    
  • 相关阅读:
    NET打包時加入卸载功能
    c#水晶报表注册码
    sqlserver:某年份某月份 是否在某时间段内的函数
    修改KindEditor编辑器 版本3.5.1
    Flash大文件上传(带进度条)
    让.Net程序脱离.net framework框架运行的方法(转载)
    夏天到了,什么时候园子的T恤可以出来?
    VS2005项目的安装与布署
    解决Select覆盖Div的简单直接的方法
    .Net向Page和UpdatePanel输出JS
  • 原文地址:https://www.cnblogs.com/1101-/p/12634732.html
Copyright © 2020-2023  润新知