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

来源:互联网 发布:网络的利与弊论文1000 编辑:程序博客网 时间:2024/06/09 19:35

 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 = 110110 + 011 = 121 2 stepsBase 14 87 + 78 = 111 1 stepBase 13 87 + 78 = 132132 + 231 = 363 2 stepsBase 12 87 + 78 = 143143 + 341 = 484 2 stepsBase 11 87 + 78 = 154154 + 451 = 5A5 2 stepsBase 10 87 + 78 = 165165 + 561 = 726726 + 627 = 13531353 + 3531 = 4884 4 stepsBase 9 87 + 78 = 176176 + 671 = 857857 + 758 = 17261762 + 2671 = 75437543 + 3457 = 1211112111 + 11121 = 23232 6 stepsBase 8 illegal ? stepsBase 7 illegal ? stepsBase 6 illegal ? stepsBase 5 illegal ? stepsBase 4 illegal ? stepsBase 3 illegal ? stepsBase 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

87ED

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 花钱如流水的人怎么办 手机贷登录不上怎么办 如果被网上追逃怎么办 5s指纹排线断了怎么办 苹果6s指纹坏了怎么办 苹果的指纹坏了怎么办 苹果7指纹坏了怎么办 苹果6指纹坏了怎么办 指纹浅打不了卡怎么办 我要贷款5万怎么办 拍拍贷一千不还怎么办 牙龈肿里面有脓怎么办 爱奇艺会员账号忘了怎么办 被私立医院坑了怎么办 在医院被坑了怎么办 流产后子宫内膜薄怎么办 人流后内膜过厚怎么办 子宫内膜薄月经量少怎么办 子宫内膜很薄该怎么办 月经量少子宫内膜薄怎么办 子宫内薄没月经怎么办 感冒20多天不好怎么办 皮肤干燥又痒怎么办了 眼周皮肤很干怎么办 产后掉头发很厉害怎么办 班上学生很吵怎么办 进了网贷黑名单怎么办 预约了挂号没去怎么办 吃完米索手心痒怎么办 三岁宝宝湿疹了怎么办 割完剥皮后水肿怎么办 微医预约挂号后怎么办 人流后出现腰疼怎么办 生育服务单丢了怎么办 客厅地面砖坏了怎么办 门锁很涩不好开怎么办 胶水粘到手机上怎么办 沾鞋胶水粘到手怎么办 轮胎内衬板坏了怎么办 轿车后减震异响怎么办 租的房间隔音差怎么办