方法1(使用链表)
说明:解题思路和力扣中一个算法题一致,点我去看
public class Test {
public static void main(String[] args) {
// 数据准备
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数:");
String s1 = scanner.next();
System.out.print("请输入第二个数:");
String s2 = scanner.next();
// 数字字符串转Node节点
ListNode l1 = str2ListNode(s1);
ListNode l2 = str2ListNode(s2);
// 两数加和
ListNode listNode = addTwoNumbers(l1, l2);
// Node节点转字符串
String sum = listNode2Str(listNode);
// 输出计算结果
System.out.println("计算结果如下:");
System.out.println(String.format("%s + %s = %s", s1, s2, sum));
}
/**
* 两数加和
*
* @param l1 Node节点1
* @param l2 Node节点2
* @return 加和之后的节点
* @date 2022/6/11 22:05
**/
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = null;
ListNode tail = null;
int carry = 0;
while (l1 != null || l2 != null) {
int a = l1 != null ? l1.val : 0;
int b = l2 != null ? l2.val : 0;
int c = a + b + carry;
carry = c / 10;
c = c % 10;
if (head == null) {
tail = head = new ListNode(c);
} else {
tail.next = new ListNode(c);
tail = tail.next;
}
l1 = l1 != null ? l1.next : null;
l2 = l2 != null ? l2.next : null;
}
if (carry > 0) {
tail.next = new ListNode(carry);
}
return head;
}
/**
* 字符串转Node节点
*
* @param number 字符串
* @return Node节点
* @date 2022/6/11 22:03
**/
private static ListNode str2ListNode(String number) {
ListNode head = null;
String[] numArr = number.split("");
for (String num : numArr) {
head = new ListNode(Integer.valueOf(num), head);
}
return head;
}
/**
* Node节点转字符串
*
* @param head Node节点
* @return 字符串
* @date 2022/6/11
**/
private static String listNode2Str(ListNode head) {
StringBuilder sb = new StringBuilder();
List<Integer> numList = new ArrayList<>();
while (head != null) {
sb.insert(0, head.val);
head = head.next;
}
return sb.toString();
}
}
class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
方法2(使用数组)
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数:");
String s1 = scanner.next();
System.out.print("请输入第二个数:");
String s2 = scanner.next();
String sum = bigAdd(s1, s2);
// 输出计算结果
System.out.println("计算结果如下:");
System.out.println(String.format("%s + %s = %s", s1, s2, sum));
}
private static String bigAdd(String m, String n) {
// 获取字符串对应的整型数组
int[] a = new int[m.length()];
int[] b = new int[n.length()];
for (int i = m.length() - 1; i >= 0; i--) {
a[i] = m.charAt(i) - '0';
}
for (int j = n.length() - 1; j >= 0; j--) {
b[j] = n.charAt(j) - '0';
}
// 保证a数组最大
if (a.length < b.length) {
int[] c = a;
a = b;
b = c;
}
// 求和
int[] result = a;
for (int i = 0; i < b.length; i++) {
result[i + a.length - b.length] += b[i];
}
// 进位
for (int j = result.length - 1; j > 0; j--) {
int c = result[j] / 10;
result[j - 1] += c;
result[j] = result[j] % 10;
}
//变成字符串
StringBuilder sb = new StringBuilder();
for (int i : result) {
sb.append(i);
}
//返回
return sb.toString();
}
}
方法3(使用数组,位置不够补0)
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数:");
String s1 = scanner.next();
System.out.print("请输入第二个数:");
String s2 = scanner.next();
String sum = bigAdd(s1, s2);
// 输出计算结果
System.out.println("计算结果如下:");
System.out.println(String.format("%s + %s = %s", s1, s2, sum));
}
private static String bigAdd(String m, String n) {
// 获取字符串对应的整型数组
int[] a = new int[m.length()];
int[] b = new int[n.length()];
for (int i = m.length() - 1; i >= 0; i--) {
a[i] = m.charAt(i) - '0';
}
for (int j = n.length() - 1; j >= 0; j--) {
b[j] = n.charAt(j) - '0';
}
//保证a数组是长度最大的数组
if (a.length < b.length) {
int[] c = a;
a = b;
b = c;
}
// 对b数组比a数组少的位置补0
int[] c = new int[a.length];
for (int i = 0; i < b.length; i++) {
c[i + (a.length - b.length)] = b[i];
}
//求和
int[] result = new int[a.length];
for (int j = 0; j < result.length; j++) {
result[j] = a[j] + c[j];
}
//进位
for (int k = result.length - 1; k > 0; k--) {
int e = result[k] / 10;
result[k - 1] += e;
result[k] = result[k] % 10;
}
//变成字符串
StringBuilder sb = new StringBuilder();
for (int i : result) {
sb.append(i);
}
//返回
return sb.toString();
}
}