java特殊回文,算法笔记_038:特殊回文数(Java)

packagecom.liuzhen.systemExe;importjava.util.HashSet;importjava.util.Scanner;importjava.util.Set;public classMain{public void getReverseN(intn){

Set set = new HashSet();for(int i = 0;i < 10;i++){int x1 =i;for(int j = 0;j < 10;j++){int x2 =j;for(int k = 0;k < 10;k++){int x3 =k;if(2*(x1+x2)+x3 ==n){char[] temp = new char[5];

temp[0] = (char) (x1 + 48);

temp[1] = (char) (x2 + 48);

temp[2] = (char) (x3 + 48);

temp[3] = (char) (x2 + 48);

temp[4] = (char) (x1 + 48);

String tempS1=String.valueOf(temp);

set.add(tempS1);

temp[0] = (char) (x2 + 48);

temp[1] = (char) (x1 + 48);

temp[2] = (char) (x3 + 48);

temp[3] = (char) (x1 + 48);

temp[4] = (char) (x2 + 48);

String tempS2=String.valueOf(temp);

set.add(tempS2);

}if(2*(x1+x2+x3) ==n){char[] temp = new char[6];

temp[0] = (char) (x1 + 48);

temp[1] = (char) (x2 + 48);

temp[2] = (char) (x3 + 48);

temp[3] = (char) (x3 + 48);

temp[4] = (char) (x2 + 48);

temp[5] = (char) (x1 + 48);

String tempS1=String.valueOf(temp);

set.add(tempS1);

temp[0] = (char) (x1 + 48);

temp[1] = (char) (x3 + 48);

temp[2] = (char) (x2 + 48);

temp[3] = (char) (x2 + 48);

temp[4] = (char) (x3 + 48);

temp[5] = (char) (x1 + 48);

String tempS2=String.valueOf(temp);

set.add(tempS2);

temp[0] = (char) (x2 + 48);

temp[1] = (char) (x1 + 48);

temp[2] = (char) (x3 + 48);

temp[3] = (char) (x3 + 48);

temp[4] = (char) (x1 + 48);

temp[5] = (char) (x2 + 48);

String tempS3=String.valueOf(temp);

set.add(tempS3);

temp[0] = (char) (x2 + 48);

temp[1] = (char) (x3 + 48);

temp[2] = (char) (x1 + 48);

temp[3] = (char) (x1 + 48);

temp[4] = (char) (x3 + 48);

temp[5] = (char) (x2 + 48);

String tempS4=String.valueOf(temp);

set.add(tempS4);

temp[0] = (char) (x3 + 48);

temp[1] = (char) (x2 + 48);

temp[2] = (char) (x1 + 48);

temp[3] = (char) (x1 + 48);

temp[4] = (char) (x2 + 48);

temp[5] = (char) (x3 + 48);

String tempS5=String.valueOf(temp);

set.add(tempS5);

temp[0] = (char) (x3 + 48);

temp[1] = (char) (x1 + 48);

temp[2] = (char) (x2 + 48);

temp[3] = (char) (x2 + 48);

temp[4] = (char) (x1 + 48);

temp[5] = (char) (x3 + 48);

String tempS6=String.valueOf(temp);

set.add(tempS6);

}

}

}

}int len =set.size();

String[] result= newString[len];int i = 0;for(String tem : set)

result[i++] =tem;int[] rI =getSort(result);for(i = 0;i < len;i++){int k =getMinI(rI);

System.out.println(result[k]);

rI[k]=len;

}

}public int[] getSort(String[] A){int len =A.length;int[] result = new int[len];int[] tempResult = new int[len];for(int i = 0;i < len;i++){

tempResult[i]=Integer.parseInt(A[i]);

}for(int i = 0;i < len;i++){int count = 0;for(int j = 0;j < len;j++){if(tempResult[i] >tempResult[j])

count++;

}

result[i]=count;

}returnresult;

}public int getMinI(int[] A){int result = 0;for(int i = 0;i < A.length;i++){if(A[result] >A[i])

result=i;

}returnresult;

}public static voidmain(String[] args){//long t1 = System.currentTimeMillis();

Main test = newMain();

Scanner in= newScanner(System.in);

System.out.println("请输入一个10进制整数:");int n =in.nextInt();

test.getReverseN(n);//long t2 = System.currentTimeMillis();//System.out.println("耗时:"+(t2-t1)+"毫秒");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值