目录
一、三角形面积(3/3分)
解题思路:
数学题,比较简单~
代码如下:
public class 三角面积 {
public static void main(String[] args) {
System.out.println(7 * 4 / 2 * 2);
}
}
二、立方变自身(5/5分)
解题思路:
得到每一个位上面的数并累加,如果与这个数本身相等则++。
代码如下:
public class 立方变自身 {
public static void main(String[] args) {
int cnt = 0;
for (int i = 1; i < 10000; i++) {
if(check(i)) {
cnt++;
}
}
System.out.println(cnt);
}
public static boolean check(long n) {
long num = n * n * n;
long sum = 0;
while(num > 0) {
long ge = num % 10;
sum += ge;
num /= 10;
}
if(sum == n) {
return true;
}
else {
return false;
}
}
}
三、三羊献瑞(9/9分)
解题思路1:
每个汉字代表一个数字,一共有八个不同的汉字,所以八层循环表示每一个汉字可能的值,再判断这些汉字带入算式是否符合条件,同时三个数的开头不能是0。
代码如下:
public class 三羊献瑞 {
public static void main(String[] args) {
for (int a = 0; a < 10; a++) {
for (int b = 0; b < 10; b++) {
for (int c = 0; c < 10; c++) {
for (int d = 0; d < 10; d++) {
for (int e = 0; e < 10; e++) {
for (int f = 0; f < 10; f++) {
for (int g = 0; g < 10; g++) {
for (int h = 0; h < 10; h++) {
if(a != b && a != c && a != d && a != e &&
a != f && a != g && a != h &&
b != c && b != d && b != e && b != f &&
b != g && b != h &&
c != d && c != e && c != f && c != g &&
c != h &&
d != e && d != f && d != g && d != h &&
e != f && e != g && e != h &&
f != g && f != h && g != h &&
a * 1000 + b * 100 + c * 10 + d +
e * 1000 + f * 100 + g * 10 + b ==
e * 10000 + f * 1000 + c * 100 + b * 10 + h &&
a != 0 && e != 0) {
System.out.print(e);
System.out.print(f);
System.out.print(g);
System.out.print(b);
}
}
}
}
}
}
}
}
}
}
}
解题思路2:
上面的层层循环让代码很麻烦,所以可以用全排列算法改进。先定义一个长度为10的数组存储
0-9共10个数字,全排列并每次判断是否合规(用数组前八位计算)。
代码如下:
public class a03_三羊献瑞_02 {
public static void main(String[] args) {
int[] arr = {0,1,2,3,4,5,6,7,8,9};
dfs(arr,0);
}
public static void dfs(int[] arr,int n) {//传入数组一个数组arr,n用来确认第一位数字
if(n == arr.length - 1) {
int a = arr[0];
int b = arr[1];
int c = arr[2];
int d = arr[3];
int e = arr[4];
int f = arr[5];
int g = arr[6];
int h = arr[7];
if((a + e) * 1000 + (b + f) * 100 + (c + g) * 10 + (b + d) ==
e * 10000 + f * 1000 + c *100 + b * 10 + h && a != 0 && e != 0) {
System.out.print(e);
System.out.print(f);
System.out.print(g);
System.out.print(b);
System.out.println();
}
}
else {
for (int i = n; i < arr.length; i++) {//从第n位开始,对剩下的数全排列
swap(arr, i, n);//先通过交换确定数组第一位
dfs(arr,n + 1);
swap(arr,i,n);//这轮全排列结束后再交换回来,恢复初始数组
}
}
}
public static void swap(int[] a,int i,int j) {//交换数组中两个索引的元素
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
答案:1085
四、循环节长度(11/11分)
解题思路:
当本次循环得到的数已经在动态数组中存在的时候说明到了下一个循环节,此时返回动态数组的长度减去这个数在动态数组中第一次出现的索引(因为循环节的开始不一定是第一位)。
答案:
return v.size() - v.indexOf(n)
五、九数组分数(15/15分)
解题思路:
一看就是全排列模板,秒了!
代码如下:
{int t=x[k]; x[k]=x[i]; x[i]=t;}
六、加法变乘法(17/17分)
解题思路:
1-49之间共有48个空,需要在这48个空中填入两个不相邻的乘号,其他四十六个空填入加号且满足式子结果等于2015。加入乘号的具体方法为:先在第1个到第46个空格之间加入一个乘号,然后在第一个空格后面第二个空格的位置上加入第二个空格,然后嵌套两层循环,每次在两个不同位置上加入空格都计算一次式子的和,如果等于2015则输出。
代码如下:
public class a05_九数组分数 {
public static void main(String[] args) {
for (int i = 1; i <= 46; i++) {//第一个乘号的位置可以放在第1个数到第46个数右边的空格上
int a = i;//第一个乘号(i右边)
for (int j = i + 2; j <= 48; j++) {//第二个乘号的位置可以放在第1个乘号后面的第二位置上
int b = j;//第二个乘号(j右边)
int sum = 0;
for (int k = 1; k <= 49; k++) {//计算这样放置的结果
if(k == a || k == b) {
sum += k * (k + 1);
k++;
}
else {
sum += k;
}
}
if(sum == 2015) {
System.out.println(a + " " + b);
}
}
}
}
}
结果:
所以本题答案为16。
七、牌型种数(21/21分)
答案:3598180
解题思路:就是4组1-13的牌发到手上有多少种不同组合。
代码如下:
public class a07_牌型种数 {
static int ans = 0;
public static void main(String[] args) {
f(0,0);
System.out.println(ans);
}
//k表示考虑到第几张牌了,cnt表示当前手上一共有几张牌
public static void f(int k,int cnt) {
if(k > 13 || cnt > 13) {
return;
}
if(k == 13 && cnt == 13) {
ans++;
return;
}
for (int i = 0; i < 5; i++) {
f(k + 1,cnt + i);
}
}
}
八、饮料换购(13/13分)
解题思路1:
定义一个计数器cnt记录得到的饮料的数,他的初始值是用户输入的购买饮料的数目n,然后当n大于等于三个时,可以用三个瓶子换一个瓶子,即计数器加加,然后n减少三个,因为又得到了一个瓶盖所以n++,这样循环直到瓶盖数目小于3。
代码如下:
import java.util.Scanner;
public class a08_饮料换购 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int cnt = n;
while(n >= 3) {
cnt++;
n = n - 3;
n++;
}
System.out.println(cnt);
}
}
在蓝桥杯官网看到一个很妙的题解分享一下:
import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
int temp=scan.nextInt();
for(int i=3;i<=temp;i+=3) {
temp++;
}
System.out.println(temp);
scan.close();
}
}
最后两题太难啦看又看不懂鞋又鞋不废
九、垒骰子(0/25分)
十、生命之树(0/31)
总分(94/150分)
=======================3/4日更新,黑色分数为本次更新内容====================
这一套题目相对简单些,总分62,但是有些高频知识点(如DFS)的分数还是拿不到,需要专门学习一下这样很有可能会在省赛中遇到。
=======================3/8日更新,红色分数为本次更新内容====================
二刷94分,还不错嘿嘿。