算法笔记_126:算法集训之编程大题集二(Java)

 目录

1 连续数的公倍数

2 漏掉的账目明细

3 罗马数字转十进制

4 逻辑推断

5 平面4点最小距离

6 取球博弈

7 人民币金额大写

8 人员排日程

9 三角螺旋阵

10 手机尾号评分

 

 


1 连续数的公倍数

 

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

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

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


请编写程序,实现对用户输入的 n (n<100)求出1~n的最小公倍数。

例如:
用户输入:
6
程序输出:
60

用户输入:
10
程序输出:
2520

 

package com.liu.ex1;

import java.math.BigInteger;
import java.util.Scanner;


public class Main {
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        BigInteger result = new BigInteger("1");
        for(int i = 1;i <= n;i++) {
            BigInteger gcd = result.gcd(new BigInteger(""+i));
            result = result.multiply(new BigInteger(""+i));
            result = result.divide(gcd);
        }
        System.out.println(result);
    }
}

 

 

 

2 漏掉的账目明细

 

某财务部门结账时发现总金额不对头。很可能是从明细上漏掉了某1笔或几笔。如果已知明细账目清单,能通过编程找到漏掉的是哪1笔或几笔吗?
如果有多种可能,则输出所有可能的情况。
我们规定:用户输入的第一行是:有错的总金额。
接下来是一个整数n,表示下面将要输入的明细账目的条数。
再接下来是n行整数,分别表示每笔账目的金额。
要求程序输出:所有可能漏掉的金额组合。每个情况1行。金额按照从小到大排列,中间用空格分开。
比如:

用户输入:
6
5
3
2
4
3
1
表明:有错的总金额是6;明细共有5笔。
此时,程序应该输出:
1 3 3
1 2 4
3 4

为了方便,不妨假设所有的金额都是整数;每笔金额不超过1000,金额的明细条数不超过100。

 

package com.liu.ex2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    public static ArrayList<Integer> list = new ArrayList<Integer>();
    public static ArrayList<String> result = new ArrayList<String>();
    public static int[] value;    //表示明细账目清单
    public static int sum = 0;    //表示正常总金额-出错总金额值
    
    public int getSum() {
        int result = 0;
        for(int i = 0;i < list.size();i++)
            result += list.get(i);
        return result;
    }
    
    public void dfs(int step) {
        while(step < value.length) {
            list.add(value[step]);
            if(getSum() == sum) {
                ArrayList<Integer> tempList = new ArrayList<Integer>();
                for(int i = 0;i < list.size();i++)
                    tempList.add(list.get(i));
                Collections.sort(tempList);
                StringBuilder s = new StringBuilder("");
                for(int i = 0;i < tempList.size();i++)
                s.append(tempList.get(i)+" ");
                if(!result.contains(s.toString())) {
                    result.add(s.toString());
                    System.out.println(s);
                }
            }
            step++;
            dfs(step);
            list.remove(list.size() - 1);
        }
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int w = in.nextInt();
        int n = in.nextInt();
        value = new int[n];
        for(int i = 0;i < n;i++) {
            int a = in.nextInt();
            value[i] = a;
            sum = sum + a;
        }
        sum = sum - w;
        test.dfs(0);
    }
}

 

 

 

3 罗马数字转十进制

 

    古罗马帝国开创了辉煌的人类文明,但他们的数字表示法的确有些繁琐,尤其在表示大数的时候,现在看起来简直不能忍受,所以在现代很少使用了。之所以这样,不是因为发明表示法的人的智力的问题,而是因为一个宗教的原因,当时的宗教禁止在数字中出现0的概念!

    罗马数字的表示主要依赖以下几个基本符号:

    I  1
    V  5
    X  10
    L  50
    C  100
    D  500
    M  1000

    这里,我们只介绍一下1000以内的数字的表示法。

    单个符号重复多少次,就表示多少倍。最多重复3次。比如:CCC表示300  XX表示20,但150并不用LLL表示,这个规则仅适用于I X C M。

    如果相邻级别的大单位在右,小单位在左,表示大单位中扣除小单位。比如:IX表示9  IV表示4  XL表示40 更多的示例参见下表,你找到规律了吗?


I,1 
II,2
III,3
IV,4
V,5
VI,6
VII,7
VIII,8
IX,9 

X,10
XI,11
XII,12
XIII,13
XIV,14
XV,15
XVI,16
XVII,17
XVIII,18
XIX,19
XX,20
XXI,21
XXII,22
XXIX,29
XXX,30
XXXIV,34
XXXV,35
XXXIX,39
XL,40
L,50
LI,51
LV,55
LX,60
LXV,65
LXXX,80
XC,90
XCIII,93
XCV,95
XCVIII,98
XCIX,99

C,100
CC,200
CCC,300
CD,400
D,500
DC,600
DCC,700
DCCC,800
CM,900
CMXCIX,999


    本题目的要求是:请编写程序,由用户输入若干个罗马数字串,程序输出对应的十进制表示。

    输入格式是:第一行是整数n,表示接下来有n个罗马数字(n<100)。以后每行一个罗马数字。罗马数字大小不超过999。

    要求程序输出n行,就是罗马数字对应的十进制数据。

    例如,用户输入:
3
LXXX
XCIII
DCCII

    则程序应该输出:
80
93
702



    注意:

    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
    
    在评卷时使用的输入数据与试卷中给出的实例数据可能是不同的。

 

package com.liu.ex3;

import java.util.Scanner;

public class Main {
    
    public int getValue(char s) {
        int result = 0;
        if(s == 'I')
            result = 1;
        else if(s == 'V')
            result = 5;
        else if(s == 'X')
            result = 10;
        else if(s == 'L')
            result = 50;
        else if(s == 'C')
            result = 100;
        else if(s == 'D')
            result = 500;
        else if(s == 'M')
            result = 1000;
        return result;
    }
    
    public void getResult(String[] A) {
        int len = A.length;
        int[] result = new int[len];
        for(int i = 0;i < len;i++) {
            for(int j = 0;j < A[i].length();j++) {
                if(j == A[i].length() - 1) {
                    result[i] = result[i] + getValue(A[i].charAt(j));
                } else {
                    int temp1 = getValue(A[i].charAt(j));
                    int temp2 = getValue(A[i].charAt(j + 1));
                    if(temp2 > temp1)
                        result[i] = result[i] - temp1;
                    else 
                        result[i] = result[i] + temp1;
                }
            }
        }
        //打印结果
        for(int i = 0;i < len;i++)
            System.out.println(result[i]);
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        String[] A = new String[n];
        for(int i = 0;i < n;i++)
            A[i] = in.nextLine();
        test.getResult(A);
    }
}

 

 

 

4 逻辑推断

 

A、B、C、D、E、F、G、H、I、J 共10名学生有可能参加本次计算机竞赛,也可能不参加。因为某种原因,他们是否参赛受到下列条件的约束:

   1. 如果A参加,B也参加;
   2. 如果C不参加,D也不参加;
   3. A和C中只能有一个人参加;
   4. B和D中有且仅有一个人参加;
   5. D、E、F、G、H 中至少有2人参加;
   6. C和G或者都参加,或者都不参加;
   7. C、E、G、I中至多只能2人参加   
   8. 如果E参加,那么F和G也都参加。
   9. 如果F参加,G、H就不能参加
   10. 如果I、J都不参加,H必须参加

请编程根据这些条件判断这10名同学中参赛者名单。如果有多种可能,则输出所有的可能情况。每种情况占一行。参赛同学按字母升序排列,用空格分隔。

比如:
C D G J
就是一种可能的情况。

多种情况的前后顺序不重要

 

package com.liu.ex4;

import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static ArrayList<Integer> list = new ArrayList<Integer>();
    
    public boolean judge1() {
        boolean judge = true;
        if(list.contains(0)) {
            if(!list.contains(1))
                judge = false;
        }
        return judge;
    }
    
    public boolean judge2() {
        boolean judge = true;
        if(!list.contains(2)) {
            if(list.contains(3))
                judge = false;
        }
        return judge;
    }
    
    public boolean judge3() {
        boolean judge = true;
        if(list.contains(0)) {
            if(list.contains(2))
                judge = false;
        }
        return judge;
    }
    
    public boolean judge4() {
        boolean judge = false;
        if(list.contains(1) && !list.contains(3))
            judge = true;
        else if(!list.contains(1) && list.contains(3))
            judge = true;
        return judge;
    }
    
    public boolean judge5() {
        boolean judge = false;
        int count = 0;
        for(int i = 3;i <= 7;i++) {
            if(list.contains(i))
                count++;
        }
        if(count >= 2)
            judge = true;
        return judge;
    }
    
    public boolean judge6() {
        boolean judge = false;
        if(list.contains(2) && list.contains(6))
            judge = true;
        else if(!list.contains(2) && !list.contains(6))
            judge = true;
        return judge;
    }
    
    public boolean judge7() {
        boolean judge = false;
        int count = 0;
        if(list.contains(2))
            count++;
        if(list.contains(4))
            count++;
        if(list.contains(6))
            count++;
        if(list.contains(8))
            count++;
        if(count <= 2)
            judge = true;
        return judge;
    }
    
    public boolean judge8() {
        boolean judge = true;
        if(list.contains(4)) {
            if(list.contains(5) == false || list.contains(6) == false)
                judge = false;
        }
        return judge;
    }
    
    public boolean judge9() {
        boolean judge = true;
        if(list.contains(5)) {
            if(list.contains(6) || list.contains(7))
                judge = false;
        }
        return judge;
    }
    
    public boolean judge10() {
        boolean judge = true;
        if(!list.contains(8) && !list.contains(9)) {
            if(!list.contains(7))
                judge = false;
        }
        return judge;
    }
    
    public boolean check() {
        if(judge1() && judge2() && judge3() &&  judge4() && judge5()) {
            if(judge6() && judge7() && judge8() && judge9() && judge10())
                return true;
        }
        return false;
    }
    
    public void dfs(int step) {
        while(step < 10) {
            list.add(step);
            if(check()) {
                ArrayList<Integer> tempList = new ArrayList<Integer>();
                for(int i = 0;i < list.size();i++)
                    tempList.add(list.get(i));
                Collections.sort(tempList);
                for(int i = 0;i < tempList.size();i++) {
                    char temp = (char) ('A' + tempList.get(i));
                    System.out.print(temp+" ");
                }
                System.out.println();
            }
            step++;
            dfs(step);
            list.remove(list.size() - 1);
        }
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        test.dfs(0);
    }
}

 

 

 

5 平面4点最小距离

 

已知平面上若干个点的坐标。

需要求出在所有的组合中,4个点间平均距离的最小值(四舍五入,保留2位小数)。

比如有4个点:a,b,c,d, 则平均距离是指:ab, ac, ad, bc, bd, cd 这6个距离的平均值。

每个点的坐标表示为:横坐标,纵坐标

坐标的取值范围是:1~1000

例如,如果程序输入:
10,10
20,20
80,50
10,20
20,10

则程序应该输出:
11.38

 

package com.liu.ex5;

import java.util.ArrayList;
import java.util.Scanner;

public class Main1 {
    public static ArrayList<point> list = new ArrayList<point>();
    public static double minDistance = Double.MAX_VALUE;
    
    static class point {
        public double x;
        public double y;
        point(double x, double y) {
            this.x = x;
            this.y = y;
        }
    }
    
    public double getDistance(point a, point b) {
        double result = Math.sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
        return result;
    }
    
    public void getResult() {
        double[][] distance = new double[list.size()][list.size()];  //获取所有点之间的距离
        for(int i = 0;i < list.size();i++) {
            point a = list.get(i);
            for(int j = i + 1;j < list.size();j++) {
                point b = list.get(j);
                distance[i][j] = getDistance(a, b);
                distance[j][i] = distance[i][j];
            }
        }
        
        for(int a = 0;a < list.size();a++) {
            for(int b = a + 1;b < list.size();b++) {
                double temp1 = distance[a][b];
                if(temp1 > minDistance)
                    continue;
                for(int c = b + 1;c < list.size();c++) {
                    double temp2 = distance[a][b] + distance[a][c] + distance[b][c];
                    if(temp2 > minDistance)
                        continue;
                    for(int d = c + 1;d < list.size();d++) {
                        double temp = distance[a][b] + distance[a][c] + distance[a][d]+
                        distance[b][c] + distance[b][d] + distance[c][d];
                        if(temp < minDistance)
                            minDistance = temp;
                    }
                }
            }
        }
        minDistance = minDistance / 6;
        System.out.printf("%.2f", minDistance);
    }
    
    public static void main(String[] args) {
        Main1 test = new Main1();
        Scanner in = new Scanner(System.in);
        while(true) {
            String a = in.nextLine();
            if(a.equals(null) || a.equals(""))
                break;
            String[] temp = a.split(",");
            double x = Double.valueOf(temp[0]);
            double y = Double.valueOf(temp[1]);
            list.add(new point(x,y));
        }
        test.getResult();
    }
}

 

 

 

6 取球博弈

 

    
    今盒子里有n个小球,A、B两人轮流从盒中取球,每个人都可以看到另一个人取了多少个,也可以看到盒中还剩下多少个,并且两人都很聪明,不会做出错误的判断。

    我们约定:
    
    每个人从盒子中取出的球的数目必须是:1,3,7或者8个。

    轮到某一方取球时不能弃权!

    A先取球,然后双方交替取球,直到取完。

    被迫拿到最后一个球的一方为负方(输方)
    

    请编程确定出在双方都不判断失误的情况下,对于特定的初始球数,A是否能赢?

    程序运行时,从标准输入获得数据,其格式如下:

    先是一个整数n(n<100),表示接下来有n个整数。然后是n个整数,每个占一行(整数<10000),表示初始球数。

    程序则输出n行,表示A的输赢情况(输为0,赢为1)。

    例如,用户输入:
4
1
2
10
18

    则程序应该输出:
0
1
1
0



    注意:

    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
    
    在评卷时使用的输入数据与试卷中给出的实例数据可能是不同的。

 

package com.liu.ex6;

import java.util.Scanner;

public class Main {
    
    public static int[] value = new int[10001];
    
    public void getValue() {
        for(int i = 9;i < 10001;i++) {
            if(value[i - 1] == 0)
                value[i] = 1;
            
            if(value[i - 3] == 0)
                value[i] = 1;
            
            if(value[i - 7] == 0)
                value[i] = 1;
            
            if(value[i - 8] == 0)
                value[i] = 1;
        }
    }
    
    public void printResult(int[] A) {
        getValue();
        for(int i = 0;i < A.length;i++)
            System.out.println(value[A[i]]);
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        value[2] = 1;
        value[4] = 1;
        value[6] = 1;
        value[8] = 1;
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] A = new int[n];
        for(int i = 0;i < n;i++)
            A[i] = in.nextInt();
        test.printResult(A);
     }
}

 

 

 

7 人民币金额大写

 

在与财务相关的应用中,经常会用到人民币金额的大写,比如发票的打印程序。
本题的任务是:从键盘输入一个十亿以内的正整数(int类型),把它转换为人民币金额大写(不考虑用户输入错误的情况)。
比如,用户输入:35201,程序输出:叁万伍仟贰佰零壹
用户输入:30201,程序输出:叁万零贰佰零壹
用户输入:30001,程序输出:叁万零壹
用户输入:31000,程序输出:叁万壹仟
用户输入:120023201,程序输出:壹亿贰仟零贰万叁仟贰佰零壹
用户输入:120020001,程序输出:壹亿贰仟零贰万零壹
用户输入:100000001,程序输出:壹亿零壹
可以看到,在万后满千位,则不加零,否则要补零,但不要出现类似“零零”的情况。
在亿后满千万位,则不加零,否则要补零,但整个“万档”没有数字时,“万”字省去。

 

package com.liu.ex7;

import java.util.Scanner;


public class Main {
    public static String[] A = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};
    
    public String getPart(String temp) {
        int num = Integer.valueOf(temp);   //除去字符串前0
        temp = num + "";
        String result = "";
        int len = temp.length();
        for(int i = 0, j = len;i < len;i++, j--) {
            int number = temp.charAt(i) - '0';
            if(number == 0) {
                //0在个位
                if(i == len - 1)
                    break;
                //0在十位
                if(len == 3 && i == 1) {
                    int number1 = temp.charAt(i + 1) - '0';
                    if(number1 == 0)
                        break;
                    else
                        result = result + A[0];
                }
                //0在十位
                if(len == 4 && i == 2) {
                    int number1 = temp.charAt(i + 1) - '0';
                    if(number1 == 0)
                        break;
                    else
                        result = result + A[0];
                }
                //0在百位
                if(len == 4 && i == 1) {
                    int number1 = temp.charAt(i + 1) - '0';
                    int number2 = temp.charAt(i + 2) - '0';
                    if(number1 != 0)
                        result = result + A[0];
                    else if(number1 == 0 && number2 != 0) {
                        i = i + 1;
                        j = j - 1;
                        result = result + A[0];
                    } else if(number1 == 0 && number2 == 0) {
                        break;
                    }
                }
            }
            if(number != 0) {
                result = result + A[number];
                if(j == 4)
                    result = result + "仟";
                else if(j == 3)
                    result = result + "佰";
                else if(j == 2)
                    result = result + "拾";
            }
        }
        return result;
    }
    
    public void getResult(int n) {
        String result = "";
        String temp = "" + n;
        int len = temp.length();
        if(len >= 9) {
            String temp1 = temp.substring(len - 4, len);
            String temp2 = temp.substring(len - 8, len - 4);
            String temp3 = temp.substring(0, len - 8);
            result = result + getPart(temp3) + "亿";
            if(Integer.valueOf(temp2) < 1000 && Integer.valueOf(temp2) != 0)
                result = result + A[0];
            if(Integer.valueOf(temp2) == 0)
                result = result + A[0];
            else
                result = result + getPart(temp2) + "万";
            if(Integer.valueOf(temp1) != 0) {
                if(Integer.valueOf(temp2) != 0 && Integer.valueOf(temp1) < 1000)
                    result = result + A[0];
                result = result + getPart(temp1);
            }
        } else if(len >= 5 && len < 9) {
            String temp1 = temp.substring(len - 4, len);
            String temp2 = temp.substring(0, len - 4);
            result = result + getPart(temp2) + "万";
            if(Integer.valueOf(temp1) != 0) {
                if(Integer.valueOf(temp1) < 1000)
                    result = result + A[0];
                result = result + getPart(temp1);
            }
        } else if(len > 0 && len < 5) {
            result = result + getPart(temp);
        }
        System.out.println(result);
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        test.getResult(n);
    }
}

 

 

 

8 人员排日程

 

    某保密单位机要人员 A,B,C,D,E 每周需要工作5天,休息2天。

    上级要求每个人每周的工作日和休息日安排必须是固定的,不能在周间变更。

    此外,由于工作需要,还有如下要求:

    1. 所有人的连续工作日不能多于3天(注意:周日连到下周一也是连续)。

    2. 一周中,至少有3天所有人都是上班的。

    3. 任何一天,必须保证 A B C D 中至少有2人上班。

    4. B D E 在周日那天必须休息。

    5. A E 周三必须上班。

    6. A C 一周中必须至少有4天能见面(即同时上班)。

    你的任务是:编写程序,列出ABCDE所有可能的一周排班情况。工作日记为1,休息日记为0
    
    A B C D E 每人占用1行记录,从星期一开始。

【输入、输出格式要求】

    程序没有输入,要求输出所有可能的方案。

    每个方案是7x5的矩阵。只有1和0组成。        
    
    矩阵中的列表示星期几,从星期一开始。

    矩阵的行分别表示A,B,C,D,E的作息时间表。

    多个矩阵间用空行分隔开。

    例如,如下的矩阵就是一个合格的解。请编程输出所有解(多个解的前后顺序不重要)。

0110111
1101110
0110111
1101110
1110110


【注意】

    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
    
    在评卷时使用的输入数据与试卷中给出的实例数据可能是不同的。

 

package com.liu.ex8;

import java.util.ArrayList;


public class Main {
    public static int[] A = {1,1,1,1,1,1,1};
    public static ArrayList<Integer> list = new ArrayList<Integer>();
    public static ArrayList<String> planList = new ArrayList<String>();
    
    public boolean check() {
        int count1 = 0;
        for(int i = 0;i < 10;i++) {
            if(A[i % 7] == 1)
                count1++;
            else if(A[i % 7] == 0)
                count1 = 0;
            if(count1 >= 4)
                return false;
        }
        return true;
    }
    
    public void dfs(int step) {
        while(step < 7) {
            list.add(step);
            if(list.size() == 2) {
                A[list.get(0)] = 0;
                A[list.get(1)] = 0;
                if(check()) {
                    StringBuilder s = new StringBuilder("");
                    for(int i = 0;i < 7;i++)
                        s.append(A[i]);
                    planList.add(s.toString());
                }
                A[list.get(0)] = 1;
                A[list.get(1)] = 1;
            }
            step++;
            dfs(step);
            list.remove(list.size() - 1);
        }
    }
    
    public void getValue(int[][] value, int[] A) {
        for(int i = 0;i < A.length;i++) {
            String temp = planList.get(A[i]);
            for(int j = 0;j < temp.length();j++)
                value[i][j] = temp.charAt(j) - '0';
        }
    }
    
    public boolean judge(int[][] value) {
        //判断要求2
        int count = 0;
        for(int j = 0;j < value[0].length;j++) {
            int sum = 0;
            for(int i = 0;i < value.length;i++) {
                if(value[i][j] == 1)
                    sum = sum + 1;
            }
            if(sum == value.length)
                count++;
        }
        if(count < 3)
            return false;
        //判断要求3
        for(int j = 0;j < value[0].length;j++) {
            int sum = 0;
            for(int i = 0;i < value.length - 1;i++) {
                if(value[i][j] == 1)
                    sum = sum + 1;
            }
            if(sum < 2)
                return false;
        }
        //判断要求4
        if(value[1][6] != 0 || value[3][6] != 0 || value[4][6] != 0)
            return false;
        //判断要求5
        if(value[0][2] != 1 || value[4][2] != 1)
            return false;
        //判断要求6
        count = 0;
        for(int j = 0;j < value[0].length;j++) {
            if(value[0][j] == 1 && value[2][j] == 1)
                count++;
        }
        if(count < 4)
            return false;
        return true;
    }
    
    
    public void getResult() {
        int len = planList.size();
        int[][] value = new int[5][7];
        int[] A = new int[5];
        for(A[0] = 0;A[0] < len;A[0]++) {
            for(A[1] = 0;A[1] < len;A[1]++) {
                for(A[2] = 0;A[2] < len;A[2]++) {
                    for(A[3] = 0;A[3] < len;A[3]++) {
                        for(A[4] = 0;A[4] < len;A[4]++) {
                            getValue(value, A);
                            if(judge(value))
                                printResult(value);
                        }
                    }
                }
            }
        }
        return;
    }
    
    public void printResult(int[][] value) {
        for(int i = 0;i < value.length;i++) {
            for(int j = 0;j < value[0].length;j++)
                System.out.print(value[i][j]);
            System.out.println();
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        test.dfs(0); //获取满足题意要求1的一周工作日安排所有方案
        test.getResult();
    }
}

 

 

 

9 三角螺旋阵

 

方阵的主对角线之上称为“上三角”。
请你设计一个用于填充n阶方阵的上三角区域的程序。填充的规则是:使用1,2,3….的自然数列,从左上角开始,按照顺时针方向螺旋填充。
例如:当n=3时,输出:
   1   2   3
   6   4
   5
当n=4时,输出:
   1   2   3   4
   9  10   5
   8   6
   7
当n=5时,输出:
   1   2   3   4   5
  12  13  14   6 
  11  15   7
  10   8
   9

程序运行时,从标准输入获得整数n(3~20)
程序输出:方阵的上三角部分。
要求格式:每个数据宽度为4,右对齐。

 

package com.liu.ex9;

import java.util.Scanner;


public class Main {
    
    public void initArray(int[][] arrayA) {
        for(int i = 0;i < arrayA.length;i++)
            arrayA[i] = new int[arrayA.length - i];
    }
    
    public void dfs(int[][] arrayA, int row, int num) {
        if(row >= arrayA.length / 2)
            return;
        //填充上方第一行
        for(int i = row;i < arrayA[row].length - row;i++)
            arrayA[row][i] = num++;
        //填充后续每一行右边最后一个元素
        for(int i = row + 1;i < arrayA[row].length - row;i++)
            arrayA[i][arrayA[i].length - 1 - row] = num++;
        //填充后续每一行开始左边第一个元素
        for(int i = arrayA[row].length - 2 - row;i > row;i--)
            arrayA[i][row] = num++;
        dfs(arrayA, row + 1, num);
    }
    
    public void printResult(int[][] arrayA) {
        for(int i = 0;i < arrayA.length;i++) {
            for(int j = 0;j < arrayA[i].length;j++)
                System.out.printf("%4d",arrayA[i][j]);
            System.out.println();
        }
    }
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] arrayA = new int[n][];
        test.initArray(arrayA);
        test.dfs(arrayA, 0, 1);
        test.printResult(arrayA);
    }
}

 

 

 

10 手机尾号评分

 

    30年的改革开放,给中国带来了翻天覆地的变化。2011全年中国手机产量约为11.72亿部。手机已经成为百姓的基本日用品!

    给手机选个好听又好记的号码可能是许多人的心愿。但号源有限,只能辅以有偿选号的方法了。

    这个程序的目的就是:根据给定的手机尾号(4位),按照一定的规则来打分。其规则如下:

    1. 如果出现连号,不管升序还是降序,都加5分。例如:5678,4321都满足加分标准。

    2. 前三个数字相同,或后三个数字相同,都加3分。例如:4888,6665,7777都满足加分的标准。注意:7777因为满足这条标准两次,所以这条规则给它加了6分。

    3. 符合AABB或者ABAB模式的加1分。例如:2255,3939,7777都符合这个模式,所以都被加分。注意:7777因为满足这条标准两次,所以这条标准给它加了2分。

    4. 含有:6,8,9中任何一个数字,每出现一次加1分。例如4326,6875,9918都符合加分标准。其中,6875被加2分;9918被加3分。

    尾号最终得分就是每条标准的加分总和!

    要求程序从标准输入接收数据,在标准输出上输出结果。

    输入格式为:第一行是一个整数n(<100),表示下边有多少输入行,接下来是n行4位一组的数据,就是等待计算加分的手机尾号。
    输出格式为:n行整数。

    例如,输入:
14
3045
0211
2345
6543
7777
8888
7878
7788
6688
2424
2244
9918
6789
8866
    则输出:
0
0
5
6
8
12
3
3
5
1
1
3
8
5

    注意:

    请仔细调试!您的程序只有能运行出正确结果的时候才有机会得分!
    
    在评卷时使用的输入数据与试卷中给出的实例数据可能是不同的。

 

package com.liu.ex10;

import java.util.ArrayList;
import java.util.Scanner;


public class Main {
    
    public int getScore(String A) {
        int count = 0;
        ArrayList<Integer> list = new ArrayList<Integer>();
        for(int i = 0;i < A.length();i++) {
            int a = A.charAt(i) - '0';
            list.add(a);
        }
        //规则1
        int i = 1;
        for(;i < 4;i++) {
            if(list.get(i) == list.get(i - 1) + 1)
                continue;
            else
                break;
        }
        if(i == 4)
            count = count + 5;
        for(i = 1;i < 4;i++) {
            if(list.get(i) == list.get(i - 1) - 1)
                continue;
            else
                break;
        }
        if(i == 4)
            count = count + 5;
        //规则2
        int a1 = list.get(0), a2 = list.get(1), a3 = list.get(2), a4 = list.get(3);
        if(a1 == a2 && a1 == a3)
            count = count + 3;
        if(a2 == a3 && a2 == a4)
            count = count + 3;
        //规则3
        if(a1 == a2 && a3 == a4)
            count = count + 1;
        if(a1 == a3 && a2 == a4)
            count = count + 1;
        //规则4
        for(i = 0;i < 4;i++) {
            if(list.get(i) == 6 || list.get(i) == 8 || list.get(i) == 9)
                count = count + 1;
        }
        return count;
    }
    
    public void printResult(String[] arrayA) {
        int[] result = new int[arrayA.length];
        for(int i = 0;i < arrayA.length;i++) {
            int temp = getScore(arrayA[i]);
            result[i] = temp;
        }
        for(int i = 0;i < result.length;i++)
            System.out.println(result[i]);
        return;
    }
    
    public static void main(String[] args) {
        Main test = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        in.nextLine();
        String[] arrayA = new String[n];
        for(int i = 0;i < n;i++)
            arrayA[i] = in.nextLine();
        test.printResult(arrayA);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值