Java蓝桥杯基础练习(4)


22、FJ的字符串

在这里插入图片描述

import java.util.Scanner;
public class FJ的字符串_22 {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        
        String s="";
        for(int i=1;i<=n;i++){
//        	s=String.valueOf((char)(64+i)); //输出第i个大写字母  ,i<=26
        	s=s+(char)(64+i)+s;
        	
        }

        System.out.println(s);
    }
}

23、芯片测试

在这里插入图片描述

import java.util.Scanner;
public class 芯片测试_23 {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int [][]arr = new int[n][n];
		int b[] = new int[n];
		for (int i =0;i<n;i++){
			for (int j = 0;j<n;j++){
				arr[i][j] = sc.nextInt();
				if (arr[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+" ");
			}
		}
	}
}

26、报时助手

在这里插入图片描述

import java.util.Scanner;
public class 报时助手_26 {
	static String[] times1 = { "zero", "one", "two", "three", "four", "five",
		"six", "seven", "eight", "nine", "ten", "eleven", "twelve",
		"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
		"eighteen", "nineteen", "twenty" };
	static String[] times2 = { "0", "0", "twenty", "thirty", "forty", "fifty" };

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		while (scanner.hasNext()) {
			int h = scanner.nextInt();
			int m = scanner.nextInt();

			if (h > 20) {
				int a = h / 10;
				int b = h % 10;
				System.out.print(times2[a] + " " + times1[b] + " "); //大于20小于60的数字,首先读整十的数,然后再加上个位数。
			} else {
				System.out.print(times1[h] + " ");
			}

			if (m == 0) {
				System.out.println("o'clock");
			} else if (m > 20) {
				int a = m / 10;
				int b = m % 10;
				System.out.println(times2[a] + " " + times1[b]); //大于20小于60的数字,首先读整十的数,然后再加上个位数。
			} else {
				System.out.println(times1[m]);
			}
		}
	}

}

28、Huffuman树

在这里插入图片描述

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Huffman树_28 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		List<Integer> list = new ArrayList<Integer>();//用来存储输入的n个数
		for(int i =0;i<n;i++){
			int a1 = sc.nextInt();
			list.add(a1);
		}
		
		boolean flag = true;
		int sum=0;  //构造Huffman树的总费用
		while(flag){
			if(list.size()<2){
				System.out.println(sum);
				flag=false;
			}else {
				int min []=new int[list.size()];  
				for(int i =0;i<min.length;i++){
					min[i]=list.get(i);
				}
				Arrays.sort(min);  //数组排序
				int b =min[0];
				int c =min[1];
				list.remove(Integer.valueOf(b)); //删除最小的两个数
				list.remove(Integer.valueOf(c));
				int d =b+c; //最小的两数之和
				sum+=d;  //计算费用
				list.add(d);  //插入数组中
			}
		}
	}
}

29、高精度加法

在这里插入图片描述

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

	/*法一:
 	对其加减乘除其实就是BigDecimal的类的一些调用。
 	加法 add()函数,减法subtract()函数,乘法multiply()函数,
	除法divide()函数,绝对值abs()函数
	*/
/*public class 高精度加法_29 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		BigDecimal a,b,c;
		a=sc.nextBigDecimal(); //BigInteger也可
		b=sc.nextBigDecimal();
		c=a.add(b);
		System.out.println(c);
	}
}*/

//法二:
public class 高精度加法_29 {

	    public static void main(String[] args) throws NumberFormatException, IOException {
	        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	        String s1 = br.readLine();
	        String s2 = br.readLine();
	        int a[] = new int[100000];
	        int b[] = new int[100000];
	        //得到输入的数的长度
	        int l1 = s1.length();
	        int l2 = s2.length();
	         //将输入的数转化成以整数形式存储的数组,a[0]存个位数,a[1]存十位数...
	        for(int i=0; i<l1; i++){      
	            a[l1-1-i] = Character.getNumericValue(s1.charAt(i));
	        }
	        for(int i=0; i<l2; i++){
	            b[l2-1-i] = Character.getNumericValue(s2.charAt(i));
	        }
	        //找出两个加数中最大的数
	        int max = l1>l2?l1:l2;
	        int c[] = new int[100000];
	        //先把每个位上的数相加,和存到数组c[]中
	        for(int i=0; i<max; i++){
	            c[i] = a[i] + b[i];
	        }
	        //将c[]中需要进位的进行处理
	        for(int i=0; i<max-1; i++){
	            if(c[i]>9){
	                c[i+1] += c[i]/10;
	                c[i] = c[i]%10;
	            }
	        }
	        //看最高的位需要进位的话,扩大位数。
	        while(c[max-1]>9){
	            c[max] = c[max-1]/10;
	            c[max-1] = c[max-1]%10;
	            max++;
	        }
	        for(int i=max-1; i>=0; i--){
	            System.out.print(c[i]); 
	        }
	    }
	}


30、阶乘计算

在这里插入图片描述

import java.math.BigInteger;
import java.util.Scanner;
public class 阶乘计算_30 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		BigInteger sum=BigInteger.valueOf(1);  //将int转为BigInteger
		int n=sc.nextInt();
		for (int i = 1; i<=n; i++) {
			sum=sum.multiply(BigInteger.valueOf(i));
			
		}
		System.out.println(sum);
	}
}
 

26个英文字母

在这里插入图片描述

public class 二十六个英文字母 {
/*//	法一:
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int n=in.nextInt();
		for(int i=1;i<=n;i++){
			String s=String.valueOf((char)(64+i));//输出第i个大写字母  ,i<=26
			System.out.print(s+" ");
		}
	}*/
	
//	法二:
	public static void main(String[] args) {
		int a[]=new int[26];
		char s='A';
		for(int i=0;i<26;i++){
			a[i]=s+i;
			System.out.print((char)a[i]+" ");
		}
	}
}

字符串的拼接

在这里插入图片描述

public class 字符串的拼接 {
	public static void main(String a[]) {
		StringBuffer s=new StringBuffer();

		String s1="asd";
		String s2="123";
		s.append(s1).append(s2);
		System.out.println("第一次s:"+s);

		for (int i = 0; i <5 ; i++) {
			s.append('A');
			
		}
		System.out.println("第二次s:"+s);
		
		s.reverse();
		System.out.println("第三次s的倒序:"+s);
		
	}
}

3个数最大的最小公倍数

  1. 如果n是一个奇数, 那么它的最大值: n * (n - 1) * (n - 2)
    例:n = 9, max = 9 * 8 * 7
  2. 当n是一个偶数时,讨论它能否被3整除
    ①同时能被 2 和 3 整除, 则其最大值:(n - 1) * (n - 2) * (n - 3)
    例:n = 12, max = 11 * 10 * 9
    ②如只能被 2 整除, 则其最大值为:n * (n - 1) * (n - 3)
    例:n = 16, max = 16 * 15 * 13
import java.util.Scanner;
public class 最大的最小公倍数_2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long N = sc.nextLong();
		long max = getMaxNum(N);
		System.out.println(max);
	}
	public static long getMaxNum(long n) {
		if(n <= 2) 
			return n;
		// 1.如果n是一个奇数, 那么它的最大值: n * (n - 1) * (n - 2)
		// 例:n = 9, max = 9 * 8 * 7
		else if(n % 2 == 1) 
			return n * (n - 1) *( n - 2);
		else {
			// 2.当n是一个偶数时,讨论它能否被3整除
			// 3.如同时能被 2 和 3 整除, 则其最大值:(n - 1) * (n - 2) * (n - 3)
			// 例:n = 12, max = 11 * 10 * 9
			if(n % 3 == 0)
				return (n - 1) * (n - 2) * (n - 3);
			// 4.如只能被 2 整除, 则其最大值为:n * (n - 1) * (n - 3)
			// 例:n = 16, max = 16 * 15 * 13
			else
				return n * (n - 1) * (n - 3);
		}
	}
}

单词分析:

在这里插入图片描述
方法一:将字母转化为整型数组来记录,利用数组的思维做

import java.util.Scanner;

public class 单词分析 {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		char []ch=sc.nextLine().toCharArray();
		int []word=new int[1000];
		for(int i=0;i<ch.length;i++) {
			word[ch[i]-'a']++;//将字母转化为整型数组来记录
		}
		
		//初始化-1,防止字符数组为空时输出出现字母
		int max=-1;
		int wordmax=-1;
		
		for(int i=0;i<word.length;i++) {
			if(word[i]>max) {
				max=word[i];//标记最大单词
				wordmax=i;//记入单词出现的次数
			}
		}
		
		char c=(char)('a'+wordmax);
		System.out.println(c);
		System.out.println(max);
		}
}

方法二:利用到HashMap和Set的特点来做。

import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class Main {
	public static  void main(String arg[]) {
		StringBuffer bf = new StringBuffer();
		Scanner sc = new Scanner(System.in);
		String str=sc.nextLine();
		HashMap<Character, Integer> hashMap = new HashMap<>();

		//统计每个出现次数
		for (int i = 0; i < str.length(); i++) {
			char c=str.charAt(i);
			if (hashMap.containsKey(c)) {
				hashMap.put(c, hashMap.get(c)+1);}
			else {
				hashMap.put(c, 1); //例:asdasdzxc,{a=2, s=2, c=1, d=2, x=1, z=1}
			}
		}
		
		//键找值,找出出现最多的次数
		int max[]=new int[1000];
		Set<Character> keySet = hashMap.keySet();
		TreeSet<Integer> countSet = new TreeSet<Integer>();  //统计字符出现次数的set集合
		for (Character key : keySet) {
			Integer value = hashMap.get(key);
			countSet.add(value);  //将字符出现次数的添加到set集合,自带排序功能
			//System.out.println(key+","+value);
		}

		//Entry键值对原理,根据输入的值找到键
		TreeSet<Character> maxword = new TreeSet<Character>();  //统计出现相同次数字符的集合,自带排序功能
		Set<Entry<Character, Integer>> entrySet = hashMap.entrySet();
		for (Entry<Character, Integer> entry : entrySet) {
			if (entry.getValue().equals(countSet.last())) {          
				maxword.add(entry.getKey());  //将出现相同次数字符的添加到set集合,自带排序功能
			}
		}
		System.out.println(maxword.first());// 出现得最多的那个字母在单词中出现的次数的 字母
		System.out.println(countSet.last());//出现得最多的那个字母在单词中出现的 次数
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

来lai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值