算法竞赛入门经典(第2版)第二章习题(Java)

算法竞赛入门经典(第2版)第二章习题(Java)

  • 要注意题目中给出的变量值得范围,选择合适的变量类型。
  • 要计算只包含加法减法的整数表达式除以正整数n的余数,可以在每步计算之后对n取余,结果不变。(防止程序进行中时数据过大溢出)
  • 取整数的某个位置的数先除以该位再模于10即可。如:取1234的百位的数1234/100%10.

引用的包代码中并没有写,在eclipse下按Shift+Ctrl+o即可导入包

习题 2-1 水仙花数(daffodil)
 public static void daffodil() {
        for (int i = 100; i < 1000; i++) {
            int b = i / 100 % 10;
            int s = i / 10 % 10;
            int g = i / 1 % 10;
            if (i == (b * b * b + s * s * s + g * g * g)) {
                System.out.println(i);
            }
        }
    }
习题 2-2 韩信点兵(hanxin)
 public static void hanxin() {
        Scanner in = new Scanner(System.in);
        //用来存放结果,当输入结束,输出结果
        String str = "";
        //记录是第几个输入的结果
        int n = 1;
        w: while (true) {
            String p = in.nextLine();
            //如果输入为空行,输入结束
            if (p.equals(""))
                break;
            String[] ls = p.split(" ");
            int a = Integer.parseInt(ls[0]);
            int b = Integer.parseInt(ls[1]);
            int c = Integer.parseInt(ls[2]);
            for (int j = 10; j <= 100; j++) {
                //判断符合条件将结果添加到结果字符串中,并跳转到外层循环,重新输入,否则会将“No answer”添加到结果字符串。
                if (j % 3 == a && j % 5 == b && j % 7 == c) {   
                    str = str + "Case " + (n++) + ": " + j + "\n";
                    continue w;
                }
            }
            str = str + "Case " + (n++) + ": No answer\n";
        }
        System.out.println(str);
    }
习题 2-3 倒三角形(triangle)
public static void triangle() {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        //最外层循环负责换行
        for (int i = 0; i < n; i++) {
            //打印每行前面的空格
            for (int k = 0; k < i; k++) {
                System.out.print(" ");
            }
            //打印“#”
            for (int j = 0; j < (2 * (n - i) - 1); j++) {
                System.out.print("#");
            }
            System.out.println();
        }
    }
习题 2-4 子序列的和(subsequence)
public static void subsequence() {
        Scanner in = new Scanner(System.in);
        //规定数据格式
        DecimalFormat df = new DecimalFormat("0.#####");
        //记录是第几次数据的结果
        int ci = 1;
        //存放结果
        String jg = "";
        while (true) {
            String str = in.nextLine();
            if (str.equals("0 0"))
                break;
            int n = Integer.parseInt(str.split(" ")[0]);
            int m = Integer.parseInt(str.split(" ")[1]);
            double sum = 0;
            for (int i = n; i <= m; i++) {
                sum = sum + (double) 1 / i / i;
            }
            jg = jg + "Case " + ci++ + ": " + df.format(sum) + "\n";
        }
        System.out.println(jg);
    }
习题 2-5 分数化小数(decimal)
public static void decimal() {
        Scanner in = new Scanner(System.in);
        DecimalFormat df;
        String jg = "";
        //记录第几次输入的结果
        int ci = 1;
        while (true) {
            String str = in.nextLine();
            if (str.equals("0 0 0"))
                break;
            int a = Integer.parseInt(str.split(" ")[0]);
            int b = Integer.parseInt(str.split(" ")[1]);
            int c = Integer.parseInt(str.split(" ")[2]);
            //根据c得到结果数据格式。
            String wei = "0.";
            for (int i = 0; i < c; i++) {
                wei += "0";
            }
            df = new DecimalFormat(wei);
            double chu = (double) a / b;
            jg = jg + "Case " + ci++ + ": " + df.format(chu) + "\n";
        }
        System.out.println(jg);
    }
习题 2-6 排列(permutation)
public static void permutation() {
        //因为abc:def:ghi=1:2:3,所以只要暴力破解列出abc即可
        //因为ghi<1000,所以a的范围为1-3
        for (int i = 1; i <= 1000 / 3 / 100; i++) {
            for (int j = 1; j <= 9; j++) {
                if (j == i)
                    continue;
                w: for (int k = 1; k <= 9; k++) {
                    if (j == k || j == i)
                        continue;
                    //如果i、j、k不同得到abc,def,ghi
                    int abc = i * 100 + j * 10 + k;
                    int def = abc * 2;
                    int ghi = abc * 3;
                    //将abc,def,ghi每位放入数组
                    int[] sz = { i, j, k, def / 100 % 10, def / 10 % 10, def % 10, ghi / 100 % 10, ghi / 10 % 10,
                            ghi % 10 };
                    //遍历数组中的数,是否与其他数不同。如果相同调到w:层循环,均不同则输出结果。
                    for (int n = 0; n < 9; n++) {
                        for (int m = n + 1; m < 9; m++) {
                            if (sz[n] == sz[m])
                                continue w;
                        }
                    }
                    System.out.println(abc + " " + def + " " + ghi);

                }
            }
        }
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值