UVa290 - Palindroms <---> smordnilaP(进制转换、回文)
来源:互联网 发布:网络的利与弊论文1000 编辑:程序博客网 时间:2024/06/09 19:35
Palindroms 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();}}}
- UVa290 - Palindroms <---> smordnilaP(进制转换、回文)
- 1019 进制转换 回文数
- pat1019 进制转换+回文数 部分正确
- USACO 1.2 Palindromic Squares (进制转换,回文)
- USACO 1.2 Dual Palindromes (回文,进制转换)
- USACO-Section1.2 Palindromic Squares [回文数][进制转换]
- USACO-Section1.2 Dual Palindromes [回文数][进制转换]
- USACO Prime Palindroms
- 进制的转换——进制和回文判断相结合
- 进制回文数
- USACO之Section1.2.4 Palindromic Squares[回文数][进制转换]
- HDU-2029~2032(回文串判断,汉字统计,进制转换,杨辉三角)
- 转换到回文字符串
- 转换到回文字符串
- 【PAT】进制回文数
- 转换成回文串-LintCode
- 1.2 dualpal 还是进制和回文
- 学校1008: X进制回文数
- 递归读取xml 文件;
- 模态窗口阻塞程序等待后台处理结果
- 【转】Visual Studio 非常实用的调试技巧
- Java面试笔记
- size_t
- UVa290 - Palindroms <---> smordnilaP(进制转换、回文)
- LUA学习笔记--基本函数库
- 2.原子--深入浅出java原子操作
- codeforces 455B A Lot of Games
- 用Bitmap加载图片资源时,报错java.lang.OutOfMemoryError: bitmap size exceeds VM budget的解决办法
- C/C++中的位运算操作
- LeetCode 058 Length of Last Word
- 初识Cura3D打印开源项目
- Value must be an existing directory配置tomcat问题