HNUCM 2024年春季学期《算法分析与设计》练习14

问题 A: 1的个数

时间限制 : 1.000 sec  内存限制 : 128 MB     [命题人 : admin]

题目描述

输入一个int型的正整数,计算出该int型数据在内存中存储时1的个数。

输出

这个数转换成2进制后,输出1的个数。

样例输入 Copy
5
样例输出 Copy
2
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int n = sc.nextInt();
            String binaryString = Integer.toBinaryString(n);
            char[] arr = binaryString.toCharArray();
            int ans = 0;
            for(int i = 0; i < arr.length ; i++){
                if(arr[i]=='1'){
                    ans++;
                }
            }
            System.out.println(ans);
        }
    }

}

问题 B: 又一道简单题

时间限制 : 5.000 sec  内存限制 : 128 MB     [命题人 : 外部导入]

题目描述

输入一个四个数字组成的整数 n,你的任务是数一数有多少种方法,恰好修改一个数字,把它 变成一个完全平方数(不能把首位修改成 0)。比如 n=7844,有两种方法:3844=622 和 7744=882。 

输入

输入第一行为整数 T (1<=T<=1000),即测试数据的组数,以后每行包含一个整数 n (1000<=n<=9999)。 

输出

对于每组数据,输出恰好修改一个数字,把 n变成完全平方数的方案数

样例输入 Copy
2
7844
9121
样例输出 Copy
Case 1: 2
Case 2: 0
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int n = sc.nextInt();
            int[] arr = new int[n];
            int[] method = new int[n];
            for(int i = 0; i < n; i++){
                arr[i] = sc.nextInt();
            }
            for(int i = 0; i < n; i++){
                int one = arr[i] / 1000;
                int two = arr[i] /100 %10;
                int three = arr[i] %100 /10;
                int four = arr[i]%10;
                for(int j = 0; j <= 9; j++){
                    if(j != 0 && j != one){
                        double b = Math.sqrt(j*1000+two*100+three*10+four);
                        if(b%1==0){
                            method[i]++;
                        }
                    }
                    if(j != two){
                        double b = Math.sqrt(one*1000+j*100+three*10+four);
                        if(b%1==0){
                            method[i]++;
                        }
                    }
                    if(j != three){
                        double b = Math.sqrt(one*1000+two*100+j*10+four);
                        if(b%1==0){
                            method[i]++;
                        }
                    }
                    if(j != four){
                        double b = Math.sqrt(one*1000+two*100+three*10+j);
                        if(b%1==0){
                            method[i]++;
                        }
                    }
                }

            }
            for(int i = 0; i < n; i++){
                System.out.println("Case "+(i+1)+": "+method[i]);
            }

        }
    }
}

问题 C: 安置路灯

时间限制 : 1.000 sec  内存限制 : 128 MB     [命题人 : admin]

题目描述

小Q正在给一条长度为n的道路设计路灯安置方案。

为了让问题更简单,小Q把道路视为n个方格,需要照亮的地方用'.'表示, 不需要照亮的障碍物格子用'X'表示。

小Q现在要在道路上设置一些路灯, 对于安置在pos位置的路灯, 这盏路灯可以照亮pos - 1, pos, pos + 1这三个位置。

小Q希望能安置尽量少的路灯照亮所有'.'区域, 希望你能帮他计算一下最少需要多少盏路灯。

输入
输入的第一行包含一个正整数t(1 <= t <= 1000), 表示测试用例数
接下来每两行一个测试数据, 第一行一个正整数n(1 <= n <= 1000),表示道路的长度。
第二行一个字符串s表示道路的构造,只包含'.'和'X'。
输出
对于每个测试用例, 输出一个正整数表示最少需要多少盏路灯。
样例输入 Copy
2
3
.X.
11
...XX....XX
样例输出 Copy
1
3
import java.util.*;
public class Main {
    public static void main(String[] args) {
        char[][] arr = new char[1000][1000];
        Scanner sc = new Scanner(System.in);
//        while (sc.hasNext()){
            int n = sc.nextInt();
            for(int i = 0; i < n; i++){
                int temp = sc.nextInt();
                char[] t = sc.next().toCharArray();
                System.arraycopy(t,0,arr[i],0,t.length);
                int count = 0;
                for(int j = 0; j < temp; j++){
                    if(arr[i][j] == '.'){
                        count++;
                        j += 2;
                    }
                }
                System.out.println(count);
            }
//        }
    }
}

问题 D: 单源最短路径问题

时间限制 : 1.000 sec  内存限制 : 128 MB     [命题人 : 201501010119]

题目描述

编程实现Dijkstra算法,求一个有向加权图中,从源点出发到其他各个顶点的最短路径。

输入

第1行第1个值表示顶点个数,第2个值表示边个数;第2行开始为边(两个顶点,边的起点和终点)及权重。

输出

顶点0到每一个顶点的最短路径长度。

样例输入 Copy
5 7
0 1 10
0 3 30
0 4 100
1 2 50
2 4 10
3 2 20
3 4 60
样例输出 Copy
0 10 50 30 60
import java.util.*;
public class Main {
    static int[][] Edge;
    static int[] used;
    static int[] dis;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
//        while (sc.hasNext()){
            int d = sc.nextInt();
            Edge = new int[d][d];
            used = new int[d];
            dis = new int[d];
            int e = sc.nextInt();
            for(int i = 0; i < d; i++){
                for(int j = 0; j < d; j++){
                    if(i==j){
                        Edge[i][j] = 0;
                    }else Edge[i][j] = 1000000;
                }
            }
            for(int i = 0; i < e; i++){
                Edge[sc.nextInt()][sc.nextInt()] = sc.nextInt();
            }
            for(int i = 0; i < d; i++) {
                dis[i] = Edge[0][i];
            }
            int k = 0;
            for(int i = 0; i < d; i++){
                int min = 100000;
                for(int j = 0; j < d; j++)
                    if(used[j]!=1&&dis[j]<min){
                        min = dis[j];
                        k = j;
                    }
                used[k] = 1;
                for(int j = 0; j < d; j++){
                        if(used[j]!=1&&dis[k]+Edge[k][j]<dis[j]){
                            dis[j] = dis[k] + Edge[k][j];
                        }
                }



            }
            for(int i = 0; i < d; i++){
                System.out.print(dis[i]+" ");
            }
        System.out.println();



//        }
    }
}

问题 E: ABC + DEF = GHI

时间限制 : 1.000 sec  内存限制 : 128 MB     [命题人 : 201501010119]

题目描述

用1, 2, 3...9 这九个数字组成一个数学公式,满足:ABC + DEF = GHI,每个数字只能出现一次,编写程序输出所有的组合。

输入

输出

输出所有的 ABC + DEF = GHI,
每行一条数据,格式为ABC+DEF=GHI
输出结果按照ABC升序排列,如果ABC相同,则按照DEF升序排列。

import java.util.*;

class Node {
    int x, y, z;
    
    public Node(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

class Main {
    static int num = 0;
    static int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    static Node[] node = new Node[362880]; // 9! / (6! * 2!) = 362880
    
    static class NodeComparator implements Comparator<Node> {
        public int compare(Node a, Node b) {
            if (a.x != b.x)
                return a.x - b.x;
            else
                return a.y - b.y;
        }
    }
    
    static void swap(int[] arr, int x, int y) {
        int temp = arr[x];
        arr[x] = arr[y];
        arr[y] = temp;
    }
    
    static void find(int x, int y) {
        if (x == y) {
            int x1 = a[0] * 100 + a[1] * 10 + a[2];
            int x2 = a[3] * 100 + a[4] * 10 + a[5];
            int x3 = a[6] * 100 + a[7] * 10 + a[8];
            if (x1 + x2 == x3) {
                node[num++] = new Node(x1, x2, x3);
            }
            return;
        }
        for (int i = x; i <= y; i++) {
            swap(a, i, x);
            find(x + 1, y);
            swap(a, i, x);
        }
    }
    
    public static void main(String[] args) {
        num = 0;
        find(0, 8);
        Arrays.sort(node, 0, num, new NodeComparator());
        for (int i = 0; i < num; i++) {
            System.out.printf("%d+%d=%d\n", node[i].x, node[i].y, node[i].z);
        }
    }
}

问题 F: 油田问题

时间限制 : 1.000 sec  内存限制 : 128 MB     [命题人 : 201501010119]

题目描述
输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块。如果两个字符“@”所在的格子相邻(横、竖或者对角线方向),即属于同一个八连块。
输入

多组输入
输入行数m,以及列数n。
然后输入*和@
1<=n,m<=100

输出

联通块个数

样例输入 Copy
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
样例输出 Copy
2
import java.util.Scanner;

public class Main {
    private static char[][] pic;
    private static int[][] idx;
    private static int count = 0;
    private static int m, n;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            m = scanner.nextInt();
            n = scanner.nextInt();
            pic = new char[m][n];
            idx = new int[m][n];

            for (int i = 0; i < m; i++) {
                pic[i] = scanner.next().toCharArray();
            }

            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (idx[i][j] == 0 && pic[i][j] == '@') {
                        solve(i, j, ++count);
                    }
                }
            }

            System.out.println(count);
            count = 0;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    idx[i][j] = 0;
                }
            }
        }
        scanner.close();
    }

    private static void solve(int r, int c, int id) {
        if (r < 0 || r >= m || c < 0 || c >= n) return;
        if (idx[r][c] > 0 || pic[r][c] != '@') return;
        idx[r][c] = id;
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (i != 0 || j != 0) solve(r + i, c + j, id);
            }
        }
    }
}

  • 7
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您讲解一下 HNUCM 最小生成树(Prim)的算法过程。 首先,Prim 算法是一种贪心算法,用于求解带权无向连通图的最小生成树。下面是具体的算法过程: 1. 选取任意一个顶点作为起点,将其加入到生成树中。 2. 找到与已经加入到生成树中的顶点相邻的所有顶点,并计算它们与生成树的距离。 3. 从这些顶点中选取距离最小的顶点,将其加入到生成树中。 4. 重复步骤2和3,直到生成树中包含了所有的顶点。 在具体实现时,可以使用一个数组来记录每个顶点是否已经加入到生成树中,以及另一个数组来记录每个顶点与生成树的距离。每次选择距离最小的顶点时,可以使用一个最小堆来优化。 至于 HNUCM 最小生成树 Prim 算法的具体实现,可以参考以下代码: ```python import heapq def prim(graph): n = len(graph) visited = [False] * n distance = [float('inf')] * n distance[0] = 0 heap = [(0, 0)] result = 0 while heap: (d, u) = heapq.heappop(heap) if visited[u]: continue visited[u] = True result += d for v, weight in graph[u]: if not visited[v] and weight < distance[v]: distance[v] = weight heapq.heappush(heap, (weight, v)) return result ``` 这段代码实现了 HNUCM 最小生成树 Prim 算法的过程,其中 graph 是一个邻接表表示的带权无向连通图,每个元素是一个二元组 (v, w),表示从节点 u 到节点 v 有一条边权为 w 的边。算法的返回值是最小生成树的总权值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值