有符號(hào)大數(shù)加墙贱、減運(yùn)算热芹。請(qǐng)?jiān)跇永绦駿x1.4基礎(chǔ)上實(shí)現(xiàn)無(wú)符號(hào)大數(shù)比較運(yùn)算(小于、小于等于惨撇、等于伊脓、大于、大于等于)魁衙,并進(jìn)一步實(shí)現(xiàn)有符號(hào)大數(shù)的加报腔、減運(yùn)算。題目要求輸入兩個(gè)有符號(hào)大數(shù)剖淀,輸出它們的和纯蛾、差。
輸入格式:
兩個(gè)有符號(hào)大數(shù)纵隔。
輸出格式:
兩個(gè)有符號(hào)大數(shù)翻诉,每個(gè)一行,正號(hào)不輸出捌刮。
輸入樣例: | -1234567890987654321333888999666 147655765659657669789687967867 |
---|---|
輸出樣例: | -1086912125327996651544201031799 -1382223656647311991123576967533 |
作者 | 李衛(wèi)明 |
---|---|
單位 | 杭州電子科技大學(xué) |
代碼長(zhǎng)度限制 | 16 KB |
時(shí)間限制 | 400 ms |
內(nèi)存限制 | 64 MB |
Java版本:
import java.util.ArrayList;
import java.util.Scanner;
public class BigNumber2 {
public static int singed_Compare(String a,String b){
//1表示 a>b 0表示 a<b
int count=0;
if( a.contains("-") && !b.contains("-")) return 0;
else if( !a.contains("-") && b.contains("-") ) return 1;
else if(!a.contains("-") && !b.contains("-")){
if(a.length()>b.length())return 1;
else if(a.length()<b.length()) return 0;
else if(a.length()==b.length()){
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
for (int i = int_arr1.length-1; i >=0 ;i--) {
if(int_arr1[i]>int_arr2[i])return 1;
else if (int_arr1[i]<int_arr2[i])return 0;
}
}
}else if(a.contains("-") && b.contains("-")){
if(a.length()>b.length())return 0;
else if(a.length()<b.length()) return 1;
else if( a.length()==b.length() ){
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
for (int i = int_arr1.length-2; i >=0 ;i--) {
if(int_arr1[i]>int_arr2[i])return 0;
else if (int_arr1[i]<int_arr2[i])return 1;
}
}
}
return count;
}
public static void signed_Add(String a,String b){
if(!a.contains("-") && !b.contains("-")){
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
bigNumber_add(int_arr1,int_arr2);
}else if( (!a.contains("-")&&b.contains("-")) || (a.contains("-")&&!b.contains("-")) ){
//判斷a,b的大小
int flag = singed_Compare(a,b);
if(a.contains("-"))a=a.replace("-","");
else b=b.replace("-","");
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
if(singed_Compare(a,b)==1) {
if(flag==1)
bigNumber_sub(int_arr1,int_arr2);
else{
System.out.print("-");
bigNumber_sub(int_arr1,int_arr2);
}
}else{
if(flag==1){
System.out.print("-");
bigNumber_sub(int_arr2,int_arr1);}else{
bigNumber_sub(int_arr2,int_arr1);
}
}
}else {
b=b.replace("-","");
a=a.replace("-","");
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
System.out.println("-");
bigNumber_add(int_arr1,int_arr2);
}
}
public static void signed_Sub(String a,String b){
if(!a.contains("-") && !b.contains("-")){
if(singed_Compare(a,b)==1){
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
bigNumber_sub(int_arr1,int_arr2);
}else {
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
System.out.print("-");
bigNumber_sub(int_arr2,int_arr1);
}
}else if( (!a.contains("-")&&b.contains("-")) || (a.contains("-")&&!b.contains("-")) ){
int flag = singed_Compare(a,b);
if(a.contains("-"))a=a.replace("-","");
else b=b.replace("-","");
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
if(flag==1) {
bigNumber_add(int_arr1,int_arr2);
}else{
System.out.print("-");
bigNumber_add(int_arr1,int_arr2);
}
}else {
b=b.replace("-","");
a=a.replace("-","");
int flag = singed_Compare(a,b);
int[] int_arr1 = split_Number(a);
int[] int_arr2 = split_Number(b);
if(flag==1){
System.out.println("-");
bigNumber_sub(int_arr1,int_arr2);
}else{
bigNumber_sub(int_arr2,int_arr1);
}
}
}
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會(huì)自動(dòng)向下取整
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];
int count=0;//count=0表示 沒(méi)有進(jìn)位
for (int i = 0; i < maxSegment; i++) {
if(i<=minSegment){
if(count==0){
int number_int = (int_arr1[i]+int_arr2[i]); //* ( (int)Math.pow(10,i*8) );
//有進(jìn)位
count = (new String(number_int+"")).length() > 8?1:0;
if(count==1) resSet[i]=number_int-100000000;
else{ resSet[i]=number_int;}
}else{
int number_int = int_arr1[i]+int_arr2[i]+1;
count = (new String(number_int+"")).length() > 8?1:0;
if(count==1) resSet[i]=number_int-100000000;
else{
resSet[i]=number_int;
// count=0;
}
}
}else{
if(count==1){
int number_int = (int_arr1.length>int_arr2.length) ?int_arr1[i]+1 : int_arr2[i]+1;
count = (new String(number_int+"")).length() > 8?1:0;
if(count==1) resSet[i]=number_int-100000000;
else {
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;//count=0表示 沒(méi)有借位
for (int i = 0; i < maxSegment; i++) {
if(i<=minSegment){
if(count==0){
//第1次計(jì)算 不需要借位count==0
if( (int_arr1[i]-int_arr2[i]) <0 ){
//需要補(bǔ)位
resSet[i] = int_arr1[i]-int_arr2[i]+100000000;
count=1;
}
else {
//不需要補(bǔ)位
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;
count=0;
}
}
}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();
System.out.println();
signed_Add(arr.get(0).toString(),arr.get(1).toString());
signed_Sub(arr.get(0).toString(),arr.get(1).toString());
}
}