大数加法 51nod 转载自 逐梦者

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数加法 51nod 转载自 逐梦者相关的知识,希望对你有一定的参考价值。

 题目在这里
1
#include <stdio.h> 2 #include <string.h> 3 #define _MAX 10005 4 int product[_MAX] = {0}; 5 char numOne[_MAX] = {0}, numTwo[_MAX] = {0}; 6 char numOneUp[_MAX] = {0}, numTwoUp[_MAX] = {0}; //倒序存储位置 7 int absMax = 1; 8 //递归进位函数 9 void Carrying(int tag, int i, int *p) 10 { 11 p[i] += tag; 12 if (p[i] > 9) 13 { 14 tag = 1; 15 p[i] -= 10; 16 Carrying(tag, i + 1, p); //写成Carrying(tag, i, j+1, p);也成立,为了让i+j递增而已 17 } 18 return ; 19 } 20 21 //递归退位函数 22 void Abdicate(int tag, int i, int *p) 23 { 24 p[i] += tag; 25 if (p[i] < 0) 26 { 27 tag = -1; 28 p[i] += 10; 29 Abdicate(tag, i + 1, p); 30 } 31 return ; 32 } 33 34 //判断大小 35 void size(int numOneLen, int numTwoLen) 36 { 37 int i, j; 38 if (numOneLen < numTwoLen) 39 { 40 absMax = 2; 41 for (i = 0; i < numTwoLen; i++) 42 { 43 numOneUp[i] ^= numTwoUp[i]; 44 numTwoUp[i] ^= numOneUp[i]; 45 numOneUp[i] ^= numTwoUp[i]; 46 } 47 return ; 48 } 49 if (numOneLen == numTwoLen) 50 { 51 for (i = numOneLen - 1; i >= 0; i--) 52 { 53 if (numOneUp[i] < numTwoUp[i]) 54 { 55 absMax = 2; 56 for (j = 0; j < numTwoLen; j++) 57 { 58 numOneUp[j] ^= numTwoUp[j]; 59 numTwoUp[j] ^= numOneUp[j]; 60 numOneUp[j] ^= numTwoUp[j]; 61 } 62 return ; 63 } 64 else if (numOneUp[i] > numTwoUp[i]) 65 { 66 return ; 67 } 68 } 69 } 70 return ; 71 } 72 73 //加减处理 74 void addOrSub(int numOneLen, int numTwoLen, int flag) //flag加法为1减法为0 75 { 76 int i, key = 0, tag, numLenMax, numLenMin; 77 numOneLen = numOneLen - 1; 78 numTwoLen = numTwoLen - 1; 79 //数据逆序 80 for (i = numOneLen; i > 0; i--) 81 { 82 numOneUp[key++] = numOne[i]; 83 } 84 if (numOne[0] != -) 85 { 86 numOneUp[key] = numOne[0]; 87 numOneLen++; 88 } 89 key = 0; 90 for (i = numTwoLen; i > 0; i--) 91 { 92 numTwoUp[key++] = numTwo[i]; 93 } 94 if (numTwo[0] != -) 95 { 96 numTwoUp[key] = numTwo[0]; 97 numTwoLen++; 98 } 99 numLenMax = numOneLen > numTwoLen ? numOneLen : numTwoLen; 100 numLenMin = numOneLen < numTwoLen ? numOneLen : numTwoLen; 101 //逐位相加 102 if (flag) 103 { 104 size(numOneLen, numTwoLen); 105 for (i = 0; i < numLenMax; i++) 106 { 107 product[i] = (int)numOneUp[i] - 48; 108 } 109 for (i = 0; i < numLenMin; i++) 110 { 111 tag = (int)numTwoUp[i]-48; 112 Carrying(tag, i, product); //递归 113 } 114 } 115 //逐位相减 116 else 117 { 118 size(numOneLen, numTwoLen); 119 for (i = 0; i < numLenMax; i++) 120 { 121 product[i] = (int)numOneUp[i] - 48; 122 } 123 for (i = 0; i < numLenMin; i++) 124 { 125 tag = -((int)numTwoUp[i]-48); 126 Abdicate(tag, i, product); //递归 127 } 128 } 129 } 130 131 //输出结果 132 void print() 133 { 134 int i, j; 135 //倒序输出结果 136 for (i = _MAX - 1; i > 0; i--) 137 { 138 if (product[i] != 0) 139 { 140 break; //查找到第一个不等于0的跳出 141 } 142 } 143 for (j = i; j >= 0; j--) 144 { 145 printf("%d",product[j]); 146 } 147 printf("\n"); 148 } 149 150 int main(int argc, const char * argv[]) 151 { 152 int numOneLen, numTwoLen; 153 154 scanf("%s %s",numOne,numTwo); //存数据 155 156 numOneLen=(int)strlen(numOne); 157 numTwoLen=(int)strlen(numTwo); 158 159 if (numOne[0] == - && numTwo[0] == -) 160 { 161 addOrSub(numOneLen, numTwoLen, 1); 162 printf("-"); 163 print(); 164 } 165 else if (numOne[0] == - || numTwo[0] == -) 166 { 167 addOrSub(numOneLen, numTwoLen, 0); 168 if ((numOne[0] == - && absMax == 1) || (numTwo[0] == - && absMax == 2)) 169 { 170 printf("-"); 171 } 172 print(); 173 } 174 else 175 { 176 addOrSub(numOneLen, numTwoLen, 1); 177 print(); 178 } 179 return 0; 180 }

 

以上是关于大数加法 51nod 转载自 逐梦者的主要内容,如果未能解决你的问题,请参考以下文章

51 Nod 1005 大数加法Java大数乱搞,python大数乱搞

51Nod1005 大数加法

51nod 1005 大数加法

51nod基础题感触(1005大数加法)

N的阶乘(10000) 51 nod——1057 (大数)

51nod 1027大数乘法