解题思路:
给组测试数据,过了应该就问题不大了.
注意事项:
参考代码:
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语言程序设计教程(第三版)课后习题7.4 (C语言代码)浏览:566 |
C二级辅导-统计字符 (C语言代码)浏览:506 |
字符串问题 (C语言代码)浏览:1521 |
简单的a+b (C语言代码)浏览:694 |
2005年春浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:591 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:641 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:787 |
P1000 (C语言代码)浏览:877 |
A+B for Input-Output Practice (VII) (C语言代码)浏览:528 |
模拟计算器 (C语言代码)浏览:2300 |