华为笔试题(7)

一、
编写一个程序,将输入字符串中的字符按如下规则排序。
规则 1 :英文字母从 A 到 Z 排列,不区分大小写。
如,输入: Type 输出: epTy
规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。
如,输入: BabA 输出: aABb
规则 3 :非英文字母的其它字符保持原来的位置。
如,输入: By?e 输出: Be?y
样例:
输入描述:
输出描述:
示例1
输入
A Famous Saying: Much Ado About Nothing (2012/8).
输出
A aaAAbc dFgghh: iimM nNn oooos Sttuuuy (2012/8).
*/

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            char[] chs = sc.nextLine().toCharArray();
            StringBuffer sb = new StringBuffer();
            for(int i = 0; i < 26; i++) {
                for(int j = 0; j < chs.length; j++) {
                    if(chs[j] == i + 'A' || chs[j] == i + 'a') {
                        sb.append(chs[j]);
                    }
                }
            }
            for(int i = 0; i < chs.length; i++) {
                if(!(chs[i] >= 'A' && chs[i] <= 'Z' || chs[i] >= 'a' && chs[i] <= 'z')) {
                    sb.insert(i, chs[i]);//insert后字符就在那个位置,原来的字符往后移
                }
            }
            System.out.println(sb.toString());
        }
        sc.close();
    }
}

二、
一个DNA序列由A/C/G/T四个字母的排列组合组成。
G和C的比例(定义为GC-Ratio)是序列中G和C两个字母的总的出现次数除以总的字母数目(也就是序列长度。
在基因工程中,这个比例非常重要。因为高的GC-Ratio可能是基因的起始点。
给定一个很长的DNA序列,以及要求的最小子序列长度,研究人员经常会需要在其中找出GC-Ratio最高的子序列。
输入描述:
输入一个string型基因序列,和int型子串的长度
输出描述:
找出GC比例最高的子串,如果有多个输出第一个的子串
示例1
输入
AAATCACGGAGAAACCAGGTCAGCTGTCTTTACCTCGC
19
输出
CACGGAGAAACCAGGTCAG

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //每次统计子串C和G的个数即可
        while(sc.hasNext()) {
            String str = sc.nextLine();
            int n = sc.nextInt();
            int len = str.length();
            int max = 0;
            int index = 0;
            for(int i = 0; i < len - n; i++) {
                int cnt = 0;
                for(int j = i; j < i + n; j++) {
                    if(str.charAt(j) == 'G' || str.charAt(j) == 'C') {
                        cnt++;
                    }
                }
                if(cnt > max) {
                    index = i;
                    max = cnt;
                }
            }
            System.out.println(str.substring(index, index + n));
        }
        sc.close();
    }
}

三、
问题描述:给出4个1-10的数字,通过加减乘除,得到数字为24就算胜利
输入:
4个1-10的数字。[数字允许重复,测试用例保证无异常数字]
输出:
true or false
输入描述:
输入4个int整数
输出描述:
返回能否得到24点,能输出true,不能输出false
示例1
输入
7 2 1 10
输出
true
*/

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //只有4个数,直接dfs
        while(sc.hasNext()) {
            int[] a = new int[4];
            for(int i = 0; i < 4; i++) {
                a[i] = sc.nextInt();
            }
            System.out.println(process(a,new boolean[4], 0));
        }
        sc.close();
    }
    private static boolean process(int a[], boolean[] vis,double res) {//注意是double,因为有除
        for(int i=0;i<a.length;i++){
            if(!vis[i]){
                vis[i]=true;
                if(process(a,vis,res+a[i])
                        || process(a,vis,res-a[i])
                        || process(a,vis,res*a[i])
                        || process(a,vis,res/a[i])){
                    return true;
                }
                vis[i]=false;
            }
        }
        if(res==24){
            return true;
        }else{
            return false;
        }
    }
}

四、
输入任意(用户,成绩)序列,可以获得成绩从高到低或从低到高的排列,
相同成绩都按先录入排列在前的规则处理。
0为从高到底排序,1为从低到高排序
输入描述:
输入多行,先输入要排序的人的个数,然后分别输入他们的名字和成绩,以一个空格隔开
输出描述:
按照指定方式输出名字和成绩,名字和成绩之间以一个空格隔开
示例1
输入
3
0
fang 90
yang 50
ning 70
输出
fang 90
ning 70
yang 50

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int n = sc.nextInt();;
            int flag = sc.nextInt();
            String[] name = new String[n];
            int[] score = new int[n];
            for(int i = 0; i < n; i++) {
                name[i] = sc.next();
                score[i] = sc.nextInt();
            }
            sort(name, score, n, flag);
            for(int i = 0; i < n; i++) {
                System.out.println(name[i]+" "+score[i]);
            }
        }
        sc.close();
    }
    private static void sort(String name[], int score[], int n, int flag) {//用稳定排序,冒泡排序
        if(flag == 0) {//降序
            for(int i = n - 1; i > 0; i--) {
                for(int j = 0; j < i; j++) {
                    if(score[j] < score[j + 1]) {
                        int tmpi = score[j];
                        score[j] = score[j + 1];
                        score[j + 1] = tmpi;
                        String tmps = name[j];
                        name[j] = name[j + 1];
                        name[j + 1] = tmps;
                    }
                }
            }
        }else if(flag == 1) {//升序
            for(int i = n - 1; i > 0; i--) {
                for(int j = 0; j < i; j++) {
                    if(score[j] > score[j + 1]) {
                        int tmpi = score[j];
                        score[j] = score[j + 1];
                        score[j + 1] = tmpi;
                        String tmps = name[j];
                        name[j] = name[j + 1];
                        name[j + 1] = tmps;
                    }
                }
            }
        }
    }
}

五、
如果A是个x行y列的矩阵,B是个y行z列的矩阵,把A和B相乘,其结果将是另一个x行z列的矩阵C。
输入描述:
输入说明:
1、第一个矩阵的行数
2、第一个矩阵的列数和第二个矩阵的行数
3、第二个矩阵的列数
4、第一个矩阵的值
5、第二个矩阵的值
输出描述:
输出两个矩阵相乘的结果
示例1
输入
2 2 2
3 8
8 0
9 0
18 9
输出
171 72
72 0

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int ai = sc.nextInt();
            int aj = sc.nextInt();
            int bj = sc.nextInt();
            int[][] a = new int[ai][aj];
            int[][] b = new int[aj][bj];
            for(int i = 0; i < ai; i++) {
                for(int j = 0; j < aj; j++) {
                    a[i][j] = sc.nextInt();
                }
            }
            for(int i = 0; i < aj; i++) {
                for(int j = 0; j < bj; j++) {
                    b[i][j] = sc.nextInt();
                }
            }
            int[][] c = multi(a, b);
            for(int i = 0; i < ai; i++) {
                for(int j = 0; j < bj - 1; j++) {
                    System.out.print(c[i][j]+" ");
                }
                System.out.println(c[i][bj - 1]);
            }
        }
        sc.close();
    }
    public static int[][]multi(int [][]a,int [][]b){
        int rowRes=a.length;//a的行数
        int columnRes=b[0].length;//b的列数
        int columna=a[0].length;//a的列数,b的行数
        int [][]c=new int[rowRes][columnRes];
        for(int i=0;i<rowRes;i++){
            for(int j=0;j<columnRes;j++){
                for(int k=0;k<columna;k++){
                    c[i][j]+=a[i][k]*b[k][j];
                }
            }
        }
        return c;
    }
}

六、
矩阵乘法的运算量与矩阵乘法的顺序强相关。
例如:
A是一个50×10的矩阵,B是10×20的矩阵,C是20×5的矩阵
计算A*B*C有两种顺序:((AB)C)或者(A(BC)),前者需要计算15000次乘法,后者只需要3500次。
编写程序计算不同的计算顺序需要进行的乘法次数
输入描述:
输入多行,先输入要计算乘法的矩阵个数n,每个矩阵的行数,列数,总共2n的数,最后输入要计算的法则
输出描述:
输出需要进行的乘法次数
示例1
输入
3
50 10
10 20
20 5
(A(BC))
输出
3500

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //类似编译原理的词法分析,用栈
        while(sc.hasNext()) {
            int n = sc.nextInt();
            int[][] a= new int[n][2];
            for(int i = 0; i < n; i++) {
                a[i][0] = sc.nextInt();
                a[i][1] = sc.nextInt();
            }
            String str = sc.next();
            System.out.println(process(n, a, str.toCharArray()));
        }
        sc.close();
    }

    public static int process(int n, int[][] a, char[] chs){
        Stack<Integer> stk = new Stack<>();
        int res = 0;
        int index = 0;
        for(int i = 0; i < chs.length; i++) {
            if(chs[i] == '(') {
                continue;
            }else if(chs[i] >= 'A' && chs[i] <= 'Z') {
                stk.push(a[index][0]);
                stk.push(a[index][1]);
                index++;
            }else if(chs[i] == ')') {//遇到右括号弹出右括号前的两个矩阵计算规约
                int last = stk.pop();
                int mid = stk.pop();stk.pop();
                int first = stk.pop();
                res += first * mid * last;
                stk.push(first);
                stk.push(last);
                if(stk.size() == 2) {//只剩一个矩阵时,计算完成
                    break;
                }
            }
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值