⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.java

📁 此文档包含有AOP
💻 JAVA
字号:
package test.math;

import java.math.BigDecimal;

public class Main {
	static int TOTAL=1000;
	static int BASIC_NUM=3;
	static int MI=4;
	public static void main(String[] args) {
		char num[] =new char[TOTAL];
		int j=1;
		num[0]='1';
		while(j<TOTAL){
			num[j]='0';
			j++;
		}
		for (int m = 0; m < MI; m++) {
			int nextBasic=0;
			for (int k = 0; k<TOTAL; k++) {// 乘2
				char temp = num[k]; //与k位相成
				int b=temp-'0';
				int t = b*BASIC_NUM+nextBasic;
				
				if(t>=10){//大于10
					num[k]=(char)((t%10)+'0');
					nextBasic=t/10;
				}else{//小于10
					num[k]=(char)(t+'0');
					nextBasic=0;
				}
			}
			//----------------------------//
			// 输出数据 开始
			//----------------------------//
			int spaceIndex=TOTAL-1;
			while(spaceIndex>0){
				if(num[spaceIndex]!='0') break;
				spaceIndex--;
			}
			System.out.print("m="+(m+1)+",位数:"+spaceIndex+" ");
			for(int n =spaceIndex;n>=0;n--){
				System.out.print(num[n]);
			}
			System.out.println();
		}
		
		//----------------------------//
		// 输出数据 开始
		//----------------------------//
		int spaceIndex=TOTAL-1;//过滤掉‘0’
		while(spaceIndex>0){
			if(num[spaceIndex]!='0') break;
			spaceIndex--;
		}
		System.out.print("m="+MI+",位数:"+spaceIndex+" ");
		for(int n =spaceIndex;n>=0;n--){
			System.out.print(num[n]);
		}
		System.out.println();
		//----------------------------//
		// 输出数据 结束
		//----------------------------//
		
		//int count=64;
		int num1=1;
		//while(count-->0){
		//	num1*=i;
		//}
		System.out.println(num1);
		System.out.println("//-----------------------//");
		System.out.println("//                       //");
		System.out.println("//   Power(x,y)          //");
		System.out.println("//                       //");
		System.out.println("//-----------------------//");
		long before = System.currentTimeMillis();
		System.out.println("x=2,y=160");
		printNum(power(2,160));
		long after = System.currentTimeMillis();
		System.out.println("用时:"+(after-before));
		int x=2;
		int y=200;
		double result=1.0;
		while(y>0){
			result*=x;
			y--;
 		}
		System.out.println(result);
		System.out.println("//-----------------------//");
		System.out.println("//                       //");
		System.out.println("//   SuperPower(x,y)     //");
		System.out.println("//                       //");
		System.out.println("//-----------------------//");
		before = System.currentTimeMillis();
		System.out.println("x=25,y=250");
		printNum(superPower(25,250));
		after = System.currentTimeMillis();
		System.out.println("用时:"+(after-before));
		
		System.out.println("//-----------------------//");
		System.out.println("//                       //");
		System.out.println("// Using BigDecimal x,y  //");
		System.out.println("//                       //");
		System.out.println("//-----------------------//");
		BigDecimal g = new BigDecimal(25);
		before = System.currentTimeMillis();
		String result3 = g.pow(250).toString();
		System.out.println("x=25,y=250");
		System.out.println("位数:"+result3.length()+" "+result3);
		after = System.currentTimeMillis();
		System.out.println("用时:"+(after-before));
	}
	
	public static char[] power(int x,int y){
		int length = x*(y);
		char num[] =new char[length];
		int j=1;
		num[0]='1';
		while(j<length){
			num[j]='0';
			j++;
		}
		for (int m = 0; m < y; m++) {
			int nextBasic=0;
			for (int k = 0; k<length; k++) {// 乘2
				char temp = num[k]; //与k位相成
				int b=temp-'0';
				int t = b*x+nextBasic;
				
				if(t>=10){//大于10
					num[k]=(char)((t%10)+'0');
					nextBasic=t/10;
				}else{//小于10
					num[k]=(char)(t+'0');
					nextBasic=0;
				}
			}
			//----------------------------//
			// 输出数据 开始
			//----------------------------//
//			int spaceIndex=length-1;
//			while(spaceIndex>0){
//				if(num[spaceIndex]!='0') break;
//				spaceIndex--;
//			}
//			System.out.print("m="+(m+1)+",位数:"+spaceIndex+" ");
//			for(int n =spaceIndex;n>=0;n--){
//				System.out.print(num[n]);
//			}
//			System.out.println();
		}
		return num;
	}
	
	public static void printNum(char[] nums){
		//----------------------------//
		// 输出数据 开始
		//----------------------------//
		int spaceIndex=nums.length-1;//过滤掉‘0’
		while(spaceIndex>0){
			if(nums[spaceIndex]!='0') break;
			spaceIndex--;
		}
		System.out.print("位数:"+(spaceIndex+1)+" ");
		for(int n =spaceIndex;n>=0;n--){
			System.out.print(nums[n]);
		}
		System.out.println();
		//----------------------------//
		// 输出数据 结束
		//----------------------------//
	}
	/**
	 * superPower(int x, int y)
	 * @param x
	 * @param y
	 * @return
	 */
	public static char[] superPower(int x,int y){
		/*
		 * 初始化数据
		 */
		String str = String.valueOf(x);
		StringBuffer sb = new StringBuffer(str);
		sb.reverse();
		char[] a = sb.toString().toCharArray();
		//char[] a = {'1','2'};//底数
		int xLength=a.length;
		int xylength=xLength*y;
		int ii = 0;
		char arr1[] = new char[xylength];//被乘数
		char arr2[] = new char[xylength];//Mid临时数组
		char ar_temp[] = new char[xylength];//临时数组
		int ar_temp_index = xylength-1;
		while(ar_temp_index>=0){
			ar_temp[ar_temp_index]='0';
			arr2[ar_temp_index]='0';
			ar_temp_index--;
		}
		while(ii<xLength){//初始化数据
			arr1[ii]=a[ii];
			ii++;
		}	
		while(ii<xylength){
			arr1[ii]='0';
			ii++;
		}
		//开始计算
		for (int m = 1; m < y; m++) {
			for(int G_m=0;G_m<xLength;G_m++){
				int tempa= a[G_m]-'0';
				int nextBasic=0;
				for (int k = 0; k<xylength; k++) {// 乘2
					char temp = arr1[k]; //与k位相成
					int b=temp-'0';
					int t = b*tempa+nextBasic;
					
					if(t>=10){//大于10
						arr2[k]=(char)((t%10)+'0');
						nextBasic=t/10;
					}else{//小于10
						arr2[k]=(char)(t+'0');
						nextBasic=0;
					}
				}
				//System.out.print("@arr1 index:"+G_m+" ");
				//printNum(arr2);
				addMatrix(ar_temp,arr2,G_m,xylength);//两数相加
				//System.out.print("add after:"+G_m+" ");
				//printNum(ar_temp);
				ar_temp_index = xylength-1;//Mid临时数组清零
				while(ar_temp_index>=0){
					arr2[ar_temp_index]='0';
					ar_temp_index--;
				}
			}//单位操作完成
			char[] mid_result = arr1;
			arr1 = ar_temp;
			ar_temp=mid_result;
			ar_temp_index = xylength-1;//临时数组清零
			while(ar_temp_index>=0){
				ar_temp[ar_temp_index]='0';
				arr2[ar_temp_index]='0';
				ar_temp_index--;
			}
		}
		return arr1;
	}
	
	public static char[] addMatrix(char []a,char []b,int start,int end){
		int xIndex = start;
		int yIndex = 0;
		int sub=0;
		for(int addIndex= start;addIndex<end;addIndex++){
			int _a=a[xIndex]-'0';
			int _b=b[yIndex]-'0';
			int _result=_a+_b+sub;
			if(_result>=10){//大于10
				a[xIndex]=(char)((_result%10)+'0');
				sub=_result/10;
			}else{//小于10
				a[xIndex]=(char)(_result+'0');
				sub=0;
			}
			xIndex++;
			yIndex++;
		}
		return a;//a作为结束字符数组
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -