本文主要是介绍hdu 2100 Lovekey(进制下的高精度加法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目连接:2100 Lovekey
解题思路:直接加法模拟, 只是将10 换成26.
#include <stdio.h>
#include <string.h>
const int N = 205;int change(char *str, int num[]) {int len = strlen(str);memset(num, 0, sizeof(num));for (int i = 0; i < len; i++)num[len - i - 1] = str[i] - 'A';return len;
}int add(int a[], int b[], int sum[], int na, int nb, int base) {int n = 0, t = 0;memset(sum, 0, sizeof(sum));for (int i = 0; i < na || i < nb; i++) {sum[i] = t;if (i < na) sum[i] += a[i];if (i < nb) sum[i] += b[i];t = sum[i] / base;sum[i] %= base;n++;}while (t) {sum[n++] = t % base;t = t / base;}return n;
}int main() {int n1, n2, num1[N], num2[N], sum[N];char str1[N], str2[N];while (scanf("%s%s", str1, str2) == 2) {n1 = change(str1, num1);n2 = change(str2, num2);n1 = add(num1, num2, sum, n1, n2, 26);int flag = 0;for (int i = n1 - 1; i >= 0; i--) {if (flag || sum[i]) {printf("%c", 'A' + sum[i]);flag = 1;}}if (flag == 0) printf("A");printf("\n");}return 0;
}
这篇关于hdu 2100 Lovekey(进制下的高精度加法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!