【大数问题】 HDOJ 2100 Lovekey

用java做还是不难的。。。

import java.util.Scanner;
import java.math.BigInteger;
import java.math.BigDecimal;

public class Main
{
	public static void main(String[] args)
	{
		Scanner scanner = new Scanner(System.in);
		String a, b;
		char s[] = new char[205];
		BigInteger ans, aa, bb;
		int len;
		while(scanner.hasNext()) {
			a = scanner.next();
			b = scanner.next();
			ans = BigInteger.ZERO;
			aa = BigInteger.ZERO;
			bb = BigInteger.ZERO;
			len = a.length();
			s = a.toCharArray();
			for(int i = 0; i < len; i++) {
				aa = aa.multiply(BigInteger.valueOf(26));
				aa = aa.add(BigInteger.valueOf(s[i] - 'A'));
			}
			len = b.length();
			s = b.toCharArray();
			for(int i = 0; i < len; i++) {
				bb = bb.multiply(BigInteger.valueOf(26));
				bb = bb.add(BigInteger.valueOf(s[i] - 'A'));
			}
			ans = aa.add(bb);
			s = new char[205];
			len = 0;
			while(ans.mod(BigInteger.valueOf(26)) == BigInteger.ZERO && ans.compareTo(BigInteger.ZERO) > 0) {
				ans = ans.divide(BigInteger.valueOf(26));
				s[len++] = 'A';
			}
			while(ans.compareTo(BigInteger.ZERO) > 0) {
				aa = ans.mod(BigInteger.valueOf(26));
				ans = ans.divide(BigInteger.valueOf(26));
				s[len++] = (char)(aa.intValue() + 'A');
			}
			for(int i = 0; i < len/2; i++) {
				char tmp = s[i];
				s[i] = s[len-1-i];
				s[len-1-i] = tmp;
			}
			if(len != 0) for(int i = 0; i < len; i++) System.out.print(s[i]);
			else System.out.println('A');
			System.out.println();
		}
	}
}


你可能感兴趣的:(HDU)