zhx's submissions
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2460 Accepted Submission(s): 717
Total Submission(s): 2460 Accepted Submission(s): 717
Problem Description
As one of the most powerful brushes, zhx submits a lot of code on many oj and most of them got AC.
One day, zhx wants to count how many submissions he made on n ojs. He knows that on the ith oj, he made ai submissions. And what you should do is to add them up.
To make the problem more complex, zhx gives you n B−base numbers and you should also return a B−base number to him.
What's more, zhx is so naive that he doesn't carry a number while adding. That means, his answer to 5+6 in 10−base is 1 . And he also asked you to calculate in his way.
One day, zhx wants to count how many submissions he made on n ojs. He knows that on the ith oj, he made ai submissions. And what you should do is to add them up.
To make the problem more complex, zhx gives you n B−base numbers and you should also return a B−base number to him.
What's more, zhx is so naive that he doesn't carry a number while adding. That means, his answer to 5+6 in 10−base is 1 . And he also asked you to calculate in his way.
Input
Multiply test cases(less than
1000
). Seek
EOF
as the end of the file.
For each test, there are two integers n and B separated by a space. ( 1≤n≤100 , 2≤B≤36 )
Then come n lines. In each line there is a B−base number(may contain leading zeros). The digits are from 0 to 9 then from a to z (lowercase). The length of a number will not execeed 200.
For each test, there are two integers n and B separated by a space. ( 1≤n≤100 , 2≤B≤36 )
Then come n lines. In each line there is a B−base number(may contain leading zeros). The digits are from 0 to 9 then from a to z (lowercase). The length of a number will not execeed 200.
Output
For each test case, output a single line indicating the answer in
B−base
(no leading zero).
Sample Input
2 3 2 2 1 4 233 3 16 ab bc cd
Sample Output
1 233 14
题意:给出n个b进制的数,要求把这n个数加起来,输出也是b进制,不需要进位。
题解:高精度加法题#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 205;
int n, b, length[N];
char str1[N], res[N], str[N][N];
int solve(char c)
{
int temp;
if (c >= '0' && c <= '9')
{
temp = c - '0';
}
else
{
temp = c + 10 - 'a';
}
return temp;
}
int main()
{
while (scanf("%d%d", &n, &b) != EOF)
{
memset(str, '0', sizeof(str));
int len = 0;
for (int i = 0; i < n; i++)
{
scanf("%s", str1);
length[i] = strlen(str1);
for (int j = 0, k = length[i] - 1; k >= 0; k--, j++)
{
str[i][j] = str1[k];
}
len = max(len, length[i]);
}
for (int i = 0; i < len; i++)
{
int temp = 0;
for (int j = 0; j < n; j++)
{
char c = str[j][i];
int temp1 = solve(c);
temp += temp1;
temp = temp % b;
}
if (temp >= 10)
{
res[i] = 'a' + temp - 10;
}
else
{
res[i] = temp + '0';
}
}
int flag = 1, i = 0;
for (i = len - 1; i >= 0; i--)
{
if (res[i] != '0')
{
break;
}
}
if (i == -1)
{
flag = 0;
}
for (int j = i; j >= 0; j--)
{
printf("%c", res[j]);
}
if (!flag)
{
printf("0");
}
printf("\n");
}
return 0;
}