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; }
相关文章推荐
- web性能测试
- android源码framework下添加新资源的方法
- 无符号整数
- java实现鼠标和键盘动作后台监听 (未测试)
- UTF-8,UTF-16,UTF-32编码方式都是UNICODE,但只是他们的保存方式不同。
- iOS复习安排
- RxJava详解
- JSON的使用
- 华为设备接口备份配置
- 【嵌入式学习日记】2016年7月21日
- 测试redis+keepalived实现简单的主备切换【转载】
- Java面向对象:封装性
- 【刷题之路】连续子数组的最大和
- 如何开始使用PyCharm,并拥有一个高效的Python IDE
- poj1988cube stacking(并查集)
- [Java]将Base64编码字符转为图片
- iOS 推送证书生成pem
- struts2+easyui分页排序
- 在Struts2中使用ValueStack、ActionContext、ServletContext、request、session等
- Python新手学习基础之运算符——比较运算符