Java之数组计算大数相乘


  
  
  
  
public class ArrayMulti {
	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		int[] date = new int[100000];
		date[1] = 1;
		int weishu = 1; // 求出来的值的位数
		System.out.println("------用数组解决大数问题---------");
		System.out.println("求n!的值");
		System.out.print("n=");
		Scanner cin = new Scanner(System.in);
		int n = cin.nextInt();
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= weishu; j++) {
				date[j] = date[j] * i;
			}
			// 确保除最高位外的每位不大于9
			for (int j = 1; j < weishu; j++) {
				if (date[j] >= 10) {
					date[j + 1] += date[j] / 10;
					date[j] = date[j] % 10;
				}
			}
			// 确保最高位不大于9
			while (date[weishu] >= 10) {
				weishu++;
				date[weishu] += date[weishu - 1] / 10;
				date[weishu - 1] = date[weishu - 1] % 10;
			}
		}

		System.out.print(n + "!= ");
		for (int k = weishu; k >= 1; k--) {
			System.out.print(date[k]);
		}
		System.out.println("");
	}
}



   

  
  
  
  
0
0

你可能感兴趣的:(java,Date,exception,String,Class)