码迷,mamicode.com
首页 > 其他好文 > 详细

笔试:输入输出

时间:2021-04-06 14:10:55      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:substring   str   约数   new   efault   uil   ++   数据结构   vat   

█ 0.代码函数:

○ 0.1 边界

Integer.MAX_VALUE
int[] preorder  ==>   preorder == null || preorder.length == 0
char[][] board  ==>   board == null
int[][] matrix  ==>   (matrix == null || matrix.length == 0) || (matrix.length == 1 && matrix[0].length == 0)
String s        ==>   s == null || s.length() == 0

○ 0.2 牛客网 数据结构转换

1)char[] str 转 String:

        StringBuilder word = new StringBuilder();
        for(int i = 0; i < str.length; i++){
            word.append(str[i]);
        }

2)String 转 char[]

char[] str = s.trim().toCharArray();

3)一维数组(char[] , int r, int c) 转 二维数组 char[][]

        char[][] board = new char[rows][cols];
        int index = 0;
        int x = 0;
        for(int i = 0 ; i < matrix.length; i++){
            if(index >= cols){
                index = 0;
                x++;
            }
            board[x][index] == matrix[i];
            index++;
        }

4)StringBuilder 转 String

res.toString().trim();

○ 0.3 函数名称:

Character.isDigit()

isEmpty()  -> String ; Stack 

String---> charAt(0) ; subString(2) 起点; subString(1,3); .length()  ; compareTo()
string.toCharArray();
Integer.parseInt(string)
linkedList.toArray(new String[list.size()])

string[i] = String.valueOf(int[i]);  str[i].equals("")

StringBuilder  append("")   string=stringbuilder.toString().trim();



Map<Character, Integer> dic = new HashMap<>();  dic.containsKey(s.charAt(j)) ; dic.get(s.charAt(j)) ;   dic.put(s.charAt(j), j); 

        Map<Integer, Integer> map = new HashMap<>();

        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            if(entry.getValue() == 1){
                return entry.getKey();
            }
        }

[]preorder  -->  Arrays.copyOfRange(preorder, 1, index+1)  ;  Arrays.sort(nums);  for(int num:nums)
ArrayList   -->  add() get(i)  .add(0, node.val); //从头部插入  ;  remove(index) ; 
List<int[]> res = new ArrayList<>();  res.add(arr一维);  res.toArray(new int[res.size()][]);

LinkedList<List<Integer>> res = new LinkedList<>();
LinkedList<Integer> path = new LinkedList<>();   res.add(new LinkedList(path));



Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
Queue<TreeNode> queue = new LinkedList<>(); 
queue.remove(); add(xx);


// 最小堆   Queue<Integer> queue = new PriorityQueue<>( (o1,o2) -> (o2.compareTo(o1)));   poll()  add()  size()




Deque<Integer> deque = new LinkedList<>();
deque.peekFirst(); .removeFirst(); .peekLast(); .removeLast();
queue.add(value); deque.removeLast(); deque.add();
queue.offer(value); deque.pollLast(); deque.offerLast();

stack push() pop() isEmpty()
HashMap get() put()

HashSet<Character> set = new HashSet<>(); set.contains() add()




# █ 1.普通示例:
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args){
        Scanner in=new Scanner(System.in);
        while(in.hasNext()){
            int n=in.nextInt();
            int m=in.nextInt();
            int[][] arr=new int[m][3];
            for (int i=0;i<m;i++){
                for (int j=0;j<3;j++){
                    arr[i][j]=in.nextInt();
                }
            }
            int res=number(n,arr);
            System.out.println(res);
        }
    }
	
	
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Main a = new Main();
        Scanner scanner = new Scanner(System.in);
        ArrayList<Integer> ret = new ArrayList<Integer>();
        int n,m;
        while (scanner.hasNextInt()){
            n=scanner.nextInt();
            m=scanner.nextInt();
            int[][] relationShip=new int[m][3];
            for (int i = 0; i < m; i++) {
                relationShip[i][0]=scanner.nextInt();
                relationShip[i][1]=scanner.nextInt();
                relationShip[i][2]=scanner.nextInt();
            }
        	System.out.println(a.getResult(n,m,relationShip));
        }

    }

█ 2.Hashmap 字符串的问题

scanner.hasNext(); String s = scanner.next(); sc.nextInt(); scanner.hasNextInt()

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext())
        {
            ArrayList<Integer> price = new ArrayList<>();
            int goodsNum = 0, buyNum = 0;
            HashMap<String, Integer> buyList = new HashMap<>();
            goodsNum = sc.nextInt();
            buyNum = sc.nextInt();
            for (int i = 0; i < goodsNum; i++)
            {
                price.add(sc.nextInt());
            }
            for (int i = 0; i < buyNum; i++)
            {
                String good = sc.next();
                if (buyList.containsKey(good))
                {
                    int count = buyList.get(good);
                    buyList.put(good, ++count);
                }
                else
                    buyList.put(good, 1);
            }
            int maxValue = 0;
            int minValue = 0;
            Collection collection = buyList.values();
            ArrayList<Integer> list = new ArrayList<>(collection);
            Collections.sort(list);
            Collections.sort(price);
            for (int i = 0; i < list.size(); i++)
            {
                minValue += list.get(list.size() - i - 1) * price.get(i);
                maxValue += list.get(list.size() - i - 1) * price.get(price.size() - i - 1);
            }
            System.out.printf(minValue + " " + maxValue + "\n");
        }
        sc.close();
    }

}

█ 3.路径规划 字符串拼接

scanner.hasNext(); String s = scanner.next();
String charAt 
StringBuffer step append
List<String> steps = new ArrayList<>(); steps.add(step.toString());

import java.util.*;
public class Main {

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

        while (scanner.hasNext()) {
            String s = scanner.next();
            String e = scanner.next();

            int diffLR = s.charAt(0) - e.charAt(0);
            int diffUD = s.charAt(1) - e.charAt(1);

            List<String> steps = new ArrayList<>();

            while (diffLR != 0 || diffUD != 0) {
                StringBuffer step = new StringBuffer("");
                if (diffLR < 0) {
                    step.append("R");
                    diffLR++;
                } else if (diffLR > 0) {
                    step.append("L");
                    diffLR--;
                }
                if (diffUD < 0) {
                    step.append("U");
                    diffUD++;
                } else if (diffUD > 0) {
                    step.append("D");
                    diffUD--;
                }
                steps.add(step.toString());
            }

            System.out.println(steps.size());
            for (String step : steps){
                System.out.println(step);
            }
        }

        scanner.close();
    }
}

█ 4.TreeNode getOrDefault Map.Entry


import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNext()) {
			int n = sc.nextInt();
			int s = sc.nextInt();
			TreeMap<Integer, Integer> buy = new TreeMap<>((a, b) -> b - a);
			TreeMap<Integer, Integer> sell = new TreeMap<>((a, b) -> b - a);
			for (int i = 0; i < n; ++i) {
				if ("B".equals(sc.next())) {
					int key = sc.nextInt();
					int value = sc.nextInt();
					buy.put(key, buy.getOrDefault(key, 0) + value);
				}else {
					int key = sc.nextInt();
					int value = sc.nextInt();
					sell.put(key, sell.getOrDefault(key, 0) + value);
				}
			}
			int cnt = 0;
			for(Map.Entry<Integer, Integer> entry: sell.entrySet()) {
				System.out.println("S " + entry.getKey() + " " + entry.getValue());
				++cnt;
				if (cnt == s) {
					break;
				}
			}
			cnt = 0;
			for (Map.Entry<Integer, Integer> entry: buy.entrySet()) {
				System.out.println("B " + entry.getKey() + " " + entry.getValue());
				++cnt;
				if (cnt == s) {
					break;
				}
			}
		}
        sc.close();
    }
}

█ 5.公约数 进制数和

import java.util.Scanner;

public class Main {
	public static int getGongYueShu(int a, int b) {// 求两个数的最大公约数
		int t = 0;
		if (a < b) {
			t = a;
			a = b;
			b = t;
		}
		int c = a % b;
		if (c == 0) {
			return b;
		} else {
			return getGongYueShu(b, c);
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while (sc.hasNextInt()) {
			int num = sc.nextInt();
			int sum = 0;
			for (int i = 2; i < num; i++) {
				int sum_ = getSum(num, i);// 将num转换成指定进制数然后各个位求和
				sum += sum_;
			}
			int gongYueShu = getGongYueShu(sum, num - 2);
			System.out.println(sum / gongYueShu + "/" + (num - 2) / gongYueShu);
		}
        sc.close();
	}

	private static int getSum(int num, int i) {
		int sum = 0;
		while (num != 0) {
			int wei = num % i;
			sum += wei;
			num = num / i;
		}
		return sum;
	}
}

笔试:输入输出

标签:substring   str   约数   new   efault   uil   ++   数据结构   vat   

原文地址:https://www.cnblogs.com/ming-michelle/p/14615095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!