竞码编程——蓝桥杯模拟赛2

	此赛有两道未解决,一道未看懂,还有一道未用Excel解决掉。大部分是暴力解法,今后再刷题掌握其他算法(全排列、dp......)回过头来继续完善此赛代码。。

A:完美车牌

package oj.hzjingma.com.simulation2;
//	其实这道题可以用数学中的排列组合方法来做:要求六位数旋转180度后仍是原来数,,则取决于该数的前三位,前三位一定后后三位也就一定了。而前三位每一位都有5种可能情况(0、1、6、8、9),一共可能的情况有5*5*5=125种情况。
//	虽然说这是模拟题,但是不可否认的是编程与数学有很大的关系,平时要多多注重数学方面的学习与提升!!
import java.util.Scanner;
//完美车牌
public class A {
	public static void main(String[] args) {
		int count=0;
		//此代码运行结果不对,原因:
		//刚开始就想错了,题目要求的是六位数的车牌号,而不是100000-999999的六位数的数字,二者有区别,比如000000可以是车牌号,却不是100000-999999的数字!!所以以下解法有漏掉的情况。。
		for (int i = 1000000; i <=2000000; i++) {
			boolean flag=true;
			String string=Integer.toString(i);
			for (int j = 1; j <=string.length()/2; j++) {
				char qian=string.charAt(j-1);
				char hou=string.charAt(string.length()-j);
				if ( ! (((qian==hou)&&(qian=='0'||qian=='1'||qian=='8')) || (qian=='6'&&hou=='9') ||(qian=='9'&&hou=='6'))  ) {
					flag=false;
					break;
				}
			}
			if (flag) {
				count++;
			}
		}
		System.out.println(count);
	}
}

B:完美日期(还未用Excel解决,要掌握Excel解蓝桥杯题的技巧!!)

package oj.hzjingma.com.simulation2;
//要掌握Excel解蓝桥杯题的技巧(!!!)
//完美日期
//难道只能模拟吗?只能一个一个地推算不成??!

//年月日中均没有出现数字4,
//年月日的数位之和是8的倍数
//例如:2020/02/02 就是一个完美日期,没有出现数字4,且数位之和是8的倍数。
//wlxsq想知道从2020/02/22开始,第88个完美日期是哪个?
public class B {
	
	public static void main(String[] args) {
		int y=2020;
		int m=2;
		int d=22;
		int ans=0;
		while(true) {
			if ((m==1||m==3||m==5||m==7||m==8||m==10||m==12)&&d==32) {
				d=1;
				m++;
				if(m==13) {
					m=1;
					y++;
				}
			}else if ((m==4||m==6||m==9||m==11)&&d==31) {
				d=1;
				m++;
			}else if ((m==2&&(y%4==0&&y%100!=0||y%400==0))&&d==30) {
				d=1;
				m++;
			}else if ((m==2&&d==29)) {
				d=1;
				m++;
			}
			if (judge(y,m,d)) {
				ans++;
			}
			if (ans==88) {
				System.out.println(y+","+m+","+d);
				break;
			}
			d++;
		}
	}
	private static boolean judge(int y,int m,int d) {
		int sum=0;
		while(y>0) {
			if(y%10==4) {
				return false;
			}
			sum+=(y%10);
			y/=10;
			
		}
		while(m>0) {
			if(m%10==4) {
				return false;
			}
			sum+=(m%10);
			m/=10;
			
		}
		while(d>0) {
			if(d%10==4) {
				return false;
			}
			sum+=(d%10);
			d/=10;
			
		}
		if(sum%8==0) {
			return true;
		}
		return false;
	}
}

C:天机锁

package oj.hzjingma.com.simulation2;
//天机锁
//4和9要么都出现,要么都不出现,且出现则数量得一样多
//数字2一定出现
//八个数字之和不超过52
//总共有多少种方案~
public class C {
	public static void main(String[] args) {
		int count=0;
		for (int i = 0; i <=99999999; i++) {
			String str = String.format("%08d", i); 
			if (str.contains("2")) {
				if (geshu(str, '4')==geshu(str, '9')) {
					if (sum(str)<=52) {
						count++;
					}
				}
			}
		}
		System.out.println(count);
	}
	private static int geshu(String str,char ch) {
		int len=str.length();
		int count=0;
		for (int i = 0; i < len; i++) {
			if (str.charAt(i)==ch) {
				count++;
			}
		}
		return count;
	}
	private static int sum(String str) {
		int res=0;
		for (int i = 0; i < str.length(); i++) {
			int n=Integer.valueOf(str.charAt(i))-'0';//返回的是字符对应的ASCII
//			System.out.println(Integer.parseInt("1"));返回的是int型本身
			res+=n;
		}
		return res;
	}
}

D:完美运算

package oj.hzjingma.com.simulation2;
//完美运算
//暴力求解
public class D {
	public static void main(String[] args) {
		int res=0;
		for (int i = 1; i <=2020; i++) {
			for (int j = i; j <=2020; j++) {
				String si=Integer.toString(i, 3);
				String sj=Integer.toString(j, 3);
				int left=Math.abs(sum_1(si)-sum_2(si));
				int right=Math.abs(sum_1(sj)-sum_2(sj));
				if (left==right) {
					res++;
				}
			}
		}
		System.out.println(res);
	}
	private static int sum_1(String str) {
		int count=0;
		int len=str.length();
		for (int i = 0; i < len; i++) {
			if (str.charAt(i)=='1') {
				count++; 
			}
		}
		return count;
	}
	private static int sum_2(String str) {
		int count=0;
		int len=str.length();
		for (int i = 0; i < len; i++) {
			if (str.charAt(i)=='2') {
				count++; 
			}
		}
		return count;
	}
}

E:三叉神树(全排列)

package oj.hzjingma.com.simulation2;
//三叉神树
public class E {
	public static void test(int[] a, int k) {
		if(k==a.length) {
			if(check(a)) {
				count++;
			}
			return;
		}
		for (int i = k; i < a.length; i++) {
			exch(a, i, k);
			test(a, k+1);
			exch(a, i, k);
		}
	}

	public static void exch(int[] a, int x, int y) {
		int k = a[x];
		a[x] = a[y];
		a[y] = k;
	}
	
	public static boolean check(int[] a) {
		if((a[1]+a[4])%2==0 && (a[2]+a[5]+a[6]+a[9]+a[10])%2==0 && (a[3]+a[7]+a[8]+a[11])%2==0 && (a[6]+a[9]+a[10])%2==0 && (a[8]+a[11])%2==0) {
			return true;
		}
		return false;
	}

	static int count = 0;

	public static void main(String[] args) {
		int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
		test(a, 0);
		System.out.println(count);
	}
}

F:JM斗牛

package oj.hzjingma.com.simulation2;
//JM斗牛
//暴力求解
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class F {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String[] sa=new String[5];
		int[] ia=new int[5];
		for (int i = 0; i < 5; i++) {
			sa[i]=sc.next();
		}
		for (int i = 0; i < 5; i++) {
			if (sa[i].equals("A")) {
				sa[i]="1";
			}else if (sa[i].equals("S")||sa[i].equals("J")||sa[i].equals("Q")||sa[i].equals("K")) {
				sa[i]="10";
			}
			ia[i]=Integer.parseInt(sa[i]);
		}
		niu(ia);
	}
	public static void niu(int[] arr) {
		int i=0;
		int j=0;
		int k=0;
		boolean flag=true;
		a:for (i = 0; i < arr.length; i++) {
			for (j = i+1; j < arr.length; j++) {
				for (k = j+1; k < arr.length; k++) {
					int sum=arr[i]+arr[j]+arr[k];
					if (sum%10==0) {
						flag=false;
						break a;
					}
				}
			}
		}
		//为啥通过变量a跳出最外层循环后,不能代码展开了???
		if(flag) {
			System.out.println("so sad!");
		}
		else {
			int res=arr[0]+arr[1]+arr[2]+arr[3]+arr[4]-arr[i]-arr[j]-arr[k];
			int yushu=res%10;
			if(yushu==0) {
				System.out.println("so cool!");
			}else {
				System.out.println(yushu);
			}
		}
		
	}
}

G:JM boy 去爬山

package oj.hzjingma.com.simulation2;
//排列组合公式用递归方法不会写!!
//快速幂!!
//记住:2的n次方=Cn1+Cn2+Cn3+....+Cnn(排列组合)
import java.util.Scanner;

public class G {
//	有一个没有通过,可能是数据太大了,再者,绕远了,没有记住排列组合和2的n次方之间的关系公式的原因造成的!!!直接求2的(n-1)次方(由推导得)即可,不用求Cn(0~n-1)再求和(太麻烦)。。。
//	public static void main(String[] args) {
//		Scanner sc=new Scanner(System.in);
//		int n=sc.nextInt();
//		int[] ia=new int[n];
//		int sum=0;
//		for (int i = 0; i <=n-1; i++) {
//			sum+=digui(n-1, i);
//		}
//		System.out.println(sum);
//	}
//	private static int digui(int n,int k) {
//		int fenmu=jc(k)*jc(n-k);
//		int fenzi=jc(n);
//		int res=fenzi/fenmu;
//		return res;
//	}
//	private static int jc(int n) {
//		int m=1;
//		for (int i = 2; i <=n; i++) {
//			m=m*i;
//		}
//		return m;
//	}
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		long res=2;
		for (int i = 1; i <n-1; i++) {
			res=res*2;
		}
		System.out.println(res);
	}
}

H:宝剑锋从磨砺出

package oj.hzjingma.com.simulation2;
//逻辑太差,想不清楚,其实想清楚后并没有那么复杂!
//贪心算法:
//思路:求方式一的最大值max,如果方式二中有大于max的,先让K减去此方式二,若还有,接着减,直到没有为止。再让剩下的K一次次地减去max(因为方式一的材料可无限次使用),直到K被减到0为止。
import java.util.Arrays;
import java.util.Scanner;

public class H {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int k=sc.nextInt();
		int[][] arr=new int[n][2];
		for (int i = 0; i < arr.length; i++) {
			arr[i][0]=sc.nextInt();
			arr[i][1]=sc.nextInt();
			
		}
		int[] max_l=new int[n];
		for (int i = 0; i < arr.length; i++) {
			max_l[i]=arr[i][0];
		}
		Arrays.sort(max_l);
		int max=max_l[max_l.length-1];
		int count=0;
		for (int i = 0; i < n; i++) {
			if (arr[i][1]>max&&k>0) {
				count++;
				k-=arr[i][1];
			}
		}
		while (k>0) {
			count++;
			k-=max;
		}
		System.out.println(count);
	}
}

I :美味糖果 (未解决,用动态规划dp)
J:梅深不见冬(未解决)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值