打印1到n位最大数

package test;

import java.util.Scanner;
import java.util.concurrent.TimeUnit;



public class Test{	
	static Scanner sc = new Scanner(System.in);
	static String line;		

	static class bigNum{
		int n = 0;
		int[] num;
		public bigNum(int n) {
			this.n = n;
			num = new int[n];
		}
		
		public void setNum(int[] num){
			this.num = num;
			n = num.length;
		}
		
		@Override
		public String toString() {
			StringBuilder sb = new StringBuilder();
			for(int i=n-1;i>=0;i--){
				sb.append(num[i]);
			}
			return sb.toString();
		}
		
		public bigNum add(bigNum b2){
			int flag = 0;
			int len = b2.n>n?b2.n+1:n+1;
			
			bigNum b = new bigNum(len);
			int i=0;
			for(;i9){
					flag = 1;
				}else{
					flag = 0;
				}
			}
			
			for(;i9){
					flag = 1;
				}else{
					flag = 0;
				}
			}
			for(;i9){
					flag = 1;
				}else{
					flag = 0;
				}
			}
			
			if(flag==0){
				b.n = len-1;
			}else{
				b.num[len-1] = 1;
			}
			return b;
		}
		
	}
		
	
	public static void main(String[] args) {
		bigNum b = new bigNum(1);
		b.setNum(new int[]{0});
		bigNum b2 = new bigNum(1);
		b2.setNum(new int[]{1});
		
		
		while(true){
			b = b.add(b2);
			System.out.println(b.add(b2));
			try {
				TimeUnit.MILLISECONDS.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
	}	
}

你可能感兴趣的:(java,算法与数据结构)