华为机试-牛客刷题

华为机试

import java.util.Scanner;

/**
 * 数字颠倒
 * @Description
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int in = scanner.nextInt();
        String str = String.valueOf(in);
        for (int i = str.length()-1; i >=0 ; i--) {
            System.out.print(str.charAt(i));
        }
    }
}


import java.util.Scanner;

/**
 * 字符串反转
 * @Description
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        for (int i = str.length()-1; i >=0 ; i--) {
            System.out.print(str.charAt(i));
        }
    }
}


import java.util.Scanner;

/**
 * @Description 汽水瓶-数学方法
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            int n = scanner.nextInt();
            if (n == 0) {
                break;
            }
            System.out.println(n/2);
        }
    }
}


import java.util.Scanner;
/**
 * @Description 汽水瓶-递归
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            //空汽水瓶个数
            int n=sc.nextInt();
            if (n == 0) {
                break;
            }
            //可以兑换的满瓶汽水数
            int count=0;
            //每次兑换剩余的空汽水瓶
            int tmp;
            while(n>=2){
                count+=n/3;
                tmp=n%3;
                //每次完成兑换之后还剩余的空汽水瓶数目
                n=n/3+tmp;
                //如果只剩2个空汽水瓶,则可以借一瓶,喝完后兑换一瓶满的还给老板
                if(n==2){
                    count++;
                    break;
                }
            }
            System.out.println(count);
        }
    }
}


import java.util.Scanner;
/**
 * @Description 单词倒排
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        System.out.println(reversestr(str));
    }
    public static String reversestr(String str){
        String[] splitstr = str.split("[^a-zA-Z]");
        StringBuffer reversestr = new StringBuffer();

        for(int i=splitstr.length-1;i>=0;i--){
            reversestr.append(splitstr[i]).append(" ");
        }
        str = reversestr.toString().trim();
        return str;
    }
}


import java.util.Scanner;
/**
 * @Description 字符逆序
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        char[] chars = str.toCharArray();
        for(int i=chars.length-1;i>=0;i--){
            System.out.print(chars[i]);
        }
    }
}


import java.util.Scanner;
/**
 * @Description 蛇形矩阵
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String args[]){//主方法,主要就是接收数据
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int num=sc.nextInt();
            printtriangle(num);
        }
    }

    public static void printtriangle(int num){ //输出方法输出蛇形矩阵
        int y=1;//列的方向第一个肯定是1
        int yCount=1;//列由上往下是等差数列,第一行和第二行差1
        for(int i=1;i<=num;i++){//行的循环次数
            int x=y;//第i行的第一个数就是列的第i个数
            int xCount=i+1; //可以看到第i行第一个加数刚好是i+1
            for(int j=1;j<=num-i+1;j++){ //列的循环次数,每次少一列
                System.out.print(x+" ");//循环输出x
                x+=xCount++;    //等差数列每次+1
            }
            System.out.println("");//换行
            y+=yCount++; //等差数列每次加1
        }
    }
}


import java.util.Scanner;
/**
 * @Description 兔子总数
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String args[]){//主方法,主要就是接收数据
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int num=sc.nextInt();
            System.out.println(test(num));
        }
    }
    public static int test(int num){
        if (num == 1 || num == 2) {
            return 1;
        }
        return test(num-1)+test(num-2);
    }
}


import java.util.Scanner;
/**
 * @Description 等差数列
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String args[]){//主方法,主要就是接收数据
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int num=sc.nextInt();
            System.out.println(test(num));
        }
    }
    public static int test(int n){
        return (1 + 3*n) * n / 2;
    }
}


import java.util.Scanner;
/**
 * @Description 最小公倍数
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String args[]){//主方法,主要就是接收数据
        Scanner sc=new Scanner(System.in);
        int a=sc.nextInt();
        int b=sc.nextInt();
        System.out.println(a*b/gcd(a,b));
    }
    public static int gcd(int a,int b){
        if (b == 0) {
            return a;
        }
        return gcd(b,a%b);
    }
}


import java.util.Scanner;
/**
 * @Description 放苹果
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main {
    public static void main(String args[]){//主方法,主要就是接收数据
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            int m=sc.nextInt();
            int n=sc.nextInt();
            //边界问题
            if (n >= 1 && n <= 10 && m >= 1 && m <= 10) {
                System.out.println(count(m, n));
            }
        }
    }
    public static int count(int m,int n){
        if(m<0||n<=0)
            return 0;
        //细分到苹果数为一或盘子数为一的情况返回一
        if(m==1||n==1||m==0)
            return 1;
        //将此事件无线细分
        return count(m,n-1)+count(m-n,n);
    }
}


import java.util.Scanner;
/**
 * @Description 日期天数
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        String[] arr = str.split(" ");
        int a = Integer.parseInt(arr[0]);
        int b = Integer.parseInt(arr[1]);
        int c = Integer.parseInt(arr[2]);
        int[] month = {31,28,31,30,31,30,31,31,30,31,30,31};
        int count = 0;
        for(int i=0;i<b-1;i++){
            count+=month[i];
        }
        count+=c;
        //判断是闰年,大于2月的加一天
        if(b>2 && (a%4==0&&a%100!=0 || a%400 ==0)){
            count++;
        }
        System.out.println(count);
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * @Description 回文子串
 * 思路:找出字符串所有的子串,回文串既然左右对称,则字符串与字符串的反转必定相同,故可以判断是否是回文串。然后在所有子串中找出最大的回文串。
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        // 获取字符串的中所有的子串
        List<String> list =  getListStr(str);
        int maxLen = 0;
        for (String obj: list) {
            StringBuffer temp = new StringBuffer(obj);
            // 判断是否是回文子串:回文子串反转后与原来的相同
            if(obj.equals(temp.reverse().toString())){
                // 找出最大回文子串长度
                if(maxLen < obj.length()){
                    maxLen = obj.length();
                }
            }
        }
        System.out.println(maxLen);
    }
    private static List<String>  getListStr(String str) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
            for (int j = i+1; j <= str.length(); j++) {
                list.add(str.substring(i,j));
            }
        }
        return list;
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * @Description 公共子串计算
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String stra = sc.nextLine();
        String strb = sc.nextLine();
        // 获取字符串的中所有的子串
        List<String> lista =  getListStr(stra);
        List<String> listb =  getListStr(strb);
        int maxLen = 0;
        for (String obja: lista) {
            for (String objb: listb){
                if(obja.equals(objb)){
                    // 找出最大子串长度
                    if(maxLen < objb.length()){
                        maxLen = objb.length();
                    }
                }
            }
        }
        System.out.println(maxLen);
    }

    private static List<String>  getListStr(String str) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
            for (int j = i+1; j <= str.length(); j++) {
                list.add(str.substring(i,j));
            }
        }
        return list;
    }
}


import java.util.Scanner;
/**
 * @Description 统计大写字母个数
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String str = sc.nextLine();
            int len= str.replaceAll("[^A-Z]","").length();
            System.out.println(len);
        }
    }
}


import java.util.Scanner;
/**
 * @Description 尼科彻斯定理
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int n = sc.nextInt();
            int b = n*(n-1)+1;
            for (int i = 0; i < n; i++) {
                if (i != n-1) {
                    System.out.print(b+2*i+"+");
                }else {
                    System.out.println(b+2*i);
                }
            }
        }
    }
}


import java.util.Scanner;

/**
 * @Description  记负均正
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int n = sc.nextInt();
            int countA = 0;
            int countB = 0;
            int temp = 0;
            double ave;
            for (int i = 0; i < n; i++) {
                int b = sc.nextInt();
                if (b < 0) {
                    countA ++;
                }else if (b > 0) {
                    countB ++;
                    temp += b;
                }
            }
            ave = (double) temp/(double) countB;
            //解决四舍五入保留一位小数的情况
//            BigDecimal b = new BigDecimal(ave);
//            ave = b.setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
            System.out.println(countA+" "+String.format("%.1f", ave));
        }
    }
}


import java.util.Scanner;
/**
 * @Description 完全数计算
 * @Author haixiaofei
 * @Date 2022/2/11 9:20
 **/
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int n = in.nextInt();
            int count = 0;    //计数变量
            for(int i=1; i <= n; i++){
                int sum = 0;
                //统计因数的和,计数到该数的1/2即可
                for(int j=1; j <= i/2; j++){
                    if(i%j == 0)
                        sum += j;
                }
                if(sum == i)
                    count++;
            }
            //输出结果
            System.out.println(count);
        }
    }
}


import java.util.Scanner;
/**
 * @Description 百钱买百鸡
 * @Author haixiaofei
 * @Date 2022/2/15 9:20
 **/
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int n = in.nextInt();
            int a = 0,b = 0,c = 0;
            for (a = 0; a <= 20; a++) {
                for (b = 0; b <=33 ; b++) {
                    for (c = 0; c < 100; c++) {
                        if (c%3 ==0 && 5*a+3*b+c/3 == 100 && a+b+c == 100) {
                            //输出结果
                            System.out.println(a+" "+b+" "+c);
                        }
                    }
                }
            }

        }
    }
}


import java.util.Scanner;
/**
 * @Description 走方格的方案数
 * @Author haixiaofei
 * @Date 2022/2/15 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextInt()) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            System.out.println(cal(m,n));
        }

    }
    private static int cal(int m,int n){
        if(m==1 || n== 1){
            return m+n;
        }
        return cal(m-1,n)+cal(m,n-1);
    }
}


import java.util.Scanner;
/**
 * @Description 查找组成一个偶数最接近的两个素数
 * @Author haixiaofei
 * @Date 2022/2/15 9:20
 **/
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            for(int i=n/2;i>=2;i--){//最接近的素数 就从数的中间开始
                if(isPrime(i)&&isPrime(n-i)){
                    System.out.println(i);
                    System.out.println(n-i);
                    break;
                }
            }
        }
    }
    public static boolean isPrime(int n){
        //素数 除了1和它本身的数,都不能被整除,所以要从2 开始到小于n
        for(int i =2;i<n;i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
}


import java.util.Scanner;
/**
 * @Description 求最大连续bit数
 * @Author haixiaofei
 * @Date 2022/2/15 9:20
 **/
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            // 转二进制
            String binaryStr = Integer.toBinaryString(n);
            // 用0 分割
            String[] strArray = binaryStr.split("0");
            // 字符串长度
            int result = 0;
            for (int i = 0; i < strArray.length; i++) {
                if (strArray[i].length() > result) {
                    result = strArray[i].length();
                }
            }
            System.out.println(result);
        }
    }
}


import java.util.Scanner;
/**
 * @Description 杨辉三角的变形
 * @Author haixiaofei
 * @Date 2022/2/15 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            int num = sc.nextInt();
            if(num == 1 || num == 2){
                System.out.println(-1);
            }else {
                if(num % 2==1){
                    System.out.println(2);
                }else  if(num %4 == 2){
                    System.out.println(4);
                }else {
                    System.out.println(3);
                }
            }
        }
    }
}

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * @Description 参数解析
 * @Author haixiaofei
 * @Date 2022/2/16 9:20
 **/
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        StringBuilder stringBuilder = new StringBuilder();
        List<String> arrayList = new ArrayList<>();
        boolean flag = false;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            if (String.valueOf(c).equals("\"")) {
                flag = !flag;
                continue;
            }

            if (String.valueOf(c).equals(" ") && !flag) {

                arrayList.add(stringBuilder.toString());
                stringBuilder = new StringBuilder();
            } else {
                stringBuilder.append(c);
            }

        }
        arrayList.add(stringBuilder.toString());
        System.out.println(arrayList.size());
        for (String s : arrayList) {
            System.out.println(s);
        }
    }
}


import java.util.*;
/**
 * @Description 表达式求值
 * @Author haixiaofei
 * @Date 2022/2/16 9:20
 **/
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        String s=sc.nextLine();
//        //将其他括号,替换成小括号
        s=s.replace("{","(");
        s=s.replace("[","(");
        s=s.replace("}",")");
        s=s.replace("]",")");
        System.out.println(slove(s));
    }
    public static int slove(String s){
        Stack<Integer> stack=new Stack<>();
        int n=s.length();
        char[] chs=s.toCharArray();
        //初始化符号为'+'
        char sign='+';
        //记录数字
        int number=0;
        for(int i=0;i<n;i++){
            char ch=chs[i];
            //当前字符是空格,跳过
            if(ch==' ')continue;
            //当前字符是数字,拼数字
            if(Character.isDigit(ch)){
                number=number*10+ch-'0';
            }
            //如果当前字符是小括号
            if(ch=='('){
                //移到小括号后一位字符
                int j=i+1;
                //统计括号的数量
                int count=1;
                while(count>0){
                    //遇到右括号,括号数-1
                    if(chs[j]==')')count--;
                    //遇到左括号,括号数+1
                    if(chs[j]=='(')count++;
                    j++;
                }
                //递归,解小括号中的表达式
                number=slove(s.substring(i+1,j-1));
                i=j-1;
            }
            //遇到符号,将数字处理后放进栈
            if(!Character.isDigit(ch) || i==n-1){
                //如果是'+',直接入栈
                if(sign=='+'){
                    stack.push(number);
                }
                //如果是'-',数字取相反数在入栈
                else if(sign=='-'){
                    stack.push(-1*number);
                }
                //如果是'*',弹出一个数字乘后放入栈
                else if(sign=='*'){
                    stack.push(stack.pop()*number);
                }
                //如果是'/',弹出一个数字/后放入栈
                else if(sign=='/'){
                    stack.push(stack.pop()/number);
                }
                //更新符号
                sign=ch;
                //刷新数字
                number=0;
            }
        }
        //栈中数字求和得到结果
        int ans=0;
        while(!stack.isEmpty()){
            ans+=stack.pop();
        }
        return ans;
    }
}

import java.util.Scanner;
/**
 * @Description 二维数组操作
 * @Author haixiaofei
 * @Date 2022/2/16 9:20
 **/
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            int m = sc.nextInt();
            int n = sc.nextInt();
            int x1 = sc.nextInt();
            int y1 = sc.nextInt();
            int x2 = sc.nextInt();
            int y2 = sc.nextInt();
            int x = sc.nextInt();
            int y = sc.nextInt();
            int a = sc.nextInt();
            int b = sc.nextInt();
            if (m<=9 && n<=9) {
                System.out.println(0);
            }else {
                System.out.println(-1);
            }
            if (x1<m && x2<m && y1<n && y2<n) {
                System.out.println(0);
            }else {
                System.out.println(-1);
            }
            if (x<m && m<9) {
                System.out.println(0);
            }else {
                System.out.println(-1);
            }
            if (y<n && n<9) {
                System.out.println(0);
            }else {
                System.out.println(-1);
            }
            if (a<m && b<n) {
                System.out.println(0);
            }else {
                System.out.println(-1);
            }
        }
    }
}


import java.util.LinkedHashMap;
import java.util.Scanner;
//HJ94 记票统计
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            int n = in.nextInt();
            String[] names = new String[n];
            LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
            for (int i = 0; i < n; i++) {
                names[i] = in.next();
                map.put(names[i], 0);
            }

            int num = in.nextInt();
            String[] votes = new String[num];
            for (int i = 0; i < num; i++) {
                votes[i] = in.next();
            }

            for (int i = 0; i < num; i++) {
                for (int j = 0; j < n; j++) {
                    if (names[j].equals(votes[i])) {
                        map.put(names[j], map.get(names[j]) + 1);
                    }
                }
            }

            int value = 0;
            for (String str : map.keySet()) {
                System.out.println(str + " : " + map.get(str));
                value = value + map.get(str);
            }
            System.out.println("Invalid : " + (num - value));
        }
    }

}


import java.util.Scanner;

/**
 * HJ107 求解立方根
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double num = sc.nextDouble();
        double x = Dichotomy(num);
        System.out.printf("%.1f", x);
    }

    //使用类似二分的思路
    public static double Dichotomy(double num) {
        double right, left, mid;
        //一定要注意边界条件,输入的num可能是负数  将x<-1的边界范围定为[x,1],x>1的边界范围定为[-1,x]
        right = Math.max(1.0, num);
        left = Math.min(-1.0, num);
        while (right - left > 0.0000001) {
            mid = (left + right) / 2;
            //如果乘积大于num,说明立方根在mid的左侧
            if (mid * mid * mid > num) {
                right = mid;
            }
            //如果乘积小于num,说明立方根在mid的右侧
            else if (mid * mid * mid < num) {
                left = mid;
            } else {
                return mid;
            }
        }
        return right;
    }
}


import java.util.*;

public class Solution {
    public static void main(String[] args) {
        int[] a = {10, 2,8,66,54,10};
        Solution solution = new Solution();
        System.out.println(solution.test(a));

    }

    /**
     * 输入一个整性数组,输出组成最大的整数,比如输入:{10, 2,8,66,54,10} 输出 :8665421100
     * @param a
     * @return
     */
    public String test(int[] a){
        StringBuilder sb = new StringBuilder();
        for (int j : a) {
            sb.append(j);  //将数组转为字符串
        }
        String str = sb.toString();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
            //将char转为int类型
            int c = Character.getNumericValue(str.charAt(i));
            list.add(c);
        }
        //对数组升序
        Collections.sort(list);
        //数组转为字符串
        StringBuilder stringBuilder = new StringBuilder();
        for (Integer integer : list) {
            stringBuilder.append(integer);
        }
        //逆序输出
        return stringBuilder.reverse().toString();
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值