HDU 大明A+B

来源:互联网 发布:淘宝直播开通要求 编辑:程序博客网 时间:2024/04/28 20:39
#include <cstdio>#include <cstring>int main() {char str_1[500] = {0}, str_2[500] = {0};while (scanf("%s%s", str_1, str_2) != EOF) {int integer_1[500], integer_2[500];int point_1[500], point_2[500];memset(integer_1, -1, sizeof(integer_1));memset(integer_2, -1, sizeof(integer_2));memset(point_1, -1, sizeof(point_1));memset(point_2, -1, sizeof(point_2));//寻找小数点int node_1 = -1, node_2 = -1;for (int i = 0; i < 500; i++)if (str_1[i] == '.')node_1 = i;//如果是整数,存入integerint count;if (node_1 == -1) {for (int j = 450; j >= 0; j--)if (str_1[j] >= '0' && str_1[j] <= '9') {node_1 = j;break;}count = 0;for (int j = node_1; j >= 0 && str_1[j] >= '0' && str_1[j] <= '9'; j--)integer_1[count++] = str_1[j] - '0';}else { //如果是小数, 则把小数部分和整数分开,分别存入point , integer.count = 0;for (int j = node_1 - 1; j >= 0 && str_1[j] >= '0' && str_1[j] <= '9'; j--)integer_1[count++] = str_1[j] - '0';count = 0;for (int j = node_1 + 1; j < 500 && str_1[j] >= '0' && str_1[j] <= '9'; j++)point_1[count++] = str_1[j] - '0';}for (int i = 0; i < 500; i++)if (str_2[i] == '.')node_2 = i;if (node_2 == -1) {for (int j = 450; j >= 0; j--)if (str_2[j] >= '0' && str_2[j] <= '9') {node_2 = j;break;}count = 0;for (int j = node_2; j >= 0 && str_2[j] >= '0' && str_2[j] <= '9'; j--)integer_2[count++] = str_2[j] - '0';}else {count = 0;for (int j = node_2 - 1; j >= 0 && str_2[j] >= '0' && str_2[j] <= '9'; j--)integer_2[count++] = str_2[j] - '0';count = 0;for (int j = node_2 + 1; j < 500 && str_2[j] >= '0' && str_2[j] <= '9'; j++)point_2[count++] = str_2[j] - '0';}//计算小数部分的和int point_3[500] = {0};int integer_3[500] = {0};for (int j = 450; j >= 0; j--) {if (point_1[j] > -1)point_3[j + 1] += point_1[j];if (point_2[j] > -1)point_3[j + 1] += point_2[j];point_3[j] += point_3[j + 1] / 10;point_3[j + 1] %= 10;}//计算整数部分的和integer_3[0] += point_3[0];for (int j = 0; j < 450; j++) {if (integer_1[j] > -1)integer_3[j] += integer_1[j];if (integer_2[j] > -1)integer_3[j] += integer_2[j];integer_3[j + 1] += integer_3[j] / 10; integer_3[j] %= 10;}//分别输出整数.小数点。小数部分int s;for (s = 450; integer_3[s] == 0; s--);for (int j = s; j >= 0; j--)printf("%d", integer_3[j]);int k;for (k = 450; point_3[k] == 0; k--);if (k > 0)printf(".");for (int j = 1; j <= k; j++)printf("%d", point_3[j]);printf("\n");memset(str_1, 0, sizeof(str_1));memset(str_2, 0, sizeof(str_2));}return 0;}

0 0
原创粉丝点击