【牛客网】NC31 第一个只出现一次的字符

选择

以下关于集合类 ArrayList 、 LinkedList 、 HashMap 描述错误的是:
A HashMap实现Map接口,它允许任何类型的键和值对象,并允许将null用作键或值
B ArrayList和LinkedList均实现了List接口
C 添加和删除元素时,ArrayList的表现更佳
D ArrayList的访问速度比LinkedList快
答案:C

ArrayList list = new ArrayList(20);中的list扩充几次
A 0
B 1
C 2
D 3
正确答案: A

用不带头结点的单链表存储队列,其队头指针指向队头结点,队尾指针指向队尾结点,则在进行出队操作时()
A 仅修改队头指针
B 仅修改队尾指针
C 队头、队尾指针都可能要修改
D 队头、队尾指针都要修改

只有一个节点:对头队尾都要置空 C
在这里插入图片描述

将一颗有 100 个结点的完全二叉树从根这一层开始,每一层从左到右依次对结点进行编号,根节点编号为 1 ,则编号为 98 的节点的父节点编号为()
A 47
B 48
C 49
D 50
正确答案: C

在Java中,HashMap中是用哪些方法来解决哈希冲突的?
A 开放地址法
B 二次哈希法
C 链地址法
D 建立一个公共溢出区
正确答案: C

下列各排序法中,最坏情况下的时间复杂度最低的是( )
A 希尔排序
B 快速排序
C 堆排序
D 冒泡排序
正确答案: C
参考答案:
堆排序最坏情况时间下的时间复杂度为 O(nlog2n) ;希尔排序最坏情况时间下的时间复杂度为 O(n1.5) ;快速排序、冒泡排序最坏情况时间下的时间复杂度为
O(n2) 。故本题答案为 C 选项。


编程

Fibonacci数列

Fibonacci数列
Fibonacci数列是这样定义的:
F[0] = 0
F[1] = 1
for each i ≥ 2: F[i] = F[i-1] + F[i-2]
因此,Fibonacci数列就形如:0, 1, 1, 2, 3, 5, 8, 13, …,在Fibonacci数列中的数我们称为Fibonacci数。给你一个N,你想让其变为一个Fibonacci数,每一步你可以把当前数字X变为X-1或者X+1,现在给你一个数N求最少需要多少步可以变为Fibonacci数。

import java.util.*;

public class Main {
    
    
    public static int Fib(int n) {
    
    
        int f1 = 1;
        int f2 = 1;
        int f3 = f1 + f2;
        while (f3 < n) {
    
    
            f1 = f2;
            f2 = f3;
            f3 = f1 + f2;
        }
        if (n - f2 < f3 - n) {
    
    
            return n - f2;
        } else {
    
    
            return f3 - n;
        }
    }
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int ret = Fib(n);
        System.out.println(ret);
    }
}

下厨房

下厨房

牛牛想尝试一些新的料理,每个料理需要一些不同的材料,问完成所有的料理需要准备多少种不同的材料。

输入描述:
每个输入包含 1 个测试用例。每个测试用例的第 i 行,表示完成第 i 件料理需要哪些材料,各个材料用空格隔开,输入只包含大写英文字母和空格,输入文件不超过 50 行,每一行不超过 50 个字符。

输出描述:
输出一行一个数字表示完成所有料理需要多少种不同的材料。

示例1
输入
BUTTER FLOUR
HONEY FLOUR EGG
输出
4

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Set<String> set = new HashSet<>();
        Scanner scan = new Scanner(System.in);
        while(scan.hasNextLine()) {
    
    
            String str = scan.nextLine();
            String[] strings = str.split(" ");
            for(int i = 0; i < strings.length; i++) {
    
    
                set.add(strings[i]);
            }
        }
        System.out.println(set.size());
    }
}


NC32 求平方根

添加链接描述
描述
实现函数 int sqrt(int x).
计算并返回 x 的平方根(向下取整)

数据范围: 0 <= x < 2^{31}-10<=x<2
31
−1
要求:空间复杂度 O(1)O(1),时间复杂度 O(logx)O(logx)

示例1
输入:
2
返回值:
1

示例2
输入:
2143195649
返回值:
46294

import java.util.*;

public class Solution {
    
       
    // 二分
    public int sqrt (int x) {
    
    
        if(x == 1) return 1;
        int left = 1;
        int right = x / 2; //
        while(left <= right) {
    
    
            int mid = left + (right - left) / 2;
            if(x / mid > mid) {
    
     //不用x > mid * mid 防溢出
                left = mid + 1;
            } else if (x / mid < mid) {
    
    
                right = mid - 1;
            } else {
    
    
                return mid;
            }
        }
        return right;
    }
    
    // 2、根据平方数的性质——连续n个奇数相加的结果一定是平方数
    public int sqrt2 (int x) {
    
    
        if(x < 0) return 0;
        int i = 1;
        int ans = 0;
        while(x >= 0) {
    
    
            x -= i;
            i += 2;
            ans++;
        }
        return ans -1;
    }
    
    public int sqrt1 (int x) {
    
    
        // write code here
        double mul = Math.sqrt(x);
        return (int)Math.floor(mul);
    }
}

floor方法 —— round方法

NC32 求平方根

添加链接描述

实现函数 int sqrt(int x).
计算并返回 x 的平方根(向下取整)

数据范围: 0 <= x < 2^{31}-10<=x<2
31
−1
要求:空间复杂度 O(1)O(1),时间复杂度 O(logx)O(logx)
示例1
输入:2
返回值:1

import java.util.*;

public class Solution {
    
    
    /**
     * @param x int整型 
     * @return int整型
     */
    public int sqrt (int x) {
    
    
        // write code here
        double mul = Math.sqrt(x);
        return (int)Math.floor(mul);
    }
}

NC31 第一个只出现一次的字符

添加链接描述

描述
在一个长为 字符串中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)

数据范围:0 \le n \le 100000≤n≤10000,且字符串只有字母组成。
要求:空间复杂度 O(n)O(n),时间复杂度 O(n)O(n)
示例1
输入:
“google”
返回值:
4

import java.util.HashMap;

public class Solution {
    
    
    // 2、哈希
    public int FirstNotRepeatingChar(String str) {
    
    
        HashMap<Character, Boolean>  map = new HashMap<>();
        char[] chars = str.toCharArray();
        for(char c : chars) {
    
    
            if(map.containsKey(c)) {
    
    
                map.put(c, false); // 重复 置为false
            }else {
    
    
                map.put(c, true); // 第一次出现
            }
        }
        for (int i = 0; i < chars.length; i++) {
    
    
            // 根据指定的k 查找对应的v 为true返回
            if(map.get(chars[i])) {
    
    
                return i;
            }
        }
        return -1;
    }
    
    // 1、数组
    public int FirstNotRepeatingChar1(String str) {
    
    
        int[] times = new int[123];
        for (int i = 0; i < str.length(); i++) {
    
    
            times[str.charAt(i)]++;
        }
        for (int i = 0; i < str.length(); i++) {
    
    
            // 遍历字符串 返回下标
            if(times[str.charAt(i)] == 1) {
    
    
                return i;
            }
        }
        return -1;
    }
}

NC7 买卖股票的最好时机(一)

添加链接描述
描述
假设你有一个数组prices,长度为n,其中prices[i]是股票在第i天的价格,请根据这个价格数组,返回买卖股票能获得的最大收益
1.你可以买入一次股票和卖出一次股票,并非每天都可以买入或卖出一次,总共只能买入和卖出一次,且买入必须在卖出的前面的某一天
2.如果不能获取到任何利润,请返回0
3.假设买入卖出均无手续费

数据范围: 0 \le n \le 10000 , 0 \le val \le 100000≤n≤10000,0≤val≤10000
要求:空间复杂度 O(1)O(1),时间复杂度 O(n)O(n)

示例1
输入:
[8,9,2,5,4,7,1]
返回值:
5

说明:
在第3天(股票价格 = 2)的时候买入,在第6天(股票价格 = 7)的时候卖出,最大利润 = 7-2 = 5 ,不能选择在第2天买入,第3天卖出,这样就亏损7了;同时,你也不能在买入前卖出股票。

import java.util.*;

public class Solution {
    
    
    /**
     * 
     * @param prices int整型一维数组 
     * @return int整型
     */
    public int maxProfit (int[] prices) {
    
    
        // write code here
        int min = prices[0];
        int max = 0;
        for (int i = 0; i < prices.length; i++) {
    
    
            min = Math.min(min, prices[i]);
            max = Math.max(max, prices[i] - min);
        }
        return max;
    }
    
    public int maxProfit1 (int[] prices) {
    
    
        // write code here
        int max = 0;
        for(int i = 0; i < prices.length; i++) {
    
    
            for(int j = i+1; j < prices.length; j++) {
    
    
                if(i == j) continue;
                int sub = prices[j] - prices[i];
                if(max < sub) {
    
    
                    max = sub;
                }
            }
        }
        return max;
    }
}

NC22 合并两个有序的数组

添加链接描述

import java.util.*;
public class Solution {
    
    
    public void merge(int A[], int m, int B[], int n) {
    
    
        int[] ret = new int[m+n];
        int i = 0; // 记录A数组下标
        int j = 0; // 记录B数组下标
        int k = 0; // 记录存放ret的下标
        while(i < m && j < n) {
    
    
            if(A[i] < B[j]) {
    
    
                ret[k++] = A[i++];
            }else {
    
    
                ret[k++] = B[j++];
            }
        }
        // A没放完
        while(i < m) {
    
    
            ret[k++] = A[i++];
        }
        // B没放完
        while(j < n) {
    
    
            ret[k++] = B[j++];
        }
        // 把ret拷贝到A中
        k = 0;
        while(k < ret.length) {
    
    
            A[k] = ret[k];
            k++;
        }
    }
    
    // 1、合并 排序
    public void merge1(int A[], int m, int B[], int n) {
    
    
        for (int i = 0; i < n; i++) {
    
    
            A[m++] =  B[i];
        }
        Arrays.sort(A);
    }
}

NC103 反转字符串

添加链接描述

import java.util.*;

public class Solution {
    
    
    /**
     * 反转字符串
     * @param str string字符串 
     * @return string字符串
     */
    public String solve (String str) {
    
    
        char[] chars = new char[str.length()];
        for (int i = 0; i < str.length(); ++i) {
    
    
            chars[i] = str.charAt(str.length()-1-i);
        }
        return new String(chars);
    }

    public String solve2 (String str) {
    
    
        char[] chars = str.toCharArray();
        int left = 0;
        int right = str.length() - 1;
        while(left < right) {
    
    
            char tmp = chars[left];
            chars[left++] = chars[right];
            chars[right--] = tmp;
        }
        return new String(chars);
    }
    
    public String solve1 (String str) {
    
    
        // write code here
        return new StringBuffer(str).reverse().toString();
    }
}

NC141 判断是否为回文字符串

添加链接描述

import java.util.*;

public class Solution {
    
    
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param str string字符串 待判断的字符串
     * @return bool布尔型
     */
    public boolean judge (String str) {
    
    
        // write code here
        StringBuffer sb = new StringBuffer(str).reverse();
        return str.equals(sb.toString());
    }
    
    public boolean judge1 (String str) {
    
    
        // write code here
        if(str.length() == 0) {
    
    
            return true;
        }
        int left = 0;
        int right = str.length() - 1;
        while(left < right) {
    
    
            if(str.charAt(left++) != str.charAt(right--)) {
    
    
                return false;
            }
        }
        return true;
    }
}

JAVA48 回文数判断

添加链接描述

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner console = new Scanner(System.in);
        Main main = new Main();
        int number = console.nextInt();
        System.out.println(main.palindromeNumber(number));
    }

    // StringBuilder逆置
    public Boolean palindromeNumber(int number) {
    
    
        //write your code here......
        StringBuffer sb = new StringBuffer(number+"").reverse();
        if(sb.toString().equals(number+"")) {
    
    
            return true;
        }
        return false;
    }
    
    // 1、循环
    public Boolean palindromeNumber1(int number) {
    
    
        //write your code here......
        int tmp = 0;
        int numberCopy = number;
        while(number != 0) {
    
    
            tmp = tmp*10 + number%10;
            number/=10;
        }
        if(tmp == numberCopy) {
    
    
            return true;
        }
        return false;
    }
}

Java Character 类

在这里插入图片描述

JAVA45 判断各类型字符个数
输入:
!@#¥% asdyuihj 345678
输出:
英文字母8数字6空格2其他5

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        int numbers = 0;
        int words = 0;
        int space = 0;
        int other = 0;
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();

        //write your code here......
        for (int i = 0; i < str.length(); i++) {
    
    
            // 用char包装类中的判断字母的方法判断每一个字符
            if(Character.isDigit(str.charAt(i))) {
    
     // 判断数字
                numbers++;
            }else if(Character.isLetter(str.charAt(i))) {
    
     // 判断字母
                words++;
            }else if(Character.isWhitespace(str.charAt(i))) {
    
    
                space++;
            }else {
    
    
                other++;
            }
        }

        System.out.println("英文字母"+words+"数字"+numbers+"空格"+space+"其他"+other);
    }
}

JAVA35 输出某一年的各个月份的天数

描述
输入任意年份,输出该年份各月天数(请使用 Calendar 类中的方法)
示例1
输入:
2021
输出:
2021年1月:31天
2021年2月:28天
2021年3月:31天
2021年4月:30天
2021年5月:31天
2021年6月:30天
2021年7月:31天
2021年8月:31天
2021年9月:30天
2021年10月:31天
2021年11月:30天
2021年12月:31天

import java.util.Calendar;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner console = new Scanner(System.in);
        int year = console.nextInt();

        //write your code here......
        Calendar calendar = Calendar.getInstance();
        for (int month = 1; month <= 12; month++) {
    
    
            calendar.set(year, month, 0);
            System.out.println(year+"年"+month+"月:"+
                    calendar.get(calendar.DAY_OF_MONTH)+"天");
        }
    }
}

Java Number & Math 类

JAVA34 求绝对值,平方根,对数,正弦值
输入:
4
输出:
4.0
2.0
1.3862943611198906
-0.7568024953079282

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner=new Scanner(System.in);
        double num = scanner.nextDouble();

        //write your code here......
        System.out.println(Math.abs(num));
        System.out.println(Math.sqrt(num));
        System.out.println(Math.log(num));
        System.out.println(Math.sin(num));
    }
}

JAVA31 十进制数转二进制数

添加链接描述

描述
控制台随机输入一个十进制数,请你将它转换为二进制数并输出
输入描述:
控制台随机输入的十进制正整数
输出描述:
该十进制数转换的二进制数字
示例1
输入:
9
输出:
1001

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        //write your code here......
        StringBuffer sb = new StringBuffer();
        sb.append(num % 2);
        while((num/=2) > 0) {
    
    
            sb.insert(0, num%2); // 
        }
        System.out.println(sb);
    }
    
    public static void main2(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        //write your code here......
        System.out.println(Integer.toBinaryString(num));
    }
    
    public static void main1(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        //write your code here......
        ArrayList<Integer> list = new ArrayList();
        if(num == 0) {
    
    
            System.out.println(num);
        }else {
    
    
            while (num > 0) {
    
    
                list.add(num % 2);
                num /= 2;
            }
            ListIterator<Integer> it = list.listIterator(list.size());
            while (it.hasPrevious()) {
    
    
                System.out.print(it.previous());
            }
        }
    }
}

JAVA30 统计字符串中字母出现次数

添加链接描述

描述
给定一个字符串,随机输入一个字母,判断该字母在这个字符串中出现的次数
输入描述:
任意一个字母
输出描述:
字母在字符串中出现次数
示例1
输入:
o
输出:
3
示例2
输入:
a
输出:
0

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

public class Main {
    
    
    public static void main(String[] args) {
    
    
        String string = "H e l l o ! n o w c o d e r";
        Scanner scanner= new Scanner(System.in);
        String word = scanner.next();
        scanner.close();
        System.out.println(check(string, word));
    }

    public static int check(String str, String word) {
    
    
        //write your code here......
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < str.length(); i++) {
    
    
            list.add(str.charAt(i));
        }
        return Collections.frequency(list, word.charAt(0));
    }
    
    public static int check2(String str, String word) {
    
    
        //write your code here......
        int cnt = 0;
        for (int i = 0; i < str.length(); i++) {
    
    
            if(str.charAt(i) == word.charAt(0)) {
    
    
                cnt++;
            }
        }
        return cnt;
    }
    
    public static int check1(String str, String word) {
    
    
        //write your code here......
        int cnt = 0;
        for(int i = 0; i < str.length(); i++) {
    
    
            char ch = str.charAt(i);
            String s = new String(ch+"");
            if(s.equals(word)) {
    
    
                cnt++;
            }
        }
        return cnt;
    }
}

JAVA29 动态字符串

添加链接描述
描述
将一个由英文字母组成的字符串转换成从末尾开始每三个字母用逗号分隔的形式。
输入描述:
一个字符串
输出描述:
修改后的字符串
示例1
输入:
hellonowcoder
输出:
h,ell,ono,wco,der

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

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();

        //write your code here......
        StringBuffer sb = new StringBuffer();
        int cnt = 0;
        for (int i = str.length() - 1; i >= 0; i--) {
    
    
            sb.append(str.charAt(i));
            cnt++;
            if(cnt % 3 == 0 && i != 0)  {
    
    
                sb.append(",");
            }
        }
        System.out.println(sb.reverse());
    }
    
    public static void main1(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();

        //write your code here......
        StringBuffer sb = new StringBuffer(str);
        for (int i = str.length() - 3; i >= 0; i -= 3) {
    
    
            sb.insert(i, ",");
        }
        System.out.println(sb);
    }
}

1、每组数据一行,按字符串原有的字符顺序,输出字符集合,即重复出现并靠后的字母不输出

牛客链接

例:输入:abcqweracb
输出:abcqwer

  • 使用StringBuilder,把出现过的字符append拼接
  • 数组,对应下标,没有出现的为0,出现过置为1
import java.util.Scanner;

public class Main {
    
    
    public static String func1(String str) {
    
    
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < str.length(); i++) {
    
    
            char ch = str.charAt(i);
            if(!sb.toString().contains(ch+"")) {
    
    
                sb.append(ch);
            }
        }
        return sb.toString();
    }
    
    public static String func(String str) {
    
    
        StringBuilder sb = new StringBuilder();
        int[] array = new int[127];
        // int[] array = new int[58];
        for(int i = 0; i < str.length(); i++) {
    
    
            char ch = str.charAt(i);
            if(array[ch] == 0) {
    
    
            // if(array[ch-65] == 0) {
    
    
                sb.append(ch);
                array[ch] = 1;
                // array[ch-65] = 1;
            }
        }
        return sb.toString();
    }
    
    public static void main(String[] args) {
    
    
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNextLine()) {
    
    
            String str = scanner.nextLine();
            String ret = func(str);
            System.out.println(ret);
        }
    }
}

JAVA5 计算商场折扣

牛牛商场促销活动:

满100全额打9折;
满500全额打8折;

满2000全额打7折;

满5000全额打6折;
且商场有抹零活动,不足一元的部分不需要付款(类型强制转换)
牛大姨算不清楚自己应该付多少钱,请你帮忙算一下

输入描述:
牛大姨账单钱数(int类型)
输出描述:
参加活动后,牛大姨应付钱数(int类型)
示例1
输入:
654
输出:
523

import java.util.*;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner console = new Scanner(System.in);
        int price = console.nextInt();
        int cost = 0;

        //write your code here......
        double discount = 0.0;
        if(price < 100) {
    
    
            discount = 1.0;
        }else if(price < 500) {
    
    
            discount = 0.9;
        }else if(price < 2000) {
    
    
            discount = 0.8;
        }else if(price < 5000) {
    
    
            discount = 0.7;
        }else if(price >= 5000) {
    
    
            discount = 0.6;
        }

        if(discount != 0.0) {
    
    
            cost = (int)(price*discount);
        }else {
    
    
            cost = price;
        }
        
        System.out.println(cost);
    }
}

JAVA8 邮箱验证

描述
请根据给出的正则表达式来验证邮箱格式是否合法,如果用户输入的格式合法则输出「邮箱格式合法」,否则输出「邮箱格式不合法」。
输入描述:
任意字符串
输出描述:
根据输入的邮箱格式判断其合法于不合法,若输入字符串符合邮箱格式则输出邮箱格式合法,否则输出邮箱格式不合法
示例1
输入:
[email protected]
输出:
邮箱格式合法
示例2
输入:
123123
输出:
邮箱格式不合法

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    

        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        String emailMatcher="[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+";

        //write your code here......
        if(str.matches(emailMatcher)) {
    
    
            System.out.println("邮箱格式合法");
        }else {
    
    
            System.out.println("邮箱格式不合法");
        }
    }
}

JAVA12 小球走过路程计算

添加链接描述

描述
一球从h米高度自由落下,每次落地后反弹回原高度的一半再落下,求它在第n次落地时共经过了多少米?第n次反弹多高?
输入描述:
输入小球的初始高度和落地的次数(先输入小球初始高度再输入反弹次数)
输出描述:
输出小球反弹的高度和经过的距离(先输出反弹的高度再输出经过的距离,中间用空格隔开)
示例1
输入:
100 3
输出:
12.500 250.000

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner scanner=new Scanner(System.in);
        float h=scanner.nextFloat();
        int n =scanner.nextInt();

        //write your code here......
        double sum = 0.0;
        while(n != 0) {
    
    
            sum += h; // 加球下落h
            h /= 2;
            if(n != 1) {
    
     // 少一次
                sum += h; // 加球反弹h
            }
            n--;
        }
        System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum));

        //输出格式为:System.out.println(String.format("%.3f", h)+" "+String.format("%.3f", sum));
    }
}

JAVA17 数组倒转

添加链接描述
描述
输入6个整数,先将其输出然后再将数组倒转,再次输出数组
输入描述:
用户随机输入的6个int类型变量
输出描述:
先输出用户输入顺序的数组(这一步预设代码已给出)
再输出反转后的数组(输出格式为Arrays.toString(arr),参考预设代码中的输出格式)
示例1
输入:
52 10 37 40 60 83
输出:
[52, 10, 37, 40, 60, 83]
[83, 60, 40, 37, 10, 52]

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = new int[6];

        // ArrayList<Integer>
        ArrayList<Integer> list = new ArrayList<>();

        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
    
    
            arr[i] = scanner.nextInt();
            list.add(arr[i]);
        }
        System.out.println(Arrays.toString(arr));

        // Collections逆置
        Collections.reverse(list);
        System.out.println(list);
    }
    
    public static void main1(String[] args) {
    
    
        int[] arr = new int[6];

        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
    
    
            arr[i] = scanner.nextInt();
        }
        System.out.println(Arrays.toString(arr));

        for (int i = 0; i < arr.length / 2; i++) {
    
     // 前一半 与 后一半 交换
            int tmp = arr[i];
            arr[i] = arr[arr.length-1-i];
            arr[arr.length-1-i] = tmp;
        }
        System.out.println(Arrays.toString(arr));
    }
}

JAVA23 定义打印方法

添加链接描述
描述
已知有三个类:First、Second、Third。要求定义一个打印方法,支持传入任意引用类型的参数(包括上述三个类),并在方法内部打印出对象的字符串形式。
输入描述:
类名
输出描述:
由该类实例化出来的对象的字符串表示
示例1
输入:
First
输出:
First

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) throws Exception {
    
    
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
    
    
            String className = scanner.next();
            // print就是需要你定义的方法
            print(Class.forName(className).newInstance());
        }
    }

    //write your code here......
    public static void print(Object obj) {
    
    
        System.out.println(obj.getClass().getName());
    }
}

class First {
    
    
    public String toString() {
    
    
        return "First";
    }
}

class Second {
    
    
    public String toString() {
    
    
        return "Second";
    }
}

class Third {
    
    
    public String toString() {
    
    
        return "Third";
    }
}

JAVA36 日期换算

添加链接描述

描述
已知,纽约时间比北京时间慢12小时,请根据用户输入的北京时间输出相应的纽约时间,若用户输入错误的月份或日期等信息则将其顺加。例如用户输入2021 13 32 14 43 54则生成北京时间为:2022-02-01 14:43:54 纽约时间为:2022-02-01 02:43:54
输入描述:
一组字符串,年,月,日,时,分,秒用空格隔开,如果用户数据输入不正常,应输出“您输入的数据不合理”
输出描述:
北京时间为:年-月-日 时:分:秒
纽约时间为:年-月-日 时:分:秒
示例1
输入:
2021 09 08 14 38 05
输出:
北京时间为:2021-09-08 14:38:05
纽约时间为:2021-09-08 02:38:05

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) throws ParseException {
    
    
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Scanner in = new Scanner(System.in);
        String str1 = in.nextLine();

        //write your code here......
        String[] arr = str1.split(" ");
        if(arr.length != 6) {
    
    
            System.out.println("您输入的数据不合理");
        } else {
    
    
            String str = arr[0]+"-"+arr[1]+"-"+arr[2]+" "+arr[3]+":"+arr[4]+":"+arr[5];
            Date date = sdf.parse(str);
            System.out.println("北京时间为:" + sdf.format(date.getTime()));
            System.out.println("纽约时间为:" + sdf.format(date.getTime() - (long)(12*60*60*1000)));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_56884023/article/details/121657354