欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

207研究所编程题目

程序员文章站 2022-03-23 22:47:26
207研究所编程题目数组旋转(按列打印)package org.agic.com207_2;/** * @author lipeng * @date 2020-11-06 22:01 * @file_version 1.0 * @description 功能描述 */public class Solution_1_matrixRotate { public static void main(String[] args) { int[][] arr = {...

207研究所编程题目

  1. 数组旋转(按列打印)
package org.agic.com207_2;
public class Solution_1_matrixRotate {
    public static void main(String[] args) {
        int[][] arr = {
                {1, 2, 3},
                {4, 5, 6}
        };
        rotate(arr, 2, 3);
        
    }
    
    public static int[] rotate(int[][] arr, int rows, int cols) {
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
                System.out.print(arr[j][i] + " ");
            }
            System.out.println();
        }
        return null;
    }
}

  1. 16进制的字符串转换为10进制字符串(不得调用本身的库,但是可以调用字符串处理工具)
package org.agic.com207_2;

import java.util.HashMap;
import java.util.Map;
public class Solution_2_hex2Dec {
    public static void main(String[] args) {
        System.out.println(hex2Dec("0x21")); // 33
        System.out.println(hex2Dec("10")); // 16
        System.out.println(hex2Dec("x1")); // -1
    }
    
    private static Map<Character, Integer> map = new HashMap() {
        {
            put('0', 0);
            put('1', 1);
            put('2', 2);
            put('3', 3);
            put('4', 4);
            put('5', 5);
            put('6', 6);
            put('7', 7);
            put('8', 8);
            put('9', 9);
            put('a', 10);
            put('A', 10);
            put('b', 11);
            put('B', 11);
            put('c', 12);
            put('C', 12);
            put('d', 13);
            put('D', 13);
            put('e', 14);
            put('E', 14);
            put('f', 15);
            put('F', 15);
        }
    };
    
    public static String hex2Dec(String hex) {
        try {
            String hex2 = hex.toUpperCase().replaceAll("0X", "");
            final int baseVal = 16;
            int length = hex2.length();
            int dec = 0;
            for (int i = length - 1; i >= 0; i--) {
                int power = length - i - 1;
                int x = map.get(hex2.charAt(i)).intValue() * (int) Math.pow(baseVal, power);
                dec += x;
            }
            return String.valueOf(dec);
        } catch (Exception e) {
            return "error input:" + hex;
        }
    }
}

  1. 找出重复的数字,按照大小排列(方法很多)
package org.agic.com207_2;

import java.util.*;
public class Solution_3_repeatNumbers {
    public static void main(String[] args) {
        System.out.println(deleteRepeatNumbers(new int[]{1, 4, 6, 7, 2, 3, 4, 2, 3, 4}));
    }
    
    
    public static List<Integer> deleteRepeatNumbers(int[] arr) {
        Map<String, Integer> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            String key = String.valueOf(arr[i]);
            if (map.get(key) == null) {
                map.put(key, 1);
            } else {
                map.put(key, map.get(key).intValue() + 1);
            }
        }
        for (String key : map.keySet()) {
            if (map.get(key).intValue() > 1) {
                list.add(Integer.parseInt(key));
            }
        }
        Collections.sort(list);
        return list;
    }
}

  1. 给定日期,获取该日期是当年的第几天
package org.agic.com207_2;
public class Solution_4_dayOfYear {
    public static void main(String[] args) {
        System.out.println(calcDayOfYear(2020, 3, 28));
    }
    
    private static boolean isLeap(int year) {
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
    
    public static int calcDayOfYear(int yyyy, int MM, int dd) {
        int[] months = {0, 31, isLeap(yyyy) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        int days = dd;
        for (int i = 1; i < MM; i++) {
            days += months[i];
        }
        return days;
    }
}

  1. 长度为length的绳子拆成n段,n1+n2+n3.+… =length; n1 等均为整数,求n1n2n3… = max,求max,并且输出 n1,n2,n3…
package org.agic.com207_2;
public class Solution_5_maxMutil {
    public static void main(String[] args) {
        System.out.println(maxMutil(8, 3));
    }
    
    public static int maxMutil(int x, int n) {
        // 2    minLength: n - yLength
        // 3 3  yLength:2
        int minLength = x / n, yLength = x % n;
        return (int) (Math.pow(minLength, n - yLength)) * (int) (Math.pow(minLength + 1, yLength));
    }
}

题目难度 一星

本文地址:https://blog.csdn.net/xiaoweitianshi001/article/details/109612236

相关标签: java 算法