HDU-5186-zhx's submissions(Java+简单模拟)

zhx's submissions

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


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   Bbase  numbers and you should also return a  Bbase  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  10base  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  Bbase  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  Bbase (no leading zero).
 

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

Sample Output
   
   
   
   
1 233 14
 

Source
BestCoder Round #33
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5189  5188  5187  5185  5184 
 


前几天的BestCoder题,中文解释在下面......
个人觉得,算上坑爹程度,还是有点难的!

zhx's submissions


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


问题描述
作为史上最强的刷子之一,zhx在各大oj上交了很多份代码,而且多数都AC了。
有一天,zhx想数一数他在
     
      n
     个oj上一共交了多少份代码。他现在已经统计出在第
     
      i
     个oj上,他交了
     
      ai
     份代码。而把它们加起来就是你的工作了。
当然zhx是一个不走寻常路的人,所以他的数字都是用
     
      B
     进制表示的。而他也要求你告诉他
     
      B
     进制的数。
但是zhx有一个恶趣味的习惯,他算加法的时候从来不进位。比如他算十进制
     
      5+6
     的答案是
     
      1
     。而且他还要求你也要按照他的方式来做加法。
输入描述
多组数据(不超过
     
      1000
     组)。读到文件尾。
对于每组数据,第一行是两个空格分开的正整数
     
      n
     
     
      B
     (
     
      1n100
     , 
     
      2B36
     )
接下来
     
      n
     行,每行一个
     
      B
     进制数
     
      ai
     。数码是0到9和小写a到z。输入可能包含前导0,数字长度不超过200位。
输出描述
对于每组询问输出一行一个
     
      B
     进制数表示答案。不能包含前导0。
输入样例
2 3
2
2
1 4
233
3 16
ab
bc
cd
输出样例
1
233
14


BestCoder题解:
送分题。唯一的意义在于比手速。首先读进来的时候把字母和数字都转换成0到35的数字,加起来直接取模,算出答案。 坑点是只有1个数的情况,还有答案等于0的时候也要输出一行一个0。 (hint: 这道题本来想出b进制高精度加法的,然后某君告诉我java里的BigInteger可自定义进制。汗.

我只想说送分题,我也做的不是那么容易!狂汗!因为题目说了数字长度不超过200位,很显然,我们最好用字符了获取当前B进制数ai.
当然你也可以使用Java的特性BigInteger自定义进制,这个楼主不了解,感兴趣的JavaOier可以试试......用字符串获取之后,然后用String.charAt(i)来把读进来的数字和字母转换成数字,存储在开头定义的数组ans里面,这个数组是存取每一位的ai和的,即加起来的每一位都进行取模运算.接下来按照输出格式,规范输出.

说一下本题坑点:
1.只有一个数的情况.
2.答案等于0的时候也要输出一个0.
3.如果输出当前位数的数字大于10,记得用字母来代替(ASCII码).

只要理清思路,不要慌张.这个题目也就迎刃而解了!AC之路任重而道远,各位Oier!与君共勉!


import java.io.*;
import java.util.*;

public class Main
{

	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Scanner input = new Scanner(System.in);
		while (input.hasNext())
		{
			int n = input.nextInt();
			int B = input.nextInt();
			int Max = 0;
			int ans[] = new int[201];
			input.nextLine();
			for (int i = 0; i < n; i++)
			{
				String str = input.nextLine();
				int len = str.length();
				Max = Math.max(Max, len); 							// 取字符串的最大长度,也就是取字符串的位数.
				int k = 0; 											// k计数器 ,记录每一个位的数和.
				for (int j = str.length() - 1; j >= 0; j--)
				{
					int temp = 0;
					if (str.charAt(j) >= '0' && str.charAt(j) <= '9')
					{
						temp = str.charAt(j) - '0';
					}
					else if (str.charAt(j) >= 'a' && str.charAt(j) <= 'z')
					{
						temp = (str.charAt(j) - 'a') + 10;
					}
					ans[k] = (ans[k] + temp) % B;                   //加起来直接取模
					k++;
				}
			}
			int l = Max - 1;
			while (ans[l] == 0 && l > 0)
			{
				l--;
			}
			if (l == 0 && ans[l] == 0)
			{
				System.out.println("0");
			}
			else
			{
				for (; l >= 0; l--)
				{
					if (ans[l] < 10)
						System.out.print(ans[l]);
					else
					{
						char transfer = (char) ('a' + ans[l] - 10);
						System.out.print(transfer);
					}
				}
				System.out.println();
			}
		}
	}

}




你可能感兴趣的:(java,ACM,HDU,BestCoder,简单模拟)