蓝桥杯——2

编程入门 行业动态 更新时间:2024-10-05 23:19:04

<a href=https://www.elefans.com/category/jswz/34/1769450.html style=蓝桥杯——2"/>

蓝桥杯——2

  • 猜年龄

    小明带两个妹妹参加元宵灯会。别人问她们多大了,她们调皮地说:“我们俩的年龄之积是年龄之和的6倍”。小明又补充说:“她们可不是双胞胎,年龄差肯定也不超过8岁啊。”
    请你写出:小明的较小的妹妹的年龄。

    public class Main{public static void main(String[] args) {for(int i = 1; i <= 100; i++){for(int j = 1; j <= 8; j++){if(i * (i + j) == (2 * i + j) * 6){System.out.println(i);}}}}
    }
    
  • 李白打酒

    话说大诗人李白,一生好饮。幸好他从不开车。
    一天,他提着酒壶,从家里出来,酒壶中有酒2斗。他边走边唱:
    无事街上走,提壶去打酒。
    逢店加一倍,遇花喝一斗。
    这一路上,他一共遇到店5次,遇到花10次,已知最后一次遇到的是花,他正好把酒喝光了。
    请你计算李白遇到店和花的次序,可以把遇店记为a,遇花记为b。则:babaabbabbabbbb 就是合理的次序。像这样的答案一共有多少呢?请你计算出所有可能方案的个数(包含题目给出的)。
    注意:通过浏览器提交答案。答案是个整数。不要书写任何多余的内容。

    public class libaidajiu2 {private static int counter;public static void main(String[] args) {counter = 0;libai(14, 2, 5, 10);System.out.println(counter);}//step表示遇到花和店的次数,又因最后一次必须是花,所以step=14,(即当step=0的时候,条件成立);// jiu表示还剩多少斗酒,dian表示还剩多少次店,hua表示还剩多少次花private static void libai(int step, int jiu, int dian, int hua) {//递归出口if (step < 0 || jiu < 0 || dian < 0 || hua < 1) {return;}//根据题意酒剩1斗,最后一次是花,则条件成立(即最后一次遇到花,把酒喝光)if (step == 0 && jiu == 1 && dian == 0 && hua == 1) {counter++;return;}libai(step - 1, jiu - 1, dian, hua - 1); // 遇到花了libai(step - 1, jiu * 2, dian - 1, hua); // 遇到店了}
    }
    
  • 神奇算式

    由4个不同的数字,组成的一个乘法算式,它们的乘积仍然由这4个数字组成。
    比如:
    210 x 6 = 1260
    8 x 473 = 3784
    27 x 81 = 2187
    一共有多少种满足要求的算式

    package mec.lanqiao;import java.util.*;public class Main {static int cnt = 0;// 判断一个数组里的元素是否各不相同static boolean isBuTong(int[] x) {Set<Integer> set = new HashSet<>();for (int i = 0; i < x.length; i++) {set.add(x[i]);}return x.length == set.size();}public static void main(String[] args) {for (int n = 1000; n < 9999; n++) {int[] store = { n / 1000, n / 100 % 10, n / 10 % 10, n % 10 };Arrays.sort(store); // 对数组进行排序if (isBuTong(store)) { // 各位数字各不相同// 找较小乘数为1位数字的情况for (int i = 0; i < store.length; i++) {if (store[i] == 0) // 第一个数字为1位数,不能为0continue;// 判断商能否被第一个数整除,并将两个乘数的各位数字放到数组nStore中,比较nStore里的元素与store里是否完全相同if (n % store[i] == 0 && n / store[i] / 100 < 10) {int t = n / store[i];int[] nStore = { store[i], t / 100, t / 10 % 10, t % 10 };Arrays.sort(nStore);boolean f = true;for (int j = 0; j < 4; j++) {if (store[j] != nStore[j]) {f = false;break;}}if (f) {cnt++; // 相同则cnt加一System.out.println(store[i] + "x" + t + "=" + n);}}}// 找较小乘数为2位数字的情况for (int i = 0; i < store.length; i++) {if (store[i] == 0) // 第一个乘数十位数不能为0continue;for (int j = 0; j < store.length; j++) {int first = store[i] * 10 + store[j]; // 第一个乘数if (n % first == 0 && n / first / 10 < 10) {int sec = n / first; // 第二个乘数int[] nStore = { store[i], store[j], sec / 10,sec % 10 };Arrays.sort(nStore);boolean f = true;for (int k = 0; k < nStore.length; k++) {if (store[k] != nStore[k]) {f = false;break;}}if (f && first <= sec) {cnt++; // 相同则cnt加一System.out.println(first + "x" + sec + "=" + n);}}}}}}System.out.println(cnt + "种");}
    }
  • 写日志

    写日志是程序的常见任务。现在要求在 t1.log, t2.log, t3.log 三个文件间轮流写入日志。也就是说第一次写入t1.log,第二次写入t2.log,… 第四次仍然写入t1.log,如此反复。

    public class Main
    {private static int n = 1;public static void main(String args[]){for (int i = 0; i<=100; i++) {
          write(i,"1111");}}public static void write(int n, String msg){n = n%3+1;String filename = "t" + n + ".log";System.out.println("write to file: " + filename + " " + msg);}
    }
    
  • 六角填数

    在六角形中,填入1~12的数字,使得每条直线上的数字之和都相同。计算位置*位置代表的数字是多少

    public class 六角填数
    {private static long Begin = 0;       //与本题无关,只是计算一下程序总共耗时多长时间private static long End = 0;		 //与本题无关,只是计算一下程序总共耗时多长时间public static void Swap(int[] c,int i,int j){int tmp = c[i];c[i] = c[j];c[j] = tmp;}public static void AllPermutation(int[] c,int start){if(start==c.length-1)        //说明已找到一个排列{if(c[0]==8&&c[3]==3&&c[4]==1)     //判断六角形的下标(0,3,4)对应的值是否符合已给的三个值{int A = c[0]+c[1]+c[2]+c[3];   //第一条边的和int B = c[4]+c[5]+c[1]+c[6];   //第二条边的和int C = c[0]+c[5]+c[7]+c[8];   //第三条边的和int D = c[4]+c[7]+c[9]+c[11];  //第四条边的和 int E = c[8]+c[9]+c[10]+c[3];  //第五条边的和int F = c[6]+c[2]+c[10]+c[11]; //第六条边的和if(A==B&&B==C&&C==D&&D==E&&E==F){for(int i=0,t=c.length;i<t;++i)  //把所有下标的值都输出{if(i!=0){System.out.print(" ");}System.out.print(c[i]);}System.out.println("\nc[1]:"+c[1]);   //所求*值End = System.currentTimeMillis() - Begin;   //与本题无关,只是计算一下程序总共耗时多长时间System.out.println("耗时:"+End+"ms");   //总共耗时System.exit(0);						//退出java虚拟机}}}else{for(int i=start,t=c.length;i<t;++i){Swap(c,i,start);AllPermutation(c,start+1);Swap(c,start,i);}}}public static void main(String[] args){int[] c = {8,2,4,3,1,5,6,7,9,10,11,12};   //根据六角形的下标位置赋值Begin = System.currentTimeMillis();    //与本题无关,只是计算一下程序总共耗时多长时间AllPermutation(c,0);      //全排列算法}
    
  • 绳圈

    今有 100 根绳子,当然会有 200 个绳头。
    如果任意取绳头两两配对,把所有绳头都打结连接起来。最后会形成若干个绳圈(不考虑是否套在一起)。
    我们的问题是:请计算最后将形成多少个绳圈的概率最大?

    public class Main{public static void main(String[] args) {double[][] dp = new double[101][101];dp[1][1] = 1;  //当前只有一根绳子,只能形成一个绳圈,且概率为1/**  有* */for(int i = 2;i < 101;i++) {  //绳子数for(int j = 1;j <= i;j++) {  //绳圈数if(i == j) { dp[i][j] = 1;}dp[i][j] = dp[i - 1][j]*(2*i - 2) / (2*i - 1) + dp[i][j -1]/(2*i - 1);}}double max = 0;int maxI = 0;for(int i  = 1;i < 101;i++) {if(max < dp[100][i]) {max = dp[100][i];maxI = i;}}System.out.println(maxI);}
    }

更多推荐

蓝桥杯——2

本文发布于:2024-03-12 20:44:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1732382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:蓝桥杯

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!