zoukankan      html  css  js  c++  java
  • 刷题基础知识

    Map的使用:
     1 public class Main
     2 {
     3     public long factorial(int n){
     4         if(n<=1)
     5             return 1;
     6         return n * factorial(n-1);
     7     }
     8 
     9     public long func(String line){
    10         HashMap<Character, Integer> map  = new HashMap<Character, Integer>();
    11         for(int i=0;i<line.length();i++){
    12             Character c = line.charAt(i);
    13             if(map.containsKey(c))
    14                 map.put(c, map.get(c)+1);    //修改Map的元素值
    15             else
    16                 map.put(c, 1);
    17         }
    18         //访问Map
    19         long tmp = 1;
    20         for(Character key : map.keySet()){
    21             int val = map.get(key);
    22             tmp *= factorial(val);
    23         }
    24 
    25         return factorial(line.length()) / tmp;
    26     }
    27 
    28 
    29     public static void main(String[] args)
    30     {
    31         Main ob = new Main();
    32         Scanner scan = new Scanner(System.in);
    33         while(scan.hasNextLine()){
    34             System.out.println(ob.func(scan.nextLine()));
    35         }
    36     }
    37 }
    匹配一个或多个空格:scan.nextLine().split("\s+");
    大部分内置类均位于java.lang.*、java.util.*、java.io.*包中
     
    格式化输出的方法:
     
    1 System.out.println("x = " + x + ", y = " + y);
    2 // printf()方式
    3 System.out.printf("x = %d, y = %f
    ", x, y);
    4 // format()方式
    5 System.out.format("x = %d, y = %f
    ", x, y);
    scan有直接返回大数的方法,无需通过String创建大数。
     
     
    当内存超了,标志数组可以考虑用BitSet:
    1 BitSet arr = new BitSet(n);
    2 arr.set(0, n, true);
    3 arr.get(0)
    在处理string和int交叉输入时,最好统一用nextLine()获取行,利用string.split('\s+')划分,然后转成int
     
     1 import java.util.Arrays;
     2 import java.util.Comparator;
     3 import java.util.Scanner;
     4 
     5 public class Main
     6 {
     7     public void func(Score[] countryList){
     8         Arrays.sort(countryList, new MyCompare());
     9         //从大到小打印
    10         for(int i=countryList.length-1;i>=0;i--)
    11             System.out.println(countryList[i].contouryName);
    12     }
    13 
    14     public static void main(String[] args)
    15     {
    16         Main obj = new Main();
    17         Scanner scan = new Scanner(System.in);
    18         while(scan.hasNextLine()){
    19             int num = Integer.parseInt(scan.nextLine());
    20             Score[] countryList = new Score[num];
    21             for(int i=0;i<num;i++){
    22                 String line = scan.nextLine();
    23                 String[] strList = line.split("\s+");
    24                 Score country = new Score(strList[0], Integer.parseInt(strList[1]),
    25                         Integer.parseInt(strList[2]), Integer.parseInt(strList[3]));
    26                 countryList[i] = country;
    27             }
    28             obj.func(countryList);
    29         }
    30     }
    31 }
    32 
    33 class Score{
    34     public String contouryName;
    35     public int goldScore;
    36     public int sliverScore;
    37     public int bronzeScore;
    38 
    39     Score(){
    40         this.contouryName = "";
    41         this.goldScore = 0;
    42         this.sliverScore = 0;
    43         this.bronzeScore = 0;
    44     }
    45 
    46     Score(String contouryName, int goldScore, int sliverScore, int bronzeScore){
    47         this.contouryName = contouryName;
    48         this.goldScore = goldScore;
    49         this.sliverScore = sliverScore;
    50         this.bronzeScore = bronzeScore;
    51     }
    52 }
    53 
    54 class MyCompare implements Comparator<Score>{
    55     @Override
    56     public int compare(Score a, Score b){
    57         if(a.goldScore > b.goldScore)
    58             return 1;
    59         else if(a.goldScore == b.goldScore){
    60             if(a.sliverScore > b.sliverScore)
    61                 return 1;
    62             else if(a.sliverScore == b.sliverScore){
    63                 if(a.bronzeScore > b.bronzeScore)
    64                     return 1;
    65                 else if(a.bronzeScore == b.bronzeScore)
    66                     return a.contouryName.compareTo(b.contouryName);
    67             }
    68         }
    69         return -1;
    70     }
    71 }
    java中的栈类:
    1 import java.util.Stack;
    求最大公约数:辗转相除法。
     
    表达式的计算:
    建立两个栈:一个a存数字,一个b存运算符。
    从左向右遍历,若为数字直接入a,若为运算符与b栈顶比较:若优先级大则入栈b;否则
     
    对List排序:
    1 import java.util.Collections;
    2 Collections.sort(list);
     
    注意string.replaceAll才支持正则表达式,string.replace不支持.
    1  String strM = mStr.replaceAll("\s+", "").toLowerCase();
    去除string开头结尾的空格和换行符: 
    1 str.trim()
    将int转成BigInterger:
    1 BigInteger bi = BigInteger.valueOf(myInteger.intValue());
    对于高精度的double小数或大数,直接利用toString()转成String,或直接打印,均会以科学计数法输出。解决办法:
    1 BigDecimal bigA = new BigDecimal(a);
    2 BigDecimal res = bigA.pow(b);
    3 String sRes = res.stripTrailingZeros().toPlainString();    //去除末尾无效0
    char转int不一定要强制转换,但是int转char要强制转换。
     
    判断数组中是否存在重复元素:
     1 public static boolean hasRepeat(String[] pInStr){
     2     HashSet<String> hashSet = new HashSet<>();
     3     for(int i=0;i<pInStr.length;i++){
     4         hashSet.add(pInStr[i]);
     5     }
     6     if(hashSet.size() == pInStr.length){
     7         return false;
     8     } else{
     9         return true;
    10     }
    11 }
    判断数组中是否包含某个元素:
    1 Arrays.asList(pInStr).contains(FirstState)
    对ArrayList动态数组排序:
     1 ArrayList<String> leftList =  new ArrayList<>();
     2 ArrayList<String> rightList =  new ArrayList<>();
     3 for(int i=0;i<pInStr.length;i++){
     4     if(FirstState.compareTo(pInStr[i])<=0){
     5         leftList.add(pInStr[i]);
     6     } else{
     7         rightList.add(pInStr[i]);
     8     }
     9 }
    10 Collections.sort(leftList);
    11 Collections.sort(rightList);
    对String[]排序:
    Arrays.sort(strList); //无返回值
  • 相关阅读:
    POJ 1330:Nearest Common Ancestors【lca】
    图论中一类问题的总结 :必须边(点) 可行边(点)
    POJ 1486 Sorting Slides【二分图匹配】
    POJ 2375 Cow Ski Area【tarjan】
    Unity打开AppStore进行评论
    Unity3D UGUI不规则图片点击事件处理
    Unity3D之聊天框怎么跟随内容大小而变换
    Unity3D之小物体层消隐技术
    Unity3D之新手引导(责任链模式)
    Unity3D之FSM有限状态机
  • 原文地址:https://www.cnblogs.com/luckyboylch/p/12327316.html
Copyright © 2011-2022 走看看