首页 > 学院 > 开发设计 > 正文

16蓝桥杯基础—特殊回文数

2019-11-06 06:11:27
字体:
来源:转载
供稿:网友
import java.util.Scanner;/*特殊回文数 * 问题描述  123321是一个非常特殊的数,它从左边读和从右边读是一样的。  输入一个正整数n, 编程求所有这样的五位和六位十进制数,满足各位数字之和等于n 。输入格式  输入一行,包含一个正整数n。输出格式  按从小到大的顺序输出满足条件的整数,每个整数占一行。样例输入52样例输出899998989989998899数据规模和约定  1<=n<=54。*/public class TeShu_HuiWenShu {public static void main(String[] args){Scanner s = new Scanner(System.in);int n = s.nextInt();if(n>=1 && n<=54){/*//5位数搜索for(int i=10000; i<100000; i++){String str = String.valueOf(i);char arr[] = new char[5];arr = str.toCharArray();boolean flag = charAt(arr);if(flag==true){int sum = arr[0]+arr[1]+arr[2]+arr[3]+arr[4];if(sum == n){System.out.PRintln(i);}}}//6位数搜索for(int i=100000; i<1000000; i++){String str = String.valueOf(i);char arr[] = new char[6];arr = str.toCharArray();boolean flag = charAt(arr);if(flag==true){int sum = arr[0]+arr[1]+arr[2]+arr[3]+arr[4]+arr[5];if(sum == n){System.out.println(i);}}}*///5位数搜索for(int i=10; i<100; i++){String str1 = String.valueOf(i);char arr1[] = new char[2];arr1 = str1.toCharArray();for(int j=10; j<100; j++){String str2 = String.valueOf(j);char arr2[] = new char[2];arr2 = str2.toCharArray();if(arr1[0]==arr2[1] && arr1[1]==arr2[0]){for(int k=0; k<10; k++){int arr3[] = new int[2];for(int l=0; l<arr3.length; l++){String str3 = String.valueOf(arr1[l]);arr3[l] = Integer.valueOf(str3);}int sum = (arr3[0]+arr3[1])*2+k;if(sum == n){int result = arr3[0]*10000+arr3[1]*1000+k*100+arr3[1]*10+arr3[0];System.out.println(result);}}}}}//6位数搜索for(int i=100; i<1000; i++){String str1 = String.valueOf(i);char arr1[] = new char[3];arr1 = str1.toCharArray();for(int j=100; j<1000; j++){String str2 = String.valueOf(j);char arr2[] = new char[3];arr2 = str2.toCharArray();if(arr1[0]==arr2[2] && arr1[1]==arr2[1] && arr1[2]==arr2[0]){int arr3[] = new int[3];for(int k=0; k<arr3.length; k++){String str3 = String.valueOf(arr1[k]);arr3[k] = Integer.valueOf(str3);}int sum = (arr3[0]+arr3[1]+arr3[2])*2;if(sum == n){int result = arr3[0]*100000+arr3[1]*10000+arr3[2]*1000+arr3[2]*100+arr3[1]*10+arr3[0];System.out.println(result);}}}}}else{System.out.println("0");}}public static boolean charAt(char arr[]){boolean flag = false;if(arr.length==5){if(arr[0]==arr[4] && arr[1]==arr[3]){flag = true;}}if(arr.length==6){if(arr[0]==arr[5] && arr[1]==arr[4] && arr[2]==arr[3]){flag = true;}}return flag;}}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表