TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
C语言趣味程序百例精解之java实现(28)回文数

程序:
- public class Test28{
- public static void main(String args[]){
- int count=0;
- for(int i=0;i<256;i++){
- if(isPalindrome(i*i))
- System.out.printf("(%d): %d %d
- ",count++,i,i*i);
- }
- }
-
-
- /**
- *是回文数
- */
-
- /** 检验数字是否是回文的 */
- public static boolean isPalindrome(long num) {
- long[] digits = new long[19];
- if (num >= 0 && num <= 9)
- return true;
- int nDigits = 0;
- while (num > 0) {
- digits[nDigits++] = num % 10;
- num /= 10;
- }
- for (int i=0; i< nDigits/2; i++)
- if (digits[i] != digits[nDigits - i - 1])
- return false;
- return true;
- }
- }
- 运行:
- C:at>java Test28
- (0): 0 0
- (1): 1 1
- (2): 2 4
- (3): 3 9
- (4): 11 121
- (5): 22 484
- (6): 26 676
- (7): 101 10201
- (8): 111 12321
- (9): 121 14641
- (10): 202 40804
- (11): 212 44944
复制代码
源码下载:http://file.javaxxz.com/2014/11/14/000836562.zip |
|