梅森素数、颠倒价牌

3 篇文章 0 订阅
3 篇文章 0 订阅
package first_projects;

import java.util.ArrayList;

public class _04颠倒价牌 {
	
	public static void main(String[] args) {
	    System.out.println(reverse("1958"));
	    ArrayList<Price> a1 = new ArrayList<Price>();
	    ArrayList<Price> a2 = new ArrayList<Price>();
	    //  枚举四位数,简单筛选
	    for (int i = 1000; i < 10000; i++) {
	      //  将其颠倒,和原价做差,将赔200多的放入一个集合,将赚800多的放入一个集合
	      String s = "" + i;
	      if (s.contains("3") || s.contains("4") || s.contains("7")) continue;
	      String re_s = reverse(s);
	      int i1 = Integer.parseInt(re_s);
	      int plus = i1 - i;
	      if (plus < -200 && plus > -300) a1.add(new Price(i, plus));
	      if (plus < 900 && plus > 800) a2.add(new Price(i, plus));
	    }

	    //  遍历两个集合两两组合,检查是否相加为558
	    for (Price p1 : a1
	        ) {
	      for (Price p2 : a2
	          ) {
	        if (p1.plus + p2.plus == 558) {
	          //  输出结果
	          System.out.println(p1.p + " " + p1.plus);
	          System.out.println(p2.p + " " + p2.plus);
	        }
	      }
	    }
	  }

	  private static String reverse(String s) {
	    char[] ans = new char[s.length()];
	    for (int i = s.length() - 1, j = 0; i >= 0; i--, j++) {
	      char c = s.charAt(i);
	      if (c == '6') ans[j] = '9';
	      else if (c == '9') ans[j] = '6';
	      else ans[j] = c;
	    }
	    return new String(ans);
	  }

	  private static class Price {
	    int p;//原价
	    int plus;//颠倒价-原价

	    public Price(int p, int plus) {
	      this.p = p;
	      this.plus = plus;
	    }
	  }

}
package first_projects;

/**
 * 标题: 振兴中华
 * 
 * 小明参加了学校的趣味运动会,其中的一个项目是:跳格子。
 * 
	从我做起振
	我做起振兴
	做起振兴中
	起振兴中华
 * 
 * 比赛时,先站在左上角的写着“从”字的格子里,可以横向或纵向跳到相邻的格子里,但不能跳到对角的格子或其它位置。一直要跳到“华”字结束。
 * 
 * 
 * 要求跳过的路线刚好构成“从我做起振兴中华”这句话。
 * 
 * 请你帮助小明算一算他一共有多少种可能的跳跃路线呢?
 * 
 * 答案是一个整数。
 * 
 * @author lvyuma
 *
 */

public class Second_Question {

	public static void main(String[] args) {

		int res = method(0, 0);
		System.out.println(res);

	}
	
	private static int method(int i, int j) {
		// TODO Auto-generated method stub
		if (i == 3 || j == 4) {
			System.out.println(i+" "+j);
			if (i==3 && j==4) {
				System.err.println(i+" "+j);
			}
			return 1; 
		}
		
		return method(i + 1, j) + method(i, j + 1);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值