思路:
import java.math.BigInteger;
import java.util.ArrayList;
public class TestOne {
private static class price{
int p;//原价
int sub;//翻转价格-原价
public price(int p, int sub) {
super();
this.p = p;
this.sub = sub;
}
}
public static void main(String[] args) {
ArrayList<price> a1 = new ArrayList<price>();//存放所有翻转后 赔200左右的价格
ArrayList<price> a2 = new ArrayList<price>(); //存放所有翻转后赚800左右的价格
//1.遍历所有的四位数
for(int i=1000;i<10000;i++) {
//1.1 转化为字符串,对字符串翻转
String str = ""+i;
//筛选。不能包含数字3 4 7
if(str.contains("3")||str.contains("4")||str.contains("7")) {
continue;
}
String Rstr = reverse(str);
//1.2 判断翻转后的价格与原价的差值
int Rp = Integer.parseInt(Rstr);//翻转后的价格
if(Rp-i>-300 && Rp-i<-200) { //赔200多
a1.add(new price(i,Rp-i));
}
if(Rp-i>800 && Rp-i<900) {//赚800多
a2.add(new price(i, Rp-i));
}
}
//2.遍历a1、a2,查找共赚558的赔钱的正确价格
for(price p1:a1) {
for(price p2:a2) {
if((p1.sub+p2.sub)==558) {
//2.1 找到后,打印输出
System.out.println(p1.p+" "+p1.sub); //赔钱的原价,赔的钱数
System.out.println(p2.p+" "+p2.sub); //赚钱的原价,赚的钱数
System.out.println();
}
}
}
}
/*
* 将数字字符进行翻转,
* 1.每次遇到6时,翻转后将其变为9
* 2.每次遇到9时,翻转后将其变为6
* */
private static String reverse(String str) {
char[] Rstr = new char[str.length()];//存放翻转后的字符串
for(int i=str.length()-1,j=0;i>=0;i--,j++) {
char c = str.charAt(i);
if(c=='6') {
Rstr[j]='9';
}else if(c=='9') {
Rstr[j]='6';
}else {
Rstr[j]=c;
}
}
return new String(Rstr);
}
}
可以得出结果是:9088