UVa290 - Palindroms <---> smordnilaP(进制转换、回文)

 Palindroms tex2html_wrap_inline26 smordnilaP 

The following problem deals with Palindroms composed of digits. A number is a palindrom, if the sequence of signs (digits or characters) read from left to right and read from right to left are identical. Now, given the number 65 with base 10, adding the number read from right to left , that means 56, leads to 121. By definition 121 is a palindrom. With another number you might have to repeat this step until the sum is of the required palindrom form. eg. 87:

    87 + 78 = 165
    165 + 561 = 726
    726 + 627 = 1353
    1353 + 3531 = 4884

The number of steps is 4.

This works in any base with any number. Naturally the number of steps increases incredibly fast, so there exist numbers in base 10 that requires more than 10,000 steps. You will have to find the numbers of steps of a given number in all the bases 15 down to 2. When a Number is in an illegal form in a base, the number of Steps will be represented by a ``?".

Example

Base 15 87 + 78 = 110
110 + 011 = 121 2 steps
Base 14 87 + 78 = 111 1 step
Base 13 87 + 78 = 132
132 + 231 = 363 2 steps
Base 12 87 + 78 = 143
143 + 341 = 484 2 steps
Base 11 87 + 78 = 154
154 + 451 = 5A5 2 steps
Base 10 87 + 78 = 165
165 + 561 = 726
726 + 627 = 1353
1353 + 3531 = 4884 4 steps
Base 9 87 + 78 = 176
176 + 671 = 857
857 + 758 = 1726
1762 + 2671 = 7543
7543 + 3457 = 12111
12111 + 11121 = 23232 6 steps
Base 8 illegal ? steps
Base 7 illegal ? steps
Base 6 illegal ? steps
Base 5 illegal ? steps
Base 4 illegal ? steps
Base 3 illegal ? steps
Base 2 illegal ? steps

Input and Output

The input contains several lines, each of them having a legal base 15 integer.

For each line of the input print a single line containing the 14 number of steps in all bases 15 down to 2 separated by a blank space. The number of steps will never be bigger than 100.

Sample Input

87
ED

Sample Output

2 1 2 2 2 4 6 ? ? ? ? ? ? ?
19 ? ? ? ? ? ? ? ? ? ? ? ? ?
import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;
import java.io.StreamTokenizer;

public class Main {
	private static final boolean DEBUG = false;
	private BufferedReader cin;
	private PrintWriter cout;
	private StreamTokenizer tokenizer;
	private String s;
	
	public void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("e:\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
				
			}

			tokenizer = new StreamTokenizer(cin);
			tokenizer.resetSyntax();
			tokenizer.wordChars('a', 'z');
			tokenizer.wordChars('A', 'Z');
			tokenizer.wordChars('0', '9');
			tokenizer.wordChars(128 + 32, 255);
			tokenizer.whitespaceChars(0, ' ');
			tokenizer.commentChar('/');
			tokenizer.quoteChar('"');
			tokenizer.quoteChar('\'');
			
			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String next() 
	{
		try {
			 tokenizer.nextToken(); 
			 if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null; 
			 else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) { 
			 	return String.valueOf((int)tokenizer.nval); 
			} else return tokenizer.sval;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public boolean input() 
	{
		s = next();
		if (s == null) return false;
		
		return true;
	}

	private boolean isPalindrom(String s)
	{
		int len = s.length();

		for (int i = 0; i < len / 2; i++) {
			if (s.charAt(i) != s.charAt(len - 1 - i)) return false;
		}

		return true;
	}

	private String add(String a, int base)
	{
		StringBuilder sb = new StringBuilder();
		int len = a.length();
		String b = new StringBuilder(a).reverse().toString();

		int carry = 0;
		for (int i = 0; i < len; i++) {
			char ch1 = a.charAt(len - 1 - i);
			char ch2 = b.charAt(len - 1 - i);
			int num1 = 0, num2 = 0, sum = 0;
			
			if (Character.isDigit(ch1)) {
				num1 = ch1 - '0';
			} else if (Character.isLetter(ch1)) {
				ch1 = Character.toLowerCase(ch1);
				num1 = ch1 - 'a' + 10;
			}

			if (Character.isDigit(ch2)) {
				num2 = ch2 - '0';
			} else if (Character.isLetter(ch2)) {
				ch2 = Character.toLowerCase(ch2);
				num2 = ch2 - 'a' + 10;
			}

			sum = num1 + num2 + carry;
			carry = sum / base;
			sum %= base;

			char ans;
			if (sum < 10) ans = (char)('0' + sum);
			else ans = (char)('A' + (sum - 10));

			sb.append(ans);
		}

		if (carry != 0) {
			char ans;
			if (carry < 10) ans = (char)('0' + carry);
			else ans = (char)('A' + (carry - 10));
			sb.append(ans);
		}
		return sb.reverse().toString();
	}
	
	private int check(String s, int base)
	{
		int len = s.length();
		for (int i = 0; i < len; i++) {
			char ch = s.charAt(i);
			if (Character.isDigit(ch)) {
				int tmp = ch - '0';
				if (tmp >= base) return -1;
			} else if (Character.isLetter(ch)) {
				ch = Character.toLowerCase(ch);
				int tmp = ch - 'a' + 10;
				if (tmp >= base) return -1;
			} else return -1;
		}

		int step = 0;
		while (!isPalindrom(s)) {
			s = add(s, base);
			step++;
		}

		return step;
	}
	
	public void solve() 
	{
		boolean first = true;
		for (int i = 15; i >= 2; i--) {
			int ans = check(s, i);
			if (first) first = false;
			else cout.print(" ");
			
			if (ans == -1) cout.print("?");
			else cout.print(ans);
		}
		cout.println();
		cout.flush();
	}
	
	public static void main(String[] args) 
	{
		Main solver = new Main();
		solver.init();
		
		while (solver.input()) {
			solver.solve();
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kgduu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值