中国人寿研发中心笔试JAVA开发

it2023-09-27  74

目录

1.反转字符串(ac)2.回文数(50%)


1.反转字符串(ac)

package 笔试题目.人寿; import java.util.Scanner; public class fanzhaun { public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNext()){ String s=sc.nextLine(); String a=""; int l=0; int r=s.length()-1; while(l<=r){ a+=s.charAt(r); r--; } System.out.println(a); } } }

2.回文数(50%)

package 笔试题目.人寿; import java.util.*; public class huiwen { static int count = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); // while (sc.hasNext()) { int N = sc.nextInt(); String M= sc.next(); int c =Integer.valueOf(M,N); // System.out.println(c); while (!isHuiwen(c)) { int c1 = reverse(c); c=c+c1; // System.out.println(c); count++; } if(count<20) { System.out.println("STEP=" + count); }else{ System.out.println("Impossible"); } } // } public static int reverse(int x) { int m = 0; while (x != 0) { m = m * 10 + x % 10; x = x / 10; } return m; } public static boolean isHuiwen(int x) { if (x < 0) return false; String s = String.valueOf(x); for (int i = 0; i < s.length() / 2; i++) { if (s.charAt(i) != s.charAt(s.length() - i - 1)) return false; } return true; } }
最新回复(0)