第一道题:说给N个不同的字符,求长度为L的时候有多少种方法。当总数大于1000000007的时候,需要求模。
举例 当N=2(假设不同字符为a,b)长度为3(a,b,aa,ab,bb,ba,aaa,aab,aba,abb,bbb,bba,bab,baa)=14
大概就是求(A N1 )得一次方到L次方的和。
楼主先用了Math.pow() 过了27% 发现溢出
然后用了 BigDecimal 过了34%
后面使用了 快速幂全AC
bigcmals
第二道题
说给一个字符串,对字符串进行处理和处理后每行的长度L。
每行的尽可能包含多的单词,单词之间或者尾部不足要用“*”补充,要是一样有两个或者两个单词以上,要求最左边的单词和最后边单词对齐,一行的单词之间的“*”要尽可能均分布,如果不能的话,左边可以比右边多1个“*”,最后一行的单词之间只要一个"*"
说明: 就是一行字符串包含多个单词,你需要把单词使用*分开,单词尽可能分布在一行,但是不能完整单词不能换行,每行单词不能超过L。
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s = scanner.nextLine(); String[] str = s.split(","); int n = str.length; int l = Integer.parseInt(scanner.nextLine()); int begin = 0; int end = 0; int preSum = 0; while (end < n){ StringBuilder sb = new StringBuilder(); preSum = 0; while(end <n && preSum + end -begin + str[end].length()<l){ preSum += str[end].length(); end++; } if(end ==n){ break; } System.out.println(reBuildStr(str,begin,end,l,preSum)); begin = end; } System.out.println(mergeEnd(str,begin,l)); } private static String mergeEnd(String[] str, int begin, int l) { StringBuilder res = new StringBuilder(); for (int i = begin; i < str.length; i++) { res.append(str[i]); res.append("*"); } if (res.length()>l) { res.deleteCharAt(res.length()-1); }else{ while (res.length()<l) { res.append("*"); } } return res.toString(); } private static String reBuildStr(String[] str, int begin,int end,int l,int preSum) { StringBuilder sb = new StringBuilder(); int curSum = end - begin; if(curSum == 1){ sb.append(str[end-1]); for (int i = 0; i < l -str[begin].length();i++) { sb.append("*"); } return sb.toString(); } int avg = (l-preSum) / (curSum -1); int remain = (l-preSum) % (curSum -1); for(int i = begin;i<begin +remain;i++){ sb.append(str[i]); for(int j = 0;j<avg +1;j++){ sb.append("*"); } } for(int i = begin + remain;i< end -1;i++){ sb.append(str[i]); for (int j = 0; j < avg;j++) { sb.append("*"); } } sb.append(str[end-1]); return sb.toString(); } }
第三题:
输入学生信息 包括姓名,语文,数学,英语成绩。
要求:
1.将每门课成绩都合格的输出。
2.按照总分、语文、数学、英语排序输出。(先看总分 ,总分想等看语文成绩、一依次类推)
3.输出录取名单。只要前三名,如果某一名有多位,都输出。
4.成绩相同的,名字按照ASCII码排序从小到大输出。
5.名字不会有重复的,所有成绩都合法。
public class Main3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); LinkedList<Student> list = new LinkedList<>(); LinkedList<Student> no = new LinkedList<>(); int index = 1; while (scanner.hasNext() && index <=10){ String[] split = scanner.nextLine().split(" "); index++; int chin = Integer.parseInt(split[1]); int math = Integer.parseInt(split[2]); int english = Integer.parseInt(split[3]); int score = chin + math + english; Student s = new Student(split[0], chin,math ,english ,score); if(chin>=60 && math>=60 && english>=60){ list.add(s); }else { no.add(s); } } System.out.println(); Comparator comparator = (o1, o2) -> { if(o1 instanceof Student && o2 instanceof Student){ Student s1 = (Student) o1; Student s2 = (Student) o2; int k = s1.score.compareTo(s2.score); if (k==0) { int j = s1.chinese.compareTo(s2.chinese); if (j ==0) { int i = s1.math.compareTo(s2.math); if(i==0){ int m = s1.english.compareTo(s2.english); if(m==0){ int n = s1.name.toLowerCase().compareTo(s2.name.toLowerCase()); return n == 0 ? 0 :-(n>0 ? 1:-1); } return -m; } return -j; } } return -k; } return 0; }; TreeSet<Student> tree = new TreeSet(comparator); tree.addAll(list); System.out.println("[First round name list]"); for (Student o : tree) { System.out.println(o.name+" " +o.chinese+" "+o.math+" "+o.english); } System.out.println(); int time = 0; Student cur = null; System.out.println("[Find name list]"); for (Student o : tree) { time += 1; if(time ==1){ System.out.println(o.name+" " +o.chinese+" "+o.math+" "+o.english); }else if(time <= 3){ if(time == 2){ cur = o; } System.out.println(o.name+" " +o.chinese+" "+o.math+" "+o.english); }else{ if(o!=null && cur.score.equals(o.score) && cur.math.equals(o.math) && cur.chinese.equals(o.chinese) && cur.english.equals(o.english)){ System.out.println(o.name+" " +o.chinese+" "+o.math+" "+o.english); }else{ return; } } } } @Test public void test(){ System.out.println("simonyin".toLowerCase().compareTo("liutao")); } static class Student { public String name; public Integer chinese; public Integer math; public Integer english; public Integer score; public Student(String name, Integer chinese, Integer math, Integer english, Integer score) { this.name = name; this.chinese = chinese; this.math = math; this.english = english; this.score = score; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Student student = (Student) o; return chinese == student.chinese && math == student.math && english == student.english && score == student.score && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, chinese, math, english, score); } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", chinese=" + chinese + ", math=" + math + ", english=" + english + ", score=" + score + '}'; } }
全部评论
(1) 回帖