您的位置:首页 > 其它

hdu 5186(模拟)

2016-07-22 09:54 411 查看

zhx's submissions

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2293 Accepted Submission(s): 641


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, output a single line indicating the answer in B−base(no leading zero).

[align=left]Sample Input[/align]

2 3
2
2
1 4
233
3 16
ab
bc
cd

[align=left]Sample Output[/align]

1
233
14

[align=left]Source[/align]
BestCoder Round #33

题意:给出n个字符串,这些字符串都是b进制的,将这些字符串相加,但是我们得到的结果是不需要进位的,问最后得到的结果是多少??
例:
3 16
ab
bc
cd
ab+bc = 57
57+cd = 14
题解:我的方法是先把字符串全部记下来,然后翻转,依照这些字符串中最长的那个进行补 0 ,然后依次相加,有进位时就减掉进制,记得判断负数,是负数就要进行取模变成正的,最后得到结果串记得翻转回来。

#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;

int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
char res[205];
char str[105][205];
int len[105];
int MAX=-1;
for(int i=0;i<n;i++){
scanf("%s",str[i]);
len[i] = strlen(str[i]);
reverse(str[i],str[i]+len[i]);
MAX = max(MAX,len[i]);
}
for(int i=0;i<n;i++){
for(int j=len[i];j<MAX;j++){
str[i][j] = '0';
}
if(i==0){
for(int j=0;j<MAX;j++){
res[j] = str[0][j];
}
}
}
int a,b;
for(int i=1;i<n;i++){
for(int j=0;j<MAX;j++){
if(str[i][j]<='9'&&str[i][j]>='0') a = str[i][j]-'0';
else a = str[i][j]-'a'+10;
if(res[j]<='9'&&res[j]>='0') b = res[j]-'0';
else b = res[j]-'a'+10;
int c = ((a+b-m)%m+m)%m;
if(c>=0&&c<=9) res[j] = c+'0';
else res[j] = c-10+'a';
}
}
reverse(res,res+MAX);
bool flag = false;
for(int i=0;i<MAX-1;i++){
if(res[i]!='0'){
flag = true;
}
if(flag){
printf("%c",res[i]);
}
}
printf("%c\n",res[MAX-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: