zoukankan      html  css  js  c++  java
  • JAVA遍历21位数

    跑得是慢了点。。。

    有没有大佬有更好的方法

    class BigNum{
    	long []num = new long[21];
    	
    	public BigNum(){
    		for(int i = 0; i < num.length; i++) {
    			num[i] = 0;
    		}
    	}
    	
    	public boolean full() {
    		for(int i = 0; i < num.length; i++) {
    			if(num[i] != 9)	{
    				return false;
    			}
    		}
    		return true;
    	}
    	
    	public void plusOne() {
    		int tag = 0;	//从最后一位开始加,如果其9,将其变为0,另高一位加一,
    						//如果高一位是9,其变为0,高一位加一一次类推直到全为9
    						//tag == 0说明从第0位开始
    		if(num[tag]!=9) {
    			num[tag]++;
    			return;
    		}else {
    			if(full())	return;	//如果全为9则溢出
    			
    			//关键算法。。进位操作
    			while(true) {
    				num[tag]=0;
    				tag++;
    				if(num[tag] != 9) {
    					num[tag]++;
    					return;
    				}
    			}
    		}
    	}
    	
    	public void show() {
    		for(int i = num.length-1; i >-1; i--) {
    			System.out.print(num[i]);
    		}
    		System.out.println();
    	}
    	
    	
    }
    
    public class T1 {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		BigNum num = new BigNum();
    		//long []per = new long[21];
    		while(!num.full()) {
    			num.plusOne();
    			num.show();
    		}
    	}
    
    }
    
  • 相关阅读:
    mysql学习笔记
    MySQL的MySQL 的JDBC的安装与使用
    numpy的使用方法
    Linux命令
    MongoDB数据库
    爬虫请求库之selenium
    解析库beautifulsoup
    Requests属性
    正向代理、反向代理
    爬虫基本原理
  • 原文地址:https://www.cnblogs.com/jzl123/p/8342324.html
Copyright © 2011-2022 走看看