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

程序:
- public class Test85{
- public static void main(String args[]){
- long n=Integer.parseInt(args[0]);
- int count=0;
- long m=0;
- System.out.printf("回文数的产生过程
- ");
- while(!isPalindrome(m=re(n)+n)){
- if(m>=Long.MAX_VALUE){
- System.out.printf("溢出,程序中断
- ");
- break;
- }
- else{
- System.out.printf("[%d]:%d+%d=%d
- ",++count,n,re(n),m);
- n=m;
- }
- }
- System.out.printf("[%d]:%d+%d=%d
- ",++count,n,re(n),m);
- System.out.printf("结束");
- }
- //反转数字
- public static long re(long a){
- long t=0;
- while(a>0){
- t=t*10+a%10;
- a/=10;
- }
-
- return t;
- }
-
- /** 检验数字是否是回文的 */
- public static boolean isPalindrome(long num) {
- if(re(num)==num){
- return true;
- }else{
- return false;
- }
- }
- }
- C:at>java Test86 1993
- 回文数的产生过程
- [1]:1993+3991=5984
- [2]:5984+4895=10879
- [3]:10879+97801=108680
- [4]:108680+86801=195481
- [5]:195481+184591=380072
- [6]:380072+270083=650155
- [7]:650155+551056=1201211
- [8]:1201211+1121021=2322232
- 结束
- C:at>java Test86 1994
- 回文数的产生过程
- [1]:1994+4991=6985
- [2]:6985+5896=12881
- [3]:12881+18821=31702
- [4]:31702+20713=52415
- [5]:52415+51425=103840
- [6]:103840+48301=152141
- [7]:152141+141251=293392
- 结束
复制代码
源码下载:http://file.javaxxz.com/2014/11/27/000516281.zip |
|