1.36进制转10进制(这题我没判断大小试着调试一下直接给A了…)
import java.util.Scanner; public class Right1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String word = sc.next(); int length = word.length(); long result = 0; if(length==0){ System.out.println(result); }else if(length==1){ if((int)word.charAt(0)>=48&&(int)word.charAt(0)<=57){ result = (int)word.charAt(0)-48; }else if((int)word.charAt(0)>=97&&(int)word.charAt(0)<=122){ result = (int)word.charAt(0)-87; }else{ result = 0; } System.out.println(result); }else { for(int i =1;i<word.length();i++){ if((int)word.charAt(i)>=48&&(int)word.charAt(i)<=57){ result += Math.pow(36,word.length()-i-1)*((int)word.charAt(i)-48); }else if((int)word.charAt(i)>=97&&(int)word.charAt(i)<=122){ result += Math.pow(36,word.length()-i-1)*((int)word.charAt(i)-87); }else{ result = 0; break; } } if((int)word.charAt(0)==45){ result*=-1; } else if((int)word.charAt(0)>=48&&(int)word.charAt(0)<=57){ result += Math.pow(36,word.length()-1)*((int)word.charAt(0)-48); }else if((int)word.charAt(0)>=97&&(int)word.charAt(0)<=122){ result += Math.pow(36,word.length()-1)*((int)word.charAt(0)-87); }else{ result = 0; } System.out.println(result); } } }2.有序数组最多构建多少个二叉搜索树(动归,不同节点做根节点的情况下左子树组合数量*右子树组合数量)
import java.util.Scanner; public class Right2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if(n==0){ System.out.println(0); }else{ int [] array = new int [n+1]; array[0] = 1; for(int i =1;i<array.length;i++){ for(int j = 1;j<=i;j++){ array[i]+=array[j-1]*array[i-j]; } } System.out.println(array[n]); } } }
全部评论
(1) 回帖