解決大整數相加的演算法

2022-03-16 00:07:59 字數 732 閱讀 1053

演算法主要解決的是使用者輸入的整數過大而溢位造成的問題。解決辦法就是將輸入的整數字串轉換到整型陣列中,加大型的整數裝換為乙個乙個的為數相加,在逐一列印出來。

以下為**:

1 #include 2 #include

3#define maxn 1000

4int

numbera[maxn], numberb[maxn], getresult[maxn];

5char

chnuma[maxn], chnumb[maxn];6//

得到兩個輸入整數最大的長度

7int

getlength()817

18//

初始化整型陣列

19void init(char *a, int *b)

2025}26

27//

兩個整形陣列祝逐項相加,將結果付給接收的整形陣列

28int

add()

2939

//如果進製超出了長度範圍

40 len =i;

41while

(tem)

4246

return

len;47}

4849

intmain()

5066 printf("\n"

);67

}68 printf("

測試結束");

69 }

演算法 大整數相加

兩個36進製的大整數以字串的形式給出,求出兩個大整數的和,並以字串方式輸出。頭條面試題 比如 12346 gsftyhs gsgw1ly public class format36 public static string sum string num1,string num2 num2 sb.to...

演算法筆記 大整數相加

大整數相加,這裡用string輸入,轉為int型別陣列存數,陣列低位存整數的低位 個位存在index 0的地方 include include const int maxlen 10000 struct bign void str2bign char str,int n,bign bign retu...

大整數相加

include include include include 思路如下 將兩個字串分別由低位到高位放置到int陣列中 然後每位對齊相加,大於10,本位取餘,高位進1 char bigintadd const char numstr1,const char numstr2 for i len1 i ...