码迷,mamicode.com
首页 > 其他好文 > 详细

杭电 HDU ACM 5186 zhx's submissions

时间:2015-04-04 15:19:02      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:acm   c++   杭电   算法   编程   

zhx‘s submissions

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


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 i技术分享th技术分享技术分享 oj, he made a技术分享i技术分享技术分享 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. (1n100技术分享, 2B36技术分享)
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
 

Source
 
浪费了好几个小时 从理解题意,题目意思是b进制数相加,每位相加结果不进位。只保留sum mod b的结果,且保证最后结果没有前置0;可以采用倒叙相加。看了看网上的代码,
感觉写的还挺简单了!
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	int n,b ;char ls[250][250],gq[250][250];
	
	while(cin>>n>>b)
	{
		int len, L=-1;
	memset(ls,0,sizeof(ls));
	memset(gq,0,sizeof(gq));
		for(int i=0;i<n;i++)
		{
			scanf("%s",ls[i]);
			 len=strlen(ls[i]);
			if(len>L)
				L=len;
			for(int j=0;j<len;j++)
			{
				gq[i][len-1-j]=ls[i][j];
			}
		}
		int sum=0;int flag=0;
		for(int k=L-1;k>=0;k--)
		{
			sum=0;
			for(int m=0;m<n;m++)
			{
				if(gq[m][k]<='9'&&gq[m][k]>='0')
					sum+=gq[m][k]-48;
				else if(gq[m][k]>=65&&gq[m][k]<='z')
					sum+=gq[m][k]+10-'a';
			}
			
			sum%=b;
			if(sum>9)
			{
				printf("%c",sum-10+'a');
				flag=1;
			}
			else 
			{
				if(sum)
					flag=1;//注意只要在尚未发现第一个不为0得数之前都不能输出!!!不能有前置0!wa至少五次!
				if(flag)
					printf("%d",sum);
			}
		}
			if(!flag)
				cout<<0;
		
		cout<<endl;
		
	}
return 0;
}

杭电 HDU ACM 5186 zhx's submissions

标签:acm   c++   杭电   算法   编程   

原文地址:http://blog.csdn.net/lsgqjh/article/details/44872579

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!