2013省赛-颠倒的价格

思路:

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 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值