/** * @version 1.0 * @auther 孙沐华 * StringBuffer 跟StingBuilder 字符串内容是存在 char[] value,所以在变化(增加/删除) * 中不用每次都更换地址(即不是每次都创建新对象) 所以说在用equals比较回文时没用 */ @SuppressWarnings({"all"}) public class test02 { private static int sum=0; public static void main(String[] args) { for (int i = 2022; i <=200222 ; i++) { if (f1(i)&&f2(i)){ sum++; } } System.out.println(sum); } public static Boolean f1(int n){//判断回文 String str =""+n; char[] chars = str.toCharArray(); StringBuilder stringBuilder = new StringBuilder(str); if (stringBuilder.toString().equals(stringBuilder.reverse().toString())){ return true; }else return false; // for (int i = 0; i <chars.length ; i++) { // if(chars[i]!=chars[chars.length-i-1]){ // return false; // } // } // return true; } public static Boolean f2(int n){//判断单调性 String str=""+n; char[] chars = str.toCharArray(); for (int i = 0; i <chars.length/2 +(chars.length%2)-1; i++) { if (chars[i]>chars[i+1]){ return false; } } return true; } }
标签:return,文山,int,每次,static,public From: https://www.cnblogs.com/xingxingbclg/p/17201364.html