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);
}
}