无符号大数加、减运算。程序设计中经常遇到无符号大数加、减运算问题,请在样例程序Ex1.4基础上实现无符号大数减运算。题目要求输入两个无符号大数,保证一个大数不小于第二个大数,输出它们的和、差。
输入格式:
两个无符号大数,前一个大于等于第二个。
输出格式:
第1行为两个无符号大数相加结果,后一行为两个无符号大数相减结果。
输入样例: | 1234567890987654321333888999666 147655765659657669789687967867 |
---|---|
输出样例: | 1234567890987654321333888999666+147655765659657669789687967867=1382223656647311991123576967533 1234567890987654321333888999666-147655765659657669789687967867=1086912125327996651544201031799 |
作者 | 李卫明 |
---|---|
单位 | 杭州电子科技大学 |
代码长度限制 | 16 KB |
时间限制 | 400 ms |
内存限制 | 64 MB |
Java版本:
package TestCode.class2;
import java.util.ArrayList;
import java.util.Scanner;
public class BigNumber {
public static int[] split_Number(String str){
str = new StringBuffer(str).reverse().toString();
char[] str_arr = str.toCharArray();
int count=0;
int segment = str.length()/8;//java会自动向下取整
int[] number_Array =new int[segment+1];
String input_str="";
for (int i = 0; i < segment; i++) {
for (int j = 0; j < 8; j++) {
int index = 8*i+j;
input_str = input_str.concat(str_arr[index]+"");
}
input_str = new StringBuffer(input_str).reverse().toString();
number_Array[i]=Integer.parseInt(input_str);
input_str="";
if(i==(segment-1)){
input_str="";
for (int j = 0; j < str.length()-8*(i+1); j++) {
int index = 8*(i+1)+j;
input_str = input_str.concat(str_arr[index]+"");
}
input_str = new StringBuffer(input_str).reverse().toString();
number_Array[i+1]=Integer.parseInt(input_str);
}
}
return number_Array;
}
public static ArrayList input_number(){
//8位一段
Scanner sc=new Scanner(System.in);
String str="";
ArrayList arr = new ArrayList();
while (sc.hasNext()){
str = sc.nextLine();
arr.add(str);
}
return arr;
}
public static void bigNumber_add(int[] int_arr1,int[] int_arr2){
int minSegment = (int_arr1.length>int_arr2.length) ?int_arr2.length:int_arr1.length;
int maxSegment = (int_arr1.length>int_arr2.length) ?int_arr1.length:int_arr2.length;
int segmentLength=8;
int[] resSet = new int[maxSegment];
for (int i = 0; i < maxSegment; i++) {
int count=0;
if(i<=minSegment){
if(count==0){
int number_int = (int_arr1[i]+int_arr2[i]); //* ( (int)Math.pow(10,i*8) );
//有进位
count = (new String(number_int+"")).length() > int_arr1.length?1:0;
resSet[i]=number_int;
}else{
int number_int = int_arr1[i]+int_arr2[i]+1;
count = (new String(number_int+"")).length() > int_arr1.length?1:0;
resSet[i]=number_int;
}
}else{
int number_int = (int_arr1.length>int_arr2.length) ?int_arr1[i] : int_arr2[i];
resSet[i]=number_int;
}
}
for (int i = resSet.length-1; i >=0; i--) {
System.out.print(resSet[i]);
}
System.out.println();
}
public static String sub_Normalization(int number){
String str="";
if(number<100000000) str=""+number;
if(number<10000000) str="0"+number;
if(number<1000000) str="00"+number;
if(number<100000) str="000"+number;
if(number<10000) str="0000"+number;
if(number<1000) str="00000"+number;
if(number<100) str="000000"+number;
if(number<10) str="0000000"+number;
if(number==0) str="00000000";
return str;
}
public static void bigNumber_sub(int[] int_arr1,int[] int_arr2){
int minSegment = (int_arr1.length>int_arr2.length) ?int_arr2.length:int_arr1.length;
int maxSegment = (int_arr1.length>int_arr2.length) ?int_arr1.length:int_arr2.length;
int segmentLength=8;
int[] resSet = new int[maxSegment];
int count=0;
for (int i = 0; i < maxSegment; i++) {
if(i<=minSegment){
if(count==0){
if( (int_arr1[i]-int_arr2[i]) <0 ){
resSet[i] = int_arr1[i]-int_arr2[i]+100000000;
count=1;
}
else {
int number_int = (int_arr1[i]-int_arr2[i]); //* ( (int)Math.pow(10,i*8) );
resSet[i] = number_int;
}
}else{
if( (int_arr1[i]-int_arr2[i]-1) <0 ){
resSet[i] = int_arr1[i]-int_arr2[i]-1+100000000;
count=1;
} else {
int number_int = (int_arr1[i]-int_arr2[i]-1); //* ( (int)Math.pow(10,i*8) );
resSet[i] = number_int;
}
}
}else{
int number_int = (int_arr1.length>int_arr2.length) ?int_arr1[i] : int_arr2[i];
resSet[i]=number_int;
}
}
for (int i = resSet.length-1; i >=0; i--) {
if(i==(resSet.length-1)) System.out.print(resSet[i]);
else {
System.out.print(sub_Normalization(resSet[i]));
}
}
System.out.println();
}
public static void main(String[] args) {
ArrayList arr = input_number();
int[] int_arr1 = split_Number(arr.get(0).toString());
int[] int_arr2 = split_Number(arr.get(1).toString());
bigNumber_add(int_arr1,int_arr2);
bigNumber_sub(int_arr1,int_arr2);
}
}