c语言大数相加
2013-11-05 00:00
148 查看
#include <stdio.h> #include <string.h> #include <stdlib.h> void add(char str1[], char str2[], char str3[]) { int ns1[100] = {0}; int ns2[100] = {0}; int len1 = strlen(str1); int len2 = strlen(str2); int i=0, j=0, k=0, t=0; if (len1 > len2) { while (i < len1) { ns1[i+1] = str1[i] - '0'; ++i; } i = 0; while (i < len2) { ns2[i+len1-len2+1] = str2[i] - '0'; ++i; } } else { while (i < len1) { ns1[i+len1-len2+1] = str1[i] - '0'; ++i; } i = 0; while (i < len2) { ns2[i+1] = str2[i] - '0'; ++i; } } k = i = len1>len2?len1:len2; while (i>0) { t = (ns1[i]+ns2[i])/10; ns1[i] = (ns1[i]+ns2[i])%10; --i; ns1[i] += t; } if (ns1[0] != 0) while (i<=k) str3[i] = ns1[i]+'0', ++i; else while (i+1<=k) str3[i] = ns1[i+1]+'0', ++i; str3[i] = '\0'; } int main() { char s1[100] = {0}; char s2[100] = {0}; char s3[101] = {0}; int i; scanf("%s%s", s1, s2); add(s1, s2, s3); printf("%s\n", s3); system("pause"); return 0; }
相关文章推荐
- hdu 1250 大数相加并用数组储存
- C语言两个以字符串形式出现的超级大数相加
- Poj 2602 Superlong sums(大数相加)
- 无限大的两个整数相加
- C++实现两个大整数相加
- NYOJ 103.大数A+B(大数问题)
- 大数相加和大数相乘
- [leetcode] 415. Add Strings(大数相加)
- [leetcode] 2. Add Two Numbers(链表、大数相加)
- 高精度大数相加
- 大数相加
- 大数相加算法实现
- codewars打怪升级日记 用JavaScript实现大数相加 /像数字一样加数字型字符串
- 剑指offer之面试题12 大数相加 实现任意两个整数的加法
- 两个非常大的数字相加,相减(JAVA)
- uva-424-大数相加
- HDU 1002(大数相加)
- 大数相加 面试题
- 大数相加
- 面试常见题目:大数相加的Java实现(考虑负数情况)