0
点赞
收藏
分享

微信扫一扫

蓝桥杯31天冲刺之八 [java]

c一段旅程c 2022-03-15 阅读 68

文章目录

神奇的算式

image-20220315131024003

题目链接:https://www.lanqiao.cn/problems/700/learning/

package daily;

import java.util.ArrayList;
import java.util.HashSet;

/**
 * https://www.lanqiao.cn/problems/700/learning/
 * 
 * @author Jia
 *
 */
public class day3_15_1 {
	public static void main(String[] args) {
		HashSet<Integer> repeatSet = new HashSet<>();// 用于判断第二种情况是不是重复算了
		int ans = 0;

		for (int i = 1000; i < 9999; i++) {
			// 判断这个数四位是否相同
			ArrayList<Integer> list = getList(i);
			// 当list大小不等于4是表示肯定有重复的数字
			if (list.size() != 4) {
				continue;
			}

			// 情况1 一位数与三位数
			int num1 = i / 1000; // 第一位数字
			int num2 = i % 1000; // 后三位数字
			int mult = num1 * num2;
			ArrayList<Integer> tempList = getList(mult);
			if (judgeListEqual(list, tempList)) {
				ans++;
			}

			// 情况2 两个两位数
			if (repeatSet.contains(i)) {
				continue;
			}
			num1 = i / 100; // 前两位数字
			num2 = i % 100; // 后两位数字
			mult = num1 * num2;
			tempList = getList(mult);
			if (judgeListEqual(list, tempList)) {
				ans++;
				repeatSet.add(num2 * 100 + num1);
			}
		}
		System.out.println(ans);
	}

	/**
	 * 将传入的数字拆分成个位数,如果有相同的数字,则返回的数组长度小于4
	 * 
	 * @param j
	 * @return
	 */
	private static ArrayList<Integer> getList(int j) {
		ArrayList<Integer> list = new ArrayList<>();
		while (j > 0) {
			if (list.contains(j % 10)) {
				break;
			}
			list.add(j % 10);
			j /= 10;
		}
		return list;
	}

	/**
	 * 判断两个数组是否包含相同的元素
	 * 
	 * @param list
	 * @param tempList
	 * @return
	 */
	private static boolean judgeListEqual(ArrayList<Integer> list, ArrayList<Integer> tempList) {
		if (list.size() != tempList.size()) {
			return false;
		}
		for (int i = 0; i < list.size(); i++) {
			if (!tempList.contains(list.get(i))) {
				return false;
			}
		}

		return true;
	}
}

缩位求和

image-20220315131644699

题目链接:https://www.lanqiao.cn/problems/181/learning/

package daily;

import java.util.Scanner;

/**
 * https://www.lanqiao.cn/problems/181/learning/
 * 
 * @author Jia
 *
 */
public class day3_15_2 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.next();// 因为读进来的最大有1000位,所以只能用string存了
		sc.close();

		while (str.length() != 1) {
			int temp = 0;// 这里可以定义为整数,1000位全为9加起来也才9000,不会越界
			for (int i = 0; i < str.length(); i++) {
				temp += str.charAt(i) - '0';
			}
			str = Integer.toString(temp);// 转化成string继续循环
		}
		System.out.println(str);
	}
}

积木大赛

image-20220315133436755

题目链接:https://www.lanqiao.cn/problems/384/learning/

package daily;

import java.util.Scanner;

/**
 * https://www.lanqiao.cn/problems/384/learning/
 * 
 * @author Jia
 *
 */
public class day3_15_3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] height = new int[n];// 记录大厦的高度
		for (int i = 0; i < n; i++) {
			height[i] = sc.nextInt();
		}
		sc.close();

		int ans = height[0];
		for (int i = 1; i < height.length; i++) {
			if (height[i] > height[i - 1]) {
				ans += height[i] - height[i - 1];// 如果后一个大厦比前一个大厦高那么就加上两个大厦的差值
			}
		}
		System.out.println(ans);
	}
}

举报

相关推荐

0 条评论