华为机试题(Java)

2017年

1.编写一个reverseadd函数,实现两个数倒置后再求和的功能,比如输入123,456就是求321+654,输出975。注意:输入100,200输出3,(自动去除开头的0),超过范围输出-1
思路:接收两个数字,如果为负数,先打印负号,再转为正数进行处理。倒序就利用取余。如果余数为零,则跳过该次循环,不为零,则存到字符串。注意:调整语句要放到continue语句前,否则会死循环。

package com.huawei.test2017.test1;

import java.util.Scanner;

public class test1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入两个数字");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();

        System.out.println( reverseadd(num1,num2) );
    }

    public static int reverseadd(int num1,int num2) {
        if ((num1<1)||(num1>700000)||(num2<1)||(num2>700000)) {
            return -1;
        }

        String res1 = "";
        String res2 = "";

        while ( num1 != 0 ){
            int r1 = num1 % 10;
            num1 = num1 / 10;
            if ( r1==0 ) {
                continue;
            }else {
                res1 += r1;
            }
        }

        while ( num2 != 0 ){
            int r2 = num2 % 10;
            num2 = num2 / 10;
            if ( r2==0 ) {
                continue;
            }else {
                res2 += r2;
            }
        }

        int reserveresult1 = Integer.parseInt(res1);
        int reserveresult2 = Integer.parseInt(res2);

        int sum = reserveresult1 + reserveresult2;

        return sum;
    }
}

2.一个立方体骰子平放在桌面上,有一面正对着读者,称为前面。我们将六个面分别称为左、右、前、后、上、下,每个面对应的数字分别为1、2、3、4、5、6。我们定义以下操作:向前滚动称为F,向后滚动称为B,向左滚动称为L,向右滚动称为R,上下面不变顺时针旋转称为C,上下面不变逆时针旋转称为U。在原始状态情况下,输入对应的操作码,输出最终的骰子状态(按照每个面的顺序左右前后上下)。

package com.huawei.test2017.test1;

import java.util.Scanner;

public class test2 {

    private static int state[]= {1,2,3,4,5,6};          //定义初始状态数组

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一串字符串,L代表向左翻转,R代表向右翻转,F代表向前翻转,B代表向后翻转,A代表逆时针翻转,C代表顺时针翻转");
        while(sc.hasNext()) {                           //如果有下一个事件则返回true
            String s = sc.next();                       //定义字符串接收操作码
            char c[] = s.toCharArray();                 //将字符串转换成一个新的字符数组
            move(c);
            for(int i=0; i<6 ;i++) {
                System.out.print( state[i] );
            }
        }
    }

    public static void left() {             //向左旋转
        int temp1 = state[0];
        int temp2 = state[1];
        state[0] = state[4];
        state[1] = state[5];
        state[5] = temp1;
        state[4] = temp2;
    }

    public static void front() {                //向前旋转
        int temp1 = state[2];
        int temp2 = state[3];
        state[2] = state[4];
        state[3] = state[5];
        state[5] = temp1;
        state[4] = temp2;
    }

    public static void clockwise() {                //顺时针旋转
        int temp1 = state[0];
        int temp2 = state[1];
        state[0] = state[3];
        state[1] = state[2];
        state[2] = temp1;
        state[3] = temp2;
    }

    public static void move(char s[]) {
        for(int i=0; iif (s[i]=='L') {
                left();
            }
            if (s[i]=='R') {                        //向右旋转通过向左旋转三次实现
                left();
                left();
                left();
            }
            if (s[i]=='F') {
                front();
            }
            if (s[i]=='B') {
                front();
                front();
                front();
            }
            if (s[i]=='C') {
                clockwise();
            }
            if (s[i]=='A') {
                clockwise();
                clockwise();
                clockwise();
            }
        }
    }
}

3.当出差遇上大雾,给定距离矩阵,大雾城市(既不能到达,也不能离开),目的地,输出最短路径和路径,迪杰斯特拉结合path数组记录路径,path在迪杰斯特拉更新dis距离数组时更新,大雾城市就是将距离矩阵变为1000,无法到达输出-1。

2016年

1.题目描述:老师想知道从某某同学当中,分数最高的是多少,现在请你编程模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
输入描述:输入包括多组测试数据。每组输入第一行是两个正整数N和M(0 < N<= 30000,0 < M < 5000),分别代表学生的数目和操作的数目。学生ID编号从1编到N。第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。接下来又M行,每一行有一个字符C(只取‘Q’或‘U’),和两个正整数A,B,当C为’Q’的时候, 表示这是一条询问操作,他询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。当C为‘U’的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
输出描述:对于每一次询问操作,在一行里面输出最高成绩。
输入例子:
5 7
1 2 3 4 5
Q 1 5

package com.huawei.test2016;

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

public class test1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        do {
            System.out.println("请输入学生数目和操作数目");
            int data[] = new int[2];
            data[0] = sc.nextInt();
            data[1] = sc.nextInt();
            System.out.println("请输入学生成绩");
            ArrayList array = new ArrayList();
            for(int i=0; i0] ;i++) {
                array.add(sc.nextInt());
            }

            int round = 0;
            char a;
            int b,c;
            while(round1]) {
                System.out.println("请输入操作指令");
                a = sc.next().charAt(0);
                b = sc.nextInt();
                c = sc.nextInt();

                if (a=='Q') {
                    int start,end;
                    if (b1;
                        end = c-1;
                    }else {
                        start = c-1;
                        end = b-1;
                    }

                    int max = array.get(start);
                    for(int index=start+1; index<=end ;index++) {
                        if (max<array.get(index)) {
                            max = array.get(index);
                        }
                    }
                    System.out.println(max);
                    max = 0;
                }

                if (a=='U') {
                    int index1 = b-1;
                    int newValue = c;
                    array.set(index1, newValue);
                }
                round++;
            }
        } while (sc.hasNext());
        sc.close();
    }
}

2018年

1.实现两个26进制数相加,a代表0,z代表25

package com.huawei.test2018;
/*
 * 实现两个26进制数相加,a代表0,z代表25
 * 例:bc+z=cb
 */
import java.util.Scanner;

public class test1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入两个26进制数");
        String res1 = sc.next();
        String res2 = sc.next();
        int num1=0;
        int sum1=0;
        char ch1;
        for(int i=0;iswitch(ch1){
                    case 'a' :
                        num1 = 0;
                        break;
                    case 'b' :
                        num1 = 1;
                        break;
                    case 'c' :
                        num1 = 2;
                        break;
                    case 'd' :
                        num1 = 3;
                        break;
                    case 'e' :
                        num1 = 4;
                        break;
                    case 'f' :
                        num1 = 5;
                        break;
                    case 'g' :
                        num1 = 6;
                        break;
                    case 'h' :
                        num1 = 7;
                        break;
                    case 'i' :
                        num1 = 8;
                        break;
                    case 'j' :
                        num1 = 9;
                        break;
                    case 'k' :
                        num1 = 10;
                        break;
                    case 'l' :
                        num1 = 11;
                        break;
                    case 'm' :
                        num1 = 12;
                        break;
                    case 'n' :
                        num1 = 13;
                        break;
                    case 'o' :
                        num1 = 14;
                        break;
                    case 'p' :
                        num1 = 15;
                        break;
                    case 'q' :
                        num1 = 16;
                        break;
                    case 'r' :
                        num1 = 17;
                        break;
                    case 's' :
                        num1 = 18;
                        break;
                    case 't' :
                        num1 = 19;
                        break;
                    case 'u' :
                        num1 = 20;
                        break;
                    case 'v' :
                        num1 = 21;
                        break;
                    case 'w' :
                        num1 = 22;
                        break;
                    case 'x' :
                        num1 = 23;
                        break;
                    case 'y' :
                        num1 = 24;
                        break;
                    case 'z' :
                        num1 = 25;
                        break;
                }
                int chengfang=0;
                chengfang = (int)Math.pow(26, res1.length()-1-i);
                num1 = num1*chengfang;
                sum1 += num1;
        }

        int num2=0;
        int sum2=0;
        char ch2;
        for(int a=0;aswitch(ch2){
                    case 'a' :
                        num2 = 0;
                        break;
                    case 'b' :
                        num2 = 1;
                        break;
                    case 'c' :
                        num2 = 2;
                        break;
                    case 'd' :
                        num2 = 3;
                        break;
                    case 'e' :
                        num2 = 4;
                        break;
                    case 'f' :
                        num2 = 5;
                        break;
                    case 'g' :
                        num2 = 6;
                        break;
                    case 'h' :
                        num2 = 7;
                        break;
                    case 'i' :
                        num2 = 8;
                        break;
                    case 'j' :
                        num2 = 9;
                        break;
                    case 'k' :
                        num2 = 10;
                        break;
                    case 'l' :
                        num2 = 11;
                        break;
                    case 'm' :
                        num2 = 12;
                        break;
                    case 'n' :
                        num2 = 13;
                        break;
                    case 'o' :
                        num2 = 14;
                        break;
                    case 'p' :
                        num2 = 15;
                        break;
                    case 'q' :
                        num2 = 16;
                        break;
                    case 'r' :
                        num2 = 17;
                        break;
                    case 's' :
                        num2 = 18;
                        break;
                    case 't' :
                        num2 = 19;
                        break;
                    case 'u' :
                        num2 = 20;
                        break;
                    case 'v' :
                        num2 = 21;
                        break;
                    case 'w' :
                        num2 = 22;
                        break;
                    case 'x' :
                        num2 = 23;
                        break;
                    case 'y' :
                        num2 = 24;
                        break;
                    case 'z' :
                        num2 = 25;
                        break;
                }
                int chengfang2=0;
                chengfang2 = (int)Math.pow(26, res2.length()-1-a);
                num2 = num2*chengfang2;
                sum2 += num2;
        }

        int sum = sum1 + sum2;
        int yushu = 0;
        int shang = sum;
        String res = "";
        while(shang!=0) {
            yushu = shang % 26;
            String yushu26 = "";
            switch(yushu){
                case 0 :
                    yushu26 = "a";
                    break;
                case 1 :
                    yushu26 = "b";
                    break;
                case 2 :
                    yushu26 = "c";
                    break;
                case 3 :
                    yushu26 = "d";
                    break;
                case 4 :
                    yushu26 = "e";
                    break;
                case 5 :
                    yushu26 = "f";
                    break;
                case 6 :
                    yushu26 = "g";
                    break;
                case 7 :
                    yushu26 = "h";
                    break;
                case 8 :
                    yushu26 = "i";
                    break;
                case 9 :
                    yushu26 = "j";
                    break;
                case 10 :
                    yushu26 = "k";
                    break;
                case 11 :
                    yushu26 = "l";
                    break;
                case 12 :
                    yushu26 = "m";
                    break;
                case 13 :
                    yushu26 = "n";
                    break;
                case 14 :
                    yushu26 = "o";
                    break;
                case 15 :
                    yushu26 = "p";
                    break;
                case 16 :
                    yushu26 = "q";
                    break;
                case 17 :
                    yushu26 = "r";
                    break;
                case 18 :
                    yushu26 = "s";
                    break;
                case 19 :
                    yushu26 = "t";
                    break;
                case 20 :
                    yushu26 = "u";
                    break;
                case 21 :
                    yushu26 = "v";
                    break;
                case 22 :
                    yushu26 = "w";
                    break;
                case 23 :
                    yushu26 = "x";
                    break;
                case 24 :
                    yushu26 = "y";
                    break;
                case 25 :
                    yushu26 = "z";
                    break;
            }
            res = yushu26 + res;
            shang = shang / 26;
        }
        System.out.println(res);
    }
}

你可能感兴趣的:(华为机试题(Java))