【Java】大数减法-不使用Biginteger类,用数组模拟运算

题目

数组模拟减法运算
数据范围1~3e30

注释

  • @Title 大数减法.java
  • @author Baisu
  • @date 2019年10月29日
  • @version 1.0

说明

版本1.0,还没开始优化,后续可能会优化代码

源代码

package 大数运算;

import java.util.*;

/**
 * @Title 大数减法.java
 * @author Baisu
 * @date 2019年10月29日
 * @version 1.0
 */

public class 大数减法 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		//声明字符串变量
		String s1 = new String();
		String s2 = new String();
		//输入大数字符串
		s1 = sc.nextLine();
		s2 = sc.nextLine();
		//字符串存储到数组
		char c1[] =s1.toCharArray();
		char c2[] =s2.toCharArray();
		//声明整型数组变量
		int a1 [] = new int [60];
		int a2 [] = new int [60];
		//字符数组转化为整数数组
		for(int i=0; i<c1.length; i++) {
			a1[c1.length-i-1] = c1[i];
			a1[c1.length-i-1]-=48;
		}
		for(int i=0; i<c2.length; i++) {
			a2[c2.length-i-1] = c2[i];
			a2[c2.length-i-1]-=48;
		}
		//比较大小
		int aa1 = 0,aa2 = 0;
		for(int i=40; i>=0; i--)
			if(a1[i]!=0) {
				aa1 = i;
				break;
			}
		for(int i=40; i>=0; i--)
			if(a2[i]!=0) {
				aa2 = i;
				break;
			}
		int t=0;
		if(aa1==aa2) {
			for(int i=aa1; i>=0; i--) {
				if(a1[i]==a2[i]) {
					continue;
				}
				else if(a1[i]<a2[i]) {
					t=1;break;
				}
				else {
					break;
				}
			}
		}
		if(aa1<aa2) {
			t=1;
		}
		//声明整型数组存储结果
		int a3 [] = new int [60];
		//计算结果
		for(int i=0; i<40; i++) {
			a3[i] = a1[i] - a2[i];
		}
		//寻找首个不为0的数组
		int b = 0;
		for(int i=40; i>=0; i--) {
			if(a3[i]!=0) {
				b=i;
				break;
			}
		}
		//修正数值
		for(int i=0; i<b; i++) {
			if(a3[i]<0) {
				if(t==0) {
					a3[i]+=10;
					a3[i+1]--;
				}
				else {
					a3[i]=Math.abs(a3[i]);
				}
			}
		}
		//输出结果
		for(int i=b; i>=0; i--) {
			System.out.print(a3[i]);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用JavaBigInteger求阶乘的方法如下: 1. 首先,你需要导入 java.math.BigInteger 包。 2. 然后,你可以使用如下代码来求一个数的阶乘: ``` BigInteger result = BigInteger.ONE; for (int i = 1; i <= n; i++) { result = result.multiply(BigInteger.valueOf(i)); } ``` 在这段代码中,n 是要求阶乘的数,result 变量用来保存结果。首先,我们将 result 初始化为 1,然后使用 for 循环将 1 到 n 的乘积累加到 result 中。 注意,BigInteger 的乘法操作是使用 multiply() 方法实现的,而不是用 * 运算符。 例如,你可以使用如下代码来求 5 的阶乘: ``` BigInteger result = BigInteger.ONE; for (int i = 1; i <= 5; i++) { result = result.multiply(BigInteger.valueOf(i)); } System.out.println(result); // 输出 120 ``` 希望这能帮到你! ### 回答2: 阶乘是一个正整数连乘积的运算,即n的阶乘(n!)等于1乘2乘3乘...乘n。 在Java中,使用BigInteger可以处理大整数运算,可以用它来求解阶乘。 首先,导入java.math包中的BigInteger。然后,我们可以定义一个方法来计算n的阶乘。方法的参数是一个正整数n,返回类型BigInteger。方法中,我们定义一个BigInteger类型的变量result,初始化为1。 接下来,使用一个循环从1到n,依次将每个数字与result进行连乘运算,最后将result返回。 具体实现代码如下: ```java import java.math.BigInteger; public class Factorial { public static BigInteger factorial(int n) { BigInteger result = BigInteger.ONE; // 初始化为1 for (int i = 1; i <= n; i++) { result = result.multiply(BigInteger.valueOf(i)); // 连乘运算 } return result; } public static void main(String[] args) { int n = 10; BigInteger result = factorial(n); System.out.println(n + "! = " + result); } } ``` 在以上代码中,我们求解了10的阶乘,并将结果打印输出。运行程序,输出结果为: ``` 10! = 3628800 ``` BigInteger可以处理大整数运算,因此可以求解非常大的阶乘,远超过普通整型或长整型的范围限制。 ### 回答3: 要使用JavaBigInteger求阶乘,首先需要导入java.math.BigInteger包。BigInteger是一个不可变的大整数,可以处理超过long类型所能表示的范围的整数。 然后,我们需要创建一个BigInteger类型的变量来存储阶乘的结果。由于阶乘的结果可能非常大,我们可以先将结果初始化为1,即BigInteger result = BigInteger.valueOf(1)。 接下来,使用一个循环来计算阶乘,从1循环到所需的阶乘数n。在每次循环中,将当前的BigInteger变量result与当前的循环变量相乘,并将结果赋值给result,即result = result.multiply(BigInteger.valueOf(i))。 最后,循环结束后,result的值即为所需的阶乘结果。 以下是一个使用JavaBigInteger求阶乘的示例代码: import java.math.BigInteger; public class Factorial { public static void main(String[] args) { int n = 10; // 求10的阶乘 BigInteger result = BigInteger.valueOf(1); for (int i = 1; i <= n; i++) { result = result.multiply(BigInteger.valueOf(i)); } System.out.println("10的阶乘是:" + result); } } 运行以上程序,输出结果为10的阶乘值,即3628800。 使用JavaBigInteger可以轻松计算超大范围内的阶乘,无需担心整数溢出的问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值