【48天笔试强训】day8

两种排序方法

描述

考拉有n个字符串字符串,任意两个字符串长度都是不同的。考拉最近学习到有两种字符串的排序方法: 1.根据字符串的字典序排序。例如:
"car" < "carriage" < "cats" < "doggies < "koala"
2.根据字符串的长度排序。例如:
"car" < "cats" < "koala" < "doggies" < "carriage"
考拉想知道自己的这些字符串排列顺序是否满足这两种排序方法,考拉要忙着吃树叶,所以需要你来帮忙验证。

输入描述:

输入第一行为字符串个数n(n ≤ 100) 接下来的n行,每行一个字符串,字符串长度均小于100,均由小写字母组成

输出描述:

如果这些字符串是根据字典序排列而不是根据长度排列输出"lexicographically",
如果根据长度排列而不是字典序排列输出"lengths",
如果两种方式都符合输出"both",否则输出"none"


示例1

输入:

3
a
aa
bbb

复制输出:

both

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n=in.nextInt();
        String[] str=new String[n];
        for(int i=0;i<n;i++){
            str[i]=in.next();
        }

        if(lexicographically(str)&&length(str)){
            System.out.println("both");
        }else if(lexicographically(str)){
            System.out.println("lexicographically");
        }else if(length(str)){
            System.out.println("lengths");
        }else{
            System.out.println("none");
        }
    }

    public static boolean lexicographically(String[] strs){
        for(int i=0;i<strs.length-1;i++){
            //前一个字典顺序比后一个大,那么说明错误
            if(strs[i].compareTo(strs[i+1])>0){
                return false;
            }
        }
        //否则就是正确
        return true;
    }

    public static boolean length(String[] strs){
        for(int i=0;i<strs.length-1;i++){
            //前一个字典顺序比后一个大,那么说明错误
            if(strs[i].length()>strs[i+1].length()){
                return false;
            }
        }
        //否则就是正确
        return true;
    }
}

求最小公倍数

描述

正整数A和正整数B 的最小公倍数是指 能被A和B整除的最小的正整数值,设计一个算法,求输入A和B的最小公倍数。

数据范围:1≤�,�≤100000 1≤a,b≤100000 

输入描述:

输入两个正整数A和B。

输出描述:

输出A和B的最小公倍数。

示例1

输入:

5 7

复制输出:

35

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int A=in.nextInt();
        int B=in.nextInt();
        int M=Math.max(A,B);
        while(M%A!=0||M%B!=0){
            M++;
        }
        System.out.println(M);
    }
}

相关推荐

  1. 48笔试day8

    2024-03-10 22:36:04       22 阅读
  2. 48笔试day13

    2024-03-10 22:36:04       20 阅读
  3. 48笔试day18

    2024-03-10 22:36:04       16 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-10 22:36:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-10 22:36:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-10 22:36:04       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-10 22:36:04       20 阅读

热门阅读

  1. https 加密解密过程是什么?

    2024-03-10 22:36:04       21 阅读
  2. for-in,for-of

    2024-03-10 22:36:04       20 阅读
  3. GPT的磁盘管理

    2024-03-10 22:36:04       23 阅读
  4. volatile

    2024-03-10 22:36:04       21 阅读
  5. HTML5- 拖拽功能

    2024-03-10 22:36:04       20 阅读
  6. Flask基于配置文件添加项目config配置

    2024-03-10 22:36:04       19 阅读
  7. 深入了解 Python 的 compile() 函数

    2024-03-10 22:36:04       20 阅读
  8. python中def一个方法,就一定对应一个return吗

    2024-03-10 22:36:04       19 阅读
  9. AI辅助研发

    2024-03-10 22:36:04       23 阅读