标签:algorithm
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 858 Accepted Submission(s): 233
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.
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.
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
Source
题意:n个B进制的数相加,B进制输出结果。
解析:开始理解错题意了,最后才发现是每位相加时不产生进位。。。直接模拟即可。
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n, m;
char s[1002][202];
char ans[202];
int len[202];
int get(char c){
return (c >= '0' && c <= '9') ? c - '0' : c - 'a' + 10;
}
char put(int x){
return x < 10 ? x + '0' : x - 10 + 'a';
}
int main(){
// freopen("in.txt", "r", stdin);
while(scanf("%d%d", &n, &m)==2){
int maxlen = 0;
for(int i=0; i<n; i++){
scanf("%s", s[i]);
len[i] = strlen(s[i]);
maxlen = max(maxlen, len[i]);
}
for(int i=0; i<maxlen; i++) ans[i] = '0';
ans[maxlen] = 0;
for(int j=1; j<=maxlen; j++){
for(int i=0; i<n; i++){
if(len[i] >= j){
int cnt = maxlen - j;
ans[cnt] = put((get(ans[cnt]) + get(s[i][len[i] - j])) % m);
}
}
}
int i = 0;
while(ans[i] == '0' && i < maxlen - 1) i ++;
printf("%s\n", ans + i);
}
return 0;
}
PS:模拟到吐血。。。
BestCoder Round #33
标签:algorithm
原文地址:http://blog.csdn.net/u013446688/article/details/44276077