论坛首页 综合技术论坛

非递归遍历二叉树

浏览 11206 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (6)
作者 正文
   发表时间:2009-11-02  
楼主没点幽默细胞 - -!
0 请登录后投票
   发表时间:2009-11-02  
我也觉得挺乐的,不过如果不理你的话就太每礼貌了,毕竟你们还是很捧我的场。

高一就写程序了,后生可畏呀。我写程序的时候都大一了,嗨....

时代出人才呀..
0 请登录后投票
   发表时间:2009-11-02  
public class Test2 {
	public static void main(String[] args) throws Exception {
		int n = 4;
		// 上层
		for (int i = 1; i <= n; i++) {
			// 打印' '
			for (int j = 1; j <= (n*2)-i; j++) {
				System.out.print(" ");
			}
			// 打印'*'
			for (int j = 1; j <= i * 2 - 1; j++) {
				System.out.print("*");
			}
			// 换行
			System.out.println("");
		}
		
		// 下层
		for(int i = 1; i <= n; i++){
			// 左侧
			// 打印' '
			for(int j = 1; j<= n-i; j++){
				System.out.print(" ");
			}
			// 打印'*'
			for (int j = 1; j <= i * 2 - 1; j++) {
				System.out.print("*");
			}
			// 右侧
			// 打印' '
			for(int j = 1; j<= (n-i)*2+1; j++){
				System.out.print(" ");
			}
			// 打印'*'
			for (int j = 1; j <= i * 2 - 1; j++) {
				System.out.print("*");
			}
			// 换行
			System.out.println("");
		}
	}
}
0 请登录后投票
   发表时间:2009-11-02  
public class Star {
	public static void main(String[] args) {
		// 打印四星, 共四层
		int total = 4, starNum = 4;
		StringBuffer buf = new StringBuffer();
		for (int i = 1; i <= total; i++) {
			buf.append(layer(total, i, starNum));
		}
		System.out.println(buf);
	}

	/**
	 * @param totalLayer
	 *            总共几层
	 * @param layer
	 *            第几层
	 * @param starNum
	 *            一层有几星
	 * @return
	 */
	public static String layer(int totalLayer, int layer, int starNum) {
		StringBuffer buf = new StringBuffer();
		for (int i = 0; i < starNum; i++) {// 共starNum行
			// 每一行由layer*2个部分组成
			buf.append(blanks((totalLayer - layer) * starNum));
			for (int j = 0; j < layer * 2; j++) {
				if (j == 0) {
					buf.append(blanks(starNum - i - 1));
				} else if (j % 2 == 0) {
					buf.append(blanks(starNum * 2 - i * 2 - 1));
				} else {
					buf.append(stars(i * 2 + 1));
				}
			}
			buf.append('\n');
		}
		return buf.toString();
	}

	static String	BLANKS	= "                    ";
	static String	STARS	= "********************";

	public static String blanks(int num) {
		return BLANKS.substring(0, num);
	}

	public static String stars(int num) {
		return STARS.substring(0, num);
	}
}

0 请登录后投票
   发表时间:2009-11-02  
treblesoftware 写道

我高中考试高考QBASIC设计就是这样写的,满分。

PS:其实我从高一就已经这样写了。不过那个时候是:

print(*);
print(**);
print(***);
print(****);



握手 ,我初中学qb也是这个程序,后来奥赛突然变成寻路了,茶几了
0 请登录后投票
   发表时间:2009-11-02   最后修改:2009-11-02
分步骤解决:
第一步 :
public class StartTree {

	public static void main(String[] args) {
		show(5);
	}
	
	public static void show(int n){
		for(int j=1;j<=n;j++){
			for(int i=1;i<=j;i++){
				System.out.print("*");
				System.out.print(" ");
			}
			System.out.println();
		}
	}

}

效果:
*
* *
* * *
* * * *
* * * * *
第二步:
public class StartTree {

	public static void main(String[] args) {
		show(5,3);
	}
	
	public static void show(int n,int m){
		for(int j=1;j<=n;j++){
			for(int i=1;i<=j;i++){
				for(int k=1;k<=2*m-1;k++)System.out.print("*");
				System.out.print(" ");
			}
			System.out.println();
		}
	}

}

效果:
*****
***** *****
***** ***** *****
***** ***** ***** *****
***** ***** ***** ***** *****
第三步:
public class StartTree {

	public static void main(String[] args) {
		show(5,3);
	}
	
	public static void show(int n,int m){
		for(int j=1;j<=n;j++){
			for(int i=1;i<=m*(n-j);i++){
				System.out.print(" ");
			}
			for(int i=1;i<=j;i++){
				for(int k=1;k<=2*m-1;k++){
					System.out.print("*");
				}
				System.out.print(" ");
			}
			System.out.println();
		}
	}

}

效果:
            *****
         ***** *****
      ***** ***** *****
   ***** ***** ***** *****
***** ***** ***** ***** *****
第四步
public class StartTree {

	public static void main(String[] args) {
		show(5,3);
	}
	
	public static void show(int n,int m){
		for(int j=1;j<=n;j++){
			for(int h=1;h<=m;h++){
				for(int i=1;i<=m*(n-j);i++){
					System.out.print(" ");
				}
				for(int i=1;i<=j;i++){
					for(int k=1;k<=2*m-1;k++){
						System.out.print("*");
					}
					System.out.print(" ");
				}
				System.out.println();
			}
		}
	}

}

效果:
            *****
            *****
            *****
         ***** *****
         ***** *****
         ***** *****
      ***** ***** *****
      ***** ***** *****
      ***** ***** *****
   ***** ***** ***** *****
   ***** ***** ***** *****
   ***** ***** ***** *****
***** ***** ***** ***** *****
***** ***** ***** ***** *****
***** ***** ***** ***** *****
第五步:
public class StartTree {

	public static void main(String[] args) {
		show(5,3);
	}
	
	public static void show(int n,int m){
		for(int j=1;j<=n;j++){
			for(int h=1;h<=m;h++){
				for(int i=1;i<=m*(n-j);i++)	System.out.print(" ");
				for(int i=1;i<=j;i++){
					for(int k=1;k<=m-h;k++)System.out.print(" ");
					for(int k=1;k<=2*h-1;k++)System.out.print("*");
					for(int k=1;k<=m-h;k++)System.out.print(" ");
					System.out.print(" ");
				}
				System.out.println();
			}
		}
	}

}

效果:
                *  
             *** 
            *****
           *     *  
          ***   *** 
         ***** *****
        *     *     *  
       ***   ***   *** 
      ***** ***** *****
     *     *     *     *  
    ***   ***   ***   *** 
   ***** ***** ***** *****
  *     *     *     *     *  
***   ***   ***   ***   *** 
***** ***** ***** ***** *****


show(5,5);的效果图


  • 大小: 35.4 KB
0 请登录后投票
   发表时间:2009-11-02  
有必要这样吗?


把你打的三角行当作一个点。。就可以了。。。有中心。。

LS的你的。。。效率很差
0 请登录后投票
   发表时间:2009-11-03  
seele 写道
有必要这样吗?


把你打的三角行当作一个点。。就可以了。。。有中心。。

LS的你的。。。效率很差



你好,我是LZ,我算法的效率是不高,用了近四层循环。不过我说的我还是不太明白,能否写一个程序出来让我看看。谢谢
0 请登录后投票
   发表时间:2009-11-03  
好吧,居然这么多回复,我也来贴一个。
可爱图形之五角星web服务器版本——闪亮登场。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.asyncweb.common.DefaultHttpResponse;
import org.apache.asyncweb.common.HttpCodecFactory;
import org.apache.asyncweb.common.HttpHeaderConstants;
import org.apache.asyncweb.common.HttpRequest;
import org.apache.asyncweb.common.MutableHttpRequest;
import org.apache.asyncweb.common.MutableHttpResponse;
import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoBuffer;
import org.apache.mina.common.IoFutureListener;
import org.apache.mina.common.IoHandler;
import org.apache.mina.common.IoSession;
import org.apache.mina.common.WriteFuture;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

public class Server {

	public void start() throws IOException {
		SocketAcceptor acceptor = new NioSocketAcceptor();
		acceptor.getFilterChain().addLast("codec",
				new ProtocolCodecFilter(new HttpCodecFactory()));

		acceptor.setReuseAddress(true);
		acceptor.getSessionConfig().setReuseAddress(true);
		acceptor.getSessionConfig().setReceiveBufferSize(1024 * 100);
		acceptor.getSessionConfig().setSendBufferSize(1024 * 100);
		acceptor.getSessionConfig().setTcpNoDelay(true);
		acceptor.getSessionConfig().setSoLinger(-1);
		acceptor.setBacklog(50000);
		acceptor.setHandler(new IoHandler() {
			@Override
			public void exceptionCaught(IoSession session, Throwable throwable)
					throws Exception {
			}

			@Override
			public void messageReceived(IoSession session, Object message)
					throws Exception {
				MutableHttpRequest request = (MutableHttpRequest) message;
				MutableHttpResponse response = new DefaultHttpResponse();

				response.setContent(IoBuffer.wrap(readPicuter().getBytes()));

				writeResponse(session, request, response);
			}

			@Override
			public void messageSent(IoSession arg0, Object message)
					throws Exception {
			}

			@Override
			public void sessionClosed(IoSession session) throws Exception {
			}

			@Override
			public void sessionCreated(IoSession session) throws Exception {
			}

			@Override
			public void sessionIdle(IoSession session, IdleStatus status)
					throws Exception {
			}

			@Override
			public void sessionOpened(IoSession session) throws Exception {
			}

			public void writeResponse(IoSession session, HttpRequest req,
					MutableHttpResponse res) {
				res.normalize(req);
				WriteFuture future = session.write(res);
				if ((session.getAttribute("file") == null)
						&& !HttpHeaderConstants.VALUE_KEEP_ALIVE
								.equalsIgnoreCase(res
										.getHeader(HttpHeaderConstants.KEY_CONNECTION)))
					future.addListener(IoFutureListener.CLOSE);
			}
		});

		acceptor.bind(new InetSocketAddress(3000));
	}

	private String readPicuter() throws IOException {
		InputStream is = Server.class.getResourceAsStream("/picuter.txt");
		StringWriter result = new StringWriter();
		PrintWriter out = new PrintWriter(result);
		BufferedReader reader = new BufferedReader(new InputStreamReader(is,
				"utf-8"));
		String line = null;
		while ((line = reader.readLine()) != null) {
			out.println(line);
		}
		is.close();
		return result.toString();
	}

	public static void main(String args[]) throws IOException {
		new Server().start();
	}
}


这个是文件
                                  *
                                 ***
                               *****
                             *      *
                           ***      ***
                         *****     *****


本来打算做成telnet的,后来考虑到web那么普及而且不用客户端,所以就来了这个。

启动程序,访问http:locahost:3000即可看到效果。

鼓掌。。。。。
0 请登录后投票
   发表时间:2009-11-03  
坊间流传此代码效率非常高。来一个贴图
apache基准测试

ab -c1000 -n1000 http://localhost:3000

  • 大小: 42.9 KB
0 请登录后投票
论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics