20190826瓜子二手车机试

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qunqunstyle99/article/details/100080619

有七个单选一个多选六个填空,不少数学的概率题目。
机试题目很简单,比较友好。

题目一 计算数组高度

给定一个数组A,定义元组(i,j),其中i<j且A[i] <= A[j],这样的元组告诉是 j - i 。找出A中元组的最大高度。如果不存在,返回0;

输入描述:
第一行一个整数表示数组长度。
第二行n个整数表示数组元素

输出描述:
一个整数

示例输入:
6
6 0 8 2 1 5
示例输出:
4

题目分析:
直接暴力,二重循环,前后两个指针,向中间靠拢,计算最大的,可以进行剪枝,不过不剪也可以过,问题不大。

代码实现:

//AC
import java.util.Scanner;

public class Main {
    public static void main(String []args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int input [] = new int[n];
        for(int i = 0;i<n ;i++)
            input[i] = in.nextInt();
        int max = 0;
        int temp = 0;
        for(int i = 0;i<n ;i++){
            for(int wear = n-1 ;wear>i;wear--){
                if(input[wear]>input[i]){
                    max = Math.max(max,wear-i);
                }
            }
        }
        System.out.print(max);
    }
}

第二题 检测大写字母

给定一个单词判断大写使用是否正确,定义三种正确的情况:

  • 全部字母都是大写,如USA
  • 所有的字母都是小写
  • 只有首字母大写

这三种情况返回 true , 其余情况返回 false。

示例输入
USA
示例输出
true

题目分析:
就直接循环遍历吧,分情况讨论:

  • 若是第一个字母时小写,则后面的字母只要出现大写就判断失败
  • 若第一个字母大写,第二个字母小写,则从第三个开始必须都是小写
  • 若第一个字母大写,第二个字母大写,则从第三个开始必须都是大写

还有就是应该加上长度的判断,要是单词就是一个字母,那就直接输出true,把边界都看清楚,但是这个题目中的用例显然没有涉及到,我没有处理这些异常情况也都过了。

代码实现:

//AC
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        boolean istrue = true;
        if (input.charAt(0) <= 'Z' && input.charAt(0) >= 'A') {
            if (input.charAt(1) <= 'Z' && input.charAt(0) >= 'A') {
                int len = input.length();
                while (len-- > 1) {
                    if (input.charAt(len) <= 'Z' && input.charAt(len) >= 'A')
                        continue;
                    else {
                        System.out.println("false");
                        istrue = false;
                        break;
                    }
                }
            } else {
                int len = input.length();
                while (len-- > 1) {
                    if (input.charAt(len) <= 'z' && input.charAt(len) >= 'a')
                        continue;
                    else {
                        System.out.println("false");
                        istrue = false;
                        break;
                    }
                }
            }
        } else {
            int len = input.length();
            while (len-- > 0) {
                if (input.charAt(len) <= 'z' && input.charAt(len) >= 'a')
                    continue;
                else {
                    System.out.println("false");
                    istrue = false;
                    break;
                }
            }
        }
        if (istrue)
            System.out.println("true");
    }
}

猜你喜欢

转载自blog.csdn.net/qunqunstyle99/article/details/100080619