博客
关于我
算法笔记_209:第六届蓝桥杯软件类决赛部分真题(Java语言B组)
阅读量:441 次
发布时间:2019-03-06

本文共 6815 字,大约阅读时间需要 22 分钟。

以下是一些编程问题及其解答和示例代码:

1. 分机号

问题描述:某公司电话分机号为3位数,要求满足降序排列且无重复数字。例如:751,520,321符合要求,而766,918,201则不符合。计算满足条件的分机号总数。

解答:通过三重循环,计算满足条件的排列组合数。具体来说,首先选择第一个数字(9-0),然后第二个数字必须小于第一个数字且不重复,最后第三个数字必须小于第二个数字且不重复。最终计算得出总数为120。

示例代码

public class Main {    public static void main(String[] args) {        int count = 0;        for(int a = 0; a < 10; a++) {            for(int b = 0; b < 10; b++) {                for(int c = 0; c < 10; c++) {                    if(a > b && b > c) {                        count++;                    }                }            }        }        System.out.println(count);    }}

2. 五星填数

问题描述:在五星图案的10个节点中填入数字1-12,排除7和11。要求每条直线上的数字和相等,且旋转或镜像后相同的算同一种填法。计算满足条件的填法总数。

解答:通过深度优先搜索(DFS)遍历所有可能的填数方式,剪枝排除不符合条件的解。最终计算得出满足条件的填法总数为12。

示例代码

public class Main {    public static int count = 0;    public void swap(int[] A, int i, int j) {        int temp = A[i];        A[i] = A[j];        A[j] = temp;    }    public void check(int[] A) {        int sum1 = A[0] + A[2] + A[5] + A[8];        int sum2 = A[0] + A[3] + A[6] + A[9];        int sum3 = A[1] + A[2] + A[3] + A[4];        int sum4 = A[1] + A[5] + A[7] + A[9];        int sum5 = A[4] + A[6] + A[7] + A[8];        if(sum1 == sum2 && sum1 == sum3 && sum1 == sum4 && sum1 == sum5) {            count++;        } else {            return;        }    }    public void dfs(int[] A, int step) {        if(step == A.length) {            check(A);            return;        } else {            for(int i = step; i < A.length; i++) {                swap(A, i, step);                dfs(A, step + 1);                swap(A, i, step);            }        }    }    public static void main(String[] args) {        Main test = new Main();        int[] A = {1,2,3,4,5,6,8,9,10,12};        test.dfs(A, 0);        System.out.println(count / 10);    }}

3. 表格计算

问题描述:编写一个类似Excel的程序,支持三种公式(SUM、AVG、STD)。输入一个n行m列的表格,计算每个格子的值。公式要求不会出现嵌套,且输入保证不会有循环依赖。

解答:通过解析公式,逐个计算每个格子的值。对于每个公式,分别实现求和、平均和标准差的计算方法。最终输出每个格子的保留两位小数的结果。

示例代码

import java.util.ArrayList;import java.util.Scanner;public class Main {    public static int n, m;    public static double[][] value;    public double getSum(int x1, int y1, int x2, int y2) {        double sum = 0;        for(int i = x1; i <= x2; i++) {            for(int j = y1; j <= y2; j++) {                sum += value[i][j];            }        }        return sum;    }    public double getAvg(int x1, int y1, int x2, int y2) {        int count = Math.abs((x2 - x1 + 1) * (y2 - y1 + 1));        double avg = getSum(x1, y1, x2, y2) / count;        return avg;    }    public double getStd(int x1, int y1, int x2, int y2) {        int count = Math.abs((x2 - x1 + 1) * (y2 - y1 + 1));        double avg = getAvg(x1, y1, x2, y2);        double result = 0;        for(int i = x1; i <= x2; i++) {            for(int j = y1; j <= y2; j++) {                result += (value[i][j] - avg) * (value[i][j] - avg);            }        }        result = Math.sqrt(result / count);        return result;    }    public boolean check(int x1, int y1, int x2, int y2) {        boolean judge = true;        for(int i = x1; i <= x2; i++) {            if(!judge) break;            for(int j = y1; j <= y2; j++) {                if(value[i][j] == -1) {                    judge = false;                    break;                }            }        }        return judge;    }    public String[] getOperaAndNum(String arrayA) {        int p = arrayA.indexOf("(");        int q = arrayA.indexOf(")");        String opera = arrayA.substring(0, p);        String[] num = arrayA.substring(p + 1, q).split(",");        String[] result = new String[5];        result[0] = opera;        for(int i = 0; i < 4; i++) {            result[i + 1] = num[i];        }        return result;    }    public void getResult(String[] A) {        value = new double[n][m];        ArrayList list = new ArrayList();        for(int i = 0; i < n; i++) {            for(int j = 0; j < m; j++) {                value[i][j] = -1;            }        }        for(int i = 0; i < A.length; i++) {            String[] arrayA = A[i].split(" ");            for(int j = 0; j < arrayA.length; j++) {                String strValue = arrayA[j];                if(strValue.charAt(0) >= '0' && strValue.charAt(0) <= '9') {                    value[i][j] = Double.valueOf(strValue);                } else {                    String[] r = getOperaAndNum(strValue);                    String opera = r[0];                    int x1 = Integer.valueOf(r[1]) - 1;                    int y1 = Integer.valueOf(r[2]) - 1;                    int x2 = Integer.valueOf(r[3]) - 1;                    int y2 = Integer.valueOf(r[4]) - 1;                    if(!check(x1, y1, x2, y2)) {                        list.add(i + " " + j + " " + strValue);                        continue;                    }                    if(opera.equals("SUM")) {                        value[i][j] = getSum(x1, y1, x2, y2);                    } else if(opera.equals("AVG")) {                        value[i][j] = getAvg(x1, y1, x2, y2);                    } else if(opera.equals("STD")) {                        value[i][j] = getStd(x1, y1, x2, y2);                    }                }            }        }        while(!list.isEmpty()) {            for(int i = list.size() - 1; i >= 0; i--) {                String[] temp = list.get(i).split(" ");                int a = Integer.valueOf(temp[0]);                int b = Integer.valueOf(temp[1]);                String[] r = getOperaAndNum(temp[2]);                String opera = r[0];                int x1 = Integer.valueOf(r[1]) - 1;                int y1 = Integer.valueOf(r[2]) - 1;                int x2 = Integer.valueOf(r[3]) - 1;                int y2 = Integer.valueOf(r[4]) - 1;                if(!check(x1, y1, x2, y2)) {                    continue;                }                if(opera.equals("SUM")) {                    value[a][b] = getSum(x1, y1, x2, y2);                } else if(opera.equals("AVG")) {                    value[a][b] = getAvg(x1, y1, x2, y2);                } else if(opera.equals("STD")) {                    value[a][b] = getStd(x1, y1, x2, y2);                }                list.remove(i);            }        }        for(int i = 0; i < n; i++) {            for(int j = 0; j < m; j++) {                System.out.printf("%.2f", value[i][j]);                if(j != m - 1) {                    System.out.print(" ");                }            }            System.out.println();        }    }    public static void main(String[] args) {        Main test = new Main();        Scanner in = new Scanner(System.in);        n = in.nextInt();        m = in.nextInt();        in.nextLine();        String[] A = new String[n];        for(int i = 0; i < n; i++) {            A[i] = in.nextLine();        }        test.getResult(A);    }}

转载地址:http://jztyz.baihongyu.com/

你可能感兴趣的文章
NIH发布包含10600张CT图像数据库 为AI算法测试铺路
查看>>
Nim教程【十二】
查看>>
Nim游戏
查看>>
NIO ByteBuffer实现原理
查看>>
Nio ByteBuffer组件读写指针切换原理与常用方法
查看>>
NIO Selector实现原理
查看>>
nio 中channel和buffer的基本使用
查看>>
NIO三大组件基础知识
查看>>
NIO与零拷贝和AIO
查看>>
NIO同步网络编程
查看>>
NIO基于UDP协议的网络编程
查看>>
NIO笔记---上
查看>>
NIO蔚来 面试——IP地址你了解多少?
查看>>
NISP一级,NISP二级报考说明,零基础入门到精通,收藏这篇就够了
查看>>
NISP国家信息安全水平考试,收藏这一篇就够了
查看>>
NIS服务器的配置过程
查看>>
Nitrux 3.8 发布!性能全面提升,带来非凡体验
查看>>
NiuShop开源商城系统 SQL注入漏洞复现
查看>>
NI笔试——大数加法
查看>>
NLog 自定义字段 写入 oracle
查看>>