就是列个竖式的思想,看着没人我就写一篇啦。
Java 代码
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = input.nextInt();//总共有n位数字
int[] add = new int[n];
int[] a = new int[n];
int[] b = new int[n];
for(int i = 0 ; i < n ; i++){
add[i] = input.nextInt();
}
for(int i = 0 ; i < n ; i++){
a[i] = input.nextInt();
}
input.nextLine();
String oper = input.nextLine();
for(int i = 0 ; i < n ; i++){
b[i] = input.nextInt();
}
int[] res = new int[n];
if("+".equals(oper)){
int carry = 0;//保存进位
for(int i = n - 1 ; i >= 0 ; i--){
res[i] = (a[i] + b[i] +carry) % add[i];
carry = (a[i] + b[i] +carry) / add[i];
}
}else{
int bet = 0;//保留借位
for(int i = n - 1 ; i >= 0 ; i--){
res[i] = (a[i] - bet - b[i] + add[i]) % add[i];
bet = (a[i] - bet - b[i]) >= 0 ? 0 : 1;
}
}
for(int i = 0 ; i < n ; i++){
System.out.print(res[i]);
if(i != n-1){
System.out.print(" ");
}
}
}
}