解题思路:
给组测试数据,过了应该就问题不大了.
注意事项:
参考代码:
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class T1448 { public static void Insert(int []tree,int n,int i) { if(tree[i] == 0) {tree[i]=n;return;} if(n>tree[i]) Insert(tree,n,2*i+1); else Insert(tree,n,2*i); } public static void main(String[] args) { Scanner in = new Scanner(System.in); final int N = 500; while(in.hasNext()) { String str = in.nextLine(); ArrayList<Integer> list = new ArrayList<Integer>(); int tree[] = new int[N]; Scanner sc = new Scanner(str); while(sc.hasNext()) { int t = sc.nextInt(); list.add(t); Insert(tree,t,1); } Collections.sort(list); tree[0]=tree[1]; String []ans = new String[list.size()+1]; ArrayList<Integer> queue = new ArrayList<Integer>(); queue.add(1); while(queue.size()!=0) { int value = queue.get(0),index = list.indexOf(tree[value])+1; if(value==1) ans[index] = tree[value]+""; else { String t = ans[list.indexOf(tree[value/2])+1]; t = t.replaceAll("\\d|-",".").substring(0,t.length()-1); if(tree[value]<tree[value/2]&&tree[value/2]<=tree[value/4] || tree[value]>tree[value/2]&&tree[value/2]>tree[value/4]) { StringBuilder sb = new StringBuilder(t); for(int k = t.length()-1;k>=0;k--) if(sb.charAt(k)=='|') {sb.setCharAt(k, '.');break;} t = sb.toString(); } ans[index]=t+"|-"+tree[value]; } int flag = 0; if(tree[2*value]!=0) { flag=1; queue.add(2*value); } if(tree[2*value+1]!=0) { flag=1; queue.add(2*value+1); } if(flag==1) ans[index]+="-|"; queue.remove(0); } for(int i = list.size();i>=1;i--) System.out.println(ans[i]); } in.close(); } }
0.0分
2 人评分
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:693 |
简单的a+b (C语言代码)浏览:641 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:552 |
【绝对值排序】 (C语言代码)浏览:892 |
1908题解浏览:680 |
1113题解浏览:823 |
A+B for Input-Output Practice (VI) (C语言代码)浏览:575 |
简单的a+b (C语言代码)浏览:574 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:567 |
C语言程序设计教程(第三版)课后习题3.7 (C语言代码)浏览:545 |