java算法集训代码填空题练习2

1 连续数的公倍数

为什么1小时有60分钟,而不是100分钟呢?这是历史上的习惯导致。
但也并非纯粹的偶然:60是个优秀的数字,它的因子比较多。
事实上,它是1至6的每个数字的倍数。即1,2,3,4,5,6都是可以除尽60。

我们希望寻找到能除尽1至n的的每个数字的最小整数。

不要小看这个数字,它可能十分大,比如n=100, 则该数为:
69720375229712477164533808935312303556800

为此,有必要使用BigInteger来记录这样的大数。

请阅读下面的代码,填写缺失的部分(下划线部分)。

注意:请把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。
直接写在题面中不能得分。

import java.math.BigInteger;
public class My1
{
    // 求能除尽1~n 每个数字的最小整数
    public static BigInteger f(int n)
    {
        int[] x = new int[n+1];
        
        for(int i=1; i<=n; i++) x[i] = i;
        
        for(int i=2; i<n; i++)
        {
            for(int j=i+1; j<=n; j++)
            {
                if(x[j] % x[i]==0) _______________;   // 填空1
            }
        }
        
        BigInteger m = BigInteger.ONE;
        for(int i=2; i<=n; i++)
        {
            m = m.multiply(__________________);   // 填空2
        }
        
        return m;
            
    }
    
    public static void main(String[] args)
    {
        System.out.println(f(30));    
    }
}


x[j] = x[j] / x[i]
BigInteger.valueOf(x[i])

2 孪生素数

孪生素数

所谓孪生素数指的就是间隔为 2 的相邻素数,它们之间的距离已经近得不能再近了,就象孪生兄弟一样。最小的孪生素数是 (3, 5),在 100 以内的孪生素数还有 (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61) 和 (71, 73),总计有 8 组。但是随着数字的增大,孪生素数的分布变得越来越稀疏,寻找孪生素数也变得越来越困难。那么会不会在超过某个界限之后就再也不存在孪生素数了呢?
孪生素数有无穷多对!这个猜想被称为孪生素数猜想,至今没有被严格证明。但借助于计算机我们确实可以找到任意大数范围内的所有孪生素数对。
下面的代码求出了正整数n以内(不含n)的所有孪生素数对的个数。比如,当n=100的时候,该方法返回8。试补全缺少的代码。

把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。

    public static boolean isPrime(int x)
    {
        for(int i=2; i<=x/2; i++)
        {
            if(x%i==0) _____________;
        }
        return true;
    }
    
    public static int twinPrimeNum(int n)
    {
        int sum = 0;
        for(int i=2; i<n; i++)
        {
            if(isPrime(i) && ___________) sum++;
        }
        
        return sum;
    }


return false
isPrime(i+2) && i + 2 < n

3 迷宫走法

迷宫问题

对于走迷宫,人们提出过很多计算机上的解法。深度优先搜索、广度优先搜索是使用最广的方法。生活中,人们更愿意使用“紧贴墙壁,靠右行走”的简单规则。
下面的代码则采用了另一种不同的解法。它把走迷宫的过程比做“染色过程”。假设入口点被染为红色,它的颜色会“传染”给与它相邻的可走的单元。这个过程不断进行下去,如果最终出口点被染色,则迷宫有解。

仔细分析代码中的逻辑,填充缺少的部分。

把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。

public class Maze
{
    class Cell
    {
        private int row;
        private int col;
        private Cell from;
        
        public Cell(int row, int col, Cell from)
        {
            this.row = row;
            this.col = col;
            this.from = from;
        }
    }
        
    char[][] maze = 
    {{'#','#','#','#','B','#','#','#','#','#','#','#'},
    {'#','#','#','#','.','.','.','.','#','#','#','#'},
    {'#','#','#','#','.','#','#','#','#','.','.','#'},
    {'#','.','.','.','.','#','#','#','#','#','.','#'},
    {'#','.','#','#','#','#','#','.','#','#','.','#'},
    {'#','.','#','#','#','#','#','.','#','#','.','#'},
    {'#','.','#','#','.','.','.','.','.','.','.','#'},
    {'#','.','#','#','.','#','#','#','.','#','.','#'},
    {'#','.','.','.','.','#','#','#','.','#','.','#'},
    {'#','#','.','#','.','#','#','#','.','#','.','A'},
    {'#','#','.','#','#','#','.','.','.','#','#','#'},
    {'#','#','#','#','#','#','#','#','#','#','#','#'}};
    
    public void show()
    {
        for(int i=0; i<maze.length; i++)
        {
            for(int j=0; j<maze[i].length; j++) 
System.out.print(" " + maze[i][j]);
            System.out.println();
        }
    }
    
    //把与from集合中相邻的可染色节点染色,被染色节点记入 dest
    //一旦发现出口将被染色,则返回当前的“传播源”节点
    public Cell colorCell(Set<Cell> from, Set<Cell> dest)
    {
        Iterator<Cell> it = from.iterator();
        while(it.hasNext())
        {
            Cell a = it.next();
            Cell[] c = new Cell[4];
            c[0] = new Cell(a.row-1, a.col, a);
            c[1] = new Cell(a.row, a.col-1, a);
            c[2] = new Cell(a.row+1, a.col, a);
            c[3] = ___________________________;
                        
            for(int i=0; i<4; i++)
            {
                if(c[i].row < 0 || c[i].row >= maze.length) continue;
                if(c[i].col < 0 || c[i].col >= maze[0].length) continue;
                
                char x = maze[c[i].row][c[i].col];
                if(x=='B') return a;
                if(x=='.') 
                {
                    maze[c[i].row][c[i].col] = '?';
                    ____________________;
                }
            }
        }
        return null;
    }
    
    public void resolve()
    {
        Set<Cell> set = new HashSet<Cell>();
        set.add(new Cell(9,11,null));
        
        for(;;)
        {
            Set<Cell> set1 = new HashSet<Cell>();
            Cell a = colorCell(set, set1);
            if(a!=null)
            {
                System.out.println("找到解!");
                while(a!=null)
                {
                    maze[a.row][a.col] = '+';
                    ______________;
                }
                break;
            }
            if(set1.isEmpty())
            {
                System.out.println("无解!");
                break;
            }
            set = set1;
        }        
    }
    
    public static void main(String[] args)
    {
        Maze m = new Maze();
        m.show();
        m.resolve();
        m.show();    
    }
}


new Cell(a.row, a.col+1, a)
dest.add(c[i])
a = a.from

4 拍7游戏

许多人都曾经玩过“拍七”游戏。规则是:大家依次从1开始顺序数数,数到含有7或7的倍数的要拍手或其它规定的方式表示越过(比如:7,14,17等都不能数出),下一人继续数下面的数字。违反规则者受罚。下面的程序模拟这个过程,拍7的情况输出“*”,请完善之。

    for(int i=1; i<100; i++)
    {
        if(i % 7 == 0)
            printf("*
");
        else if(___________________)
            printf("*
");    
        else
            printf("%d
", i);
    }


String.valueOf(i).contains("7")
 或者i/10 == 7 || i%10 == 7

5 排列为平方数

若干不同的数字,排列组合后能产生多少个平方数?
下面的代码解决了这个问题。
对于:1,6,9
排列后,可产生3个平方数:
169
196
961

请阅读下面的代码,填写缺失的部分(下划线部分)。

注意:请把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。
直接写在题面中不能得分。

public class My
{
    public static void f(int[] a, int n)
    {
        if(n==a.length-1)
        {
            int k = 0;
            for(int i=0; i<a.length; i++) k = ____________ + a[i];   // 填空1
            int m = (int)(Math.sqrt(k)+0.5);
            if(m*m==k)
            {
                System.out.println(k);
            }
            return;
        }
        
        for(int i=n; i<a.length; i++)
        {
            int t = a[n];
            a[n] = a[i];
            a[i] = t;
            
            f(a, _______________);        // 填空2
            
            t = a[n];
            a[n] = a[i];
            a[i] = t;
        }
    }
    
    public static void main(String[] args)
    {
        int[] a = {1,9,6};        
        f(a, 0);
    }
}


k * 10
n + 1

6 平面点最小距离

最近距离

已知平面上的若干点的位置,存入一个List中。现在需要计算所有这些点中,距离最近的两个点间的最小距离。请补全缺失的代码。

把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。

class MyPoint
{
    private double x;  // 横坐标
    private double y;  // 纵坐标
        
    public MyPoint(double x, double y)
    {
        this.x = x;
        this.y = y;
    }
    
    public static double distance(MyPoint p1, MyPoint p2)
    {
        double dx = p1.x - p2.x;
        double dy = p1.y - p2.y;        
        return Math.sqrt(dx*dx + dy*dy);
    }
    
    /*
      lst中含有若干个点的坐标
      返回其中距离最小的点的距离
    */
    public static double getMinDistance(List<MyPoint> lst)
    {
        if(lst==null || lst.size()<2) return Double.MAX_VALUE;
        
        double r = Double.MAX_VALUE;
        MyPoint p0 = lst.remove(0);
        for(int i=0; i<lst.size(); i++)
        {
            MyPoint p = lst.get(i);
            double d = MyPoint.distance(p0,p);
            if(d<r) _________;
        }
        
        double d2 = __________________;
        return d2 < r ? d2 : r;
    }
}


r = d
getMinDistance(lst)

7 扑克牌排列

下面代码模拟了一套扑克牌(初始排序A~K,共13张)的操作过程。

操作过程是:
手里拿着这套扑克牌,从前面拿一张放在后面,再从前面拿一张放桌子上,再从前面拿一张放在后面,…
如此循环操作,直到剩下最后一张牌也放在桌子上。

下面代码的目的就是为了求出最后桌上的牌的顺序。

初始的排列如果是A,2,3…K,则最后桌上的顺序为:
[2, 4, 6, 8, 10, Q, A, 5, 9, K, 7, 3, J]

import java.util.*;
public class A23
{
    public static List moveCard(List src)
    {
        if(src==null) return null;
        
        List dst = new Vector();
        for(;;)
        {
            if(__________________) break;  // 填空
            src.add(src.remove(0));
            dst.add(__________________);  // 填空
        }
        
        return dst;
    }
    
    public static void main(String[] args)
    {
        List a = new Vector();
        a.addAll(Arrays.asList("A","2","3","4","5","6","7","8","9","10","J","Q","K"));
        System.out.println(moveCard(a));
    }
}


请分析代码逻辑,并推测划线处的代码。

答案写在 “解答.txt” 文件中

注意:只写划线处应该填的内容,划线前后的内容不要抄写。

src.size() == 0
src.remove(0)

8 三进制转十进制

不同进制的数值间的转换是软件开发中很可能

会遇到的常规问题。下面的代码演示了如何把键盘输入的3

进制数字转换为十进制。试完善之。

    BufferedReader br = new BufferedReader

(new InputStreamReader(System.in));
    String s = br.readLine();
    int n = 0;
    for(int i=0; i<s.length(); i++)
    {
        char c = s.charAt(i);
        if(c<'0' || c > '2') throw new 

RuntimeException("Format error");
        n = ______________________;
    }
    System.out.println(n);


n = n * 3 + (c - '0')

9 识别复制串

代码的目标:判断一个串是否为某个基本串的简单复制构成的。

例如:
abcabcabc,它由“abc”复制3次构成,则程序输出:abc
aa 由“a”复制两次构成,则程序输出:a
axa 不是简单复制构成,则不输出任何信息
aaxx 也不是仅仅由简单复制构成,不输出信息。

请阅读下面的代码,填写缺失的部分(下划线部分)。

注意:请把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。
直接写在题面中不能得分。

public class DaSai
{
    public static void findRepeat(String x)
    {
        for(int i=1; i<=x.length()/2; i++)
        {
            String base = x.substring(0,i);
            int p = i;
            for(;;)
            {
                if(p+i>x.length()) break;
                if(x.substring(p,p+i).equals(base)==false) break;
                ________________;   // 填空1
            }
            
            if(______________)  // 填空2
            {
                System.out.println(base);
                break;
            }
        }
    }
    
    public static void main(String[] args)
    {
        findRepeat("IhaveagoodideaIhaveagoodideaIhaveagoodidea");
    }
}

p = p + i
p == x.length()

10 蔬菜价格计算

计算蔬菜总价

为了丰富群众菜篮子,平抑菜价,相关部分组织了蔬菜的调运。今某箱中有多个品种的蔬菜。蔬菜的单价(元/公斤)存放在price数组中,蔬菜的重量(公斤)存放在weight数组中。下面的代码计算该箱蔬菜的总价,试完善之。

把填空的答案(仅填空处的答案,不包括题面)存入考生文件夹下对应题号的“解答.txt”中即可。

    public static double getTotal(double[] price, double[] weight)
    {
        double x = 0;
        for(int i=0; i<price.length; i++)
        {
            ____________________;
        }
        return x;
    }


x += price[i] * weight[i]
原文地址:https://www.cnblogs.com/a1439775520/p/13078288.html