蓝桥杯_基础_特殊回文数

问题描述
  123321是一个非常特殊的数,它从左边读和从右边读是一样的。
  输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。
输入格式
  输入一行,包含一个正整数n。
输出格式
  按从小到大的顺序输出满足条件的整数,每个整数占一行。
样例输入
52
样例输出
899998
989989
998899
数据规模和约定
  1<=n<=54。

解题源代码如下:

import java.util.*;
//创建于20190304
//为算法刷题模板

class Main {
        private static Scanner sc = new Scanner(System.in);

        public static void main (String[] args){
                int com = sc.nextInt();
                Vector<Integer> result_ini = new Vector<Integer>();
                //定义可变字符串数组来存储结果 
                for(int i = 10000;i<1000000;i++){
                        if(i<100000&&five(i)&&(i%10+i/10%10+i/100%10+i/1000%10+i/10000)==com){
                                result_ini.add(i);
                        }else if(i>99999&&six(i)&&(i%10+i/10%10+i/100%10+i/1000%10+i/10000%10+i/100000)==com){
                                result_ini.add(i);
                        }
                }
                for(int temp:sort(result_ini)){
                        System.out.println(temp);
                }
        }

        public static boolean five(int temp){//五位十进制数
                String str = temp +"";
                return (str.charAt(0)==str.charAt(4)&&str.charAt(1)==str.charAt(3))?true:false;
        }

        public static boolean six(int temp){//六位十进制数
                String str = temp +"";
                return (str.charAt(0)==str.charAt(5)&&str.charAt(1)==str.charAt(4)&&str.charAt(2)==str.charAt(3))?true:false;
        }

        public static Integer[] sort(Vector<Integer> vc){
                //Object temp[] = vc.toArray();//接收并转化为Integer数组
                Integer result[] = new Integer[vc.size()];
                vc.toArray(result);
                //for(int i = 0;i<temp.length;i++){
                //      result[i] = (int)temp[i];
                //}
                Arrays.sort(result);//直接调用内部方法进行排序
                return result;
        }
}

解题思路:分别设置五位十进制数和六位十进制数回文数判断方法

然后再通(i>99999&&six(i)&&(i%10+i/10%10+i/100%10+i/1000%10+i/10000%10+i/100000)==com)以及(i>99999&&six(i)&&(i%10+i/10%10+i/100%10+i/1000%10+i/10000%10+i/100000)==com)来判断各位数之和是否等于键入的数,如果相等则把此数添加进vector,然后通过排序方法和转换方法转换成排好序的数组并且输出

猜你喜欢

转载自www.cnblogs.com/lavender-pansy/p/10487656.html