Java算法编程题系列之 3、猜算式
2016-02-19 23:07
483 查看
Java算法编程题系列之 3、猜算式
题目:
看下面的算式:
□□ x □□ = □□ x □□□
它表示:两个两位数相乘等于一个两位数乘以一个三位数。
如果没有限定条件,这样的例子很多。
但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。
该算式中1至9的每个数字出现且只出现一次!
比如:
46 x 79 = 23 x 158
54 x 69 = 27 x 138
54 x 93 = 27 x 186
…..
请编程,输出所有可能的情况!
注意:
左边的两个乘数交换算同一方案,不要重复输出!
不同方案的输出顺序不重要
题目解答:
题目:
看下面的算式:
□□ x □□ = □□ x □□□
它表示:两个两位数相乘等于一个两位数乘以一个三位数。
如果没有限定条件,这样的例子很多。
但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。
该算式中1至9的每个数字出现且只出现一次!
比如:
46 x 79 = 23 x 158
54 x 69 = 27 x 138
54 x 93 = 27 x 186
…..
请编程,输出所有可能的情况!
注意:
左边的两个乘数交换算同一方案,不要重复输出!
不同方案的输出顺序不重要
题目解答:
import java.util.*; public class Question3 { public static long count = 0; public static List<Vector<Character>> filteredNonRedundantResults; /* 用于存放全排列的结果 */ private static boolean isfilter(Vector<Character> result) { /* 按照要求条件进行过滤 判断 */ int a = (result.elementAt(0) - '0') * 10 + (result.elementAt(1) - '0'); int b = (result.elementAt(2) - '0') * 10 + (result.elementAt(3) - '0'); int c = (result.elementAt(4) - '0') * 10 + (result.elementAt(5) - '0'); int d = (result.elementAt(6) - '0') * 100 + (result.elementAt(7) - '0') * 10 + (result.elementAt(8) - '0'); if (a*b == c*d) { return true; } return false; } public static void print(Vector<Character> vector) { /* 格式化输出结果 */ System.out.printf("%c%c x %c%c = %c%c x %c%c%c",vector.elementAt(0),vector.elementAt(1),vector.elementAt(2),vector.elementAt(3),vector.elementAt(4),vector.elementAt(5),vector.elementAt(6),vector.elementAt(7),vector.elementAt(8)); } private static void fullPermutation(Vector<Character> sourse, Vector<Character> result) { /* 全排列 */ if (sourse.size() == 0 && isfilter(result)) { boolean exit = false; for (int i = 0;i < filteredNonRedundantResults.size();i++) { int ra = (result.elementAt(0) - '0') * 10 + (result.elementAt(1) - '0'); int rb = (result.elementAt(2) - '0') * 10 + (result.elementAt(3) - '0'); int fa = (filteredNonRedundantResults.get(i).elementAt(0) - '0') * 10 + (filteredNonRedundantResults.get(i).elementAt(1) - '0'); int fb = (filteredNonRedundantResults.get(i).elementAt(2) - '0') * 10 + (filteredNonRedundantResults.get(i).elementAt(3) - '0'); if (ra==fb && rb==fa) { //去除重复的数据 exit = true; break; } } if (exit==false) {//结果中不存在该组数据,添加进结果中 filteredNonRedundantResults.add(new Vector<Character> (result)); } return; } for (int i = 0; i < sourse.size();i++) { result.add(sourse.elementAt(i)); sourse.remove(i); fullPermutation(sourse,result); sourse.add(i, result.elementAt(result.size() - 1)); result.remove(result.size() - 1); } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = 9; Vector<Character> sourse = new Vector<Character> (); Vector<Character> result = new Vector<Character> (); for(int i =1 ;i <= n;i++) { sourse.add((char)('0' + i)); } Question3.filteredNonRedundantResults = new ArrayList<Vector<Character>> (); Question3.fullPermutation(sourse, result); for(int i = 0;i < Question3.filteredNonRedundantResults.size();i++) { Question3.print(Question3.filteredNonRedundantResults.get(i)); System.out.println(); } } }
相关文章推荐
- 关于Java小项目——图书管理系统的总结
- 关于研究Java中GlassPane的心得
- 定制化Azure站点Java运行环境(4)
- [Introduction to programming in Java 笔记] 1.3.9 Factoring integers 素因子分解
- 从头认识Spring-2.8 基于java注解的配置(基本没有xml)
- Java [Leetcode 228]Summary Ranges
- 关于 Eclipse
- 深入理解Spring IOC
- 定制化Azure站点Java运行环境(3)
- 定制化Azure站点Java运行环境(2)
- SQL Developer报错:Unable to find a Java Virtual Machine解决办法
- LEEDCODE 12 Integer to Roman (JAVA题解)
- Java反射机制
- spring mvc中的@PathVariable[动态参数类似于spring中的通配符功能]
- Java [Leetcode 204]Count Primes
- java基础—java的Io操作学习(3)
- java多线程设计模
- andriod Java中度转度分秒
- CS 106A JAVA学习
- Eclipse简介、下载安装与插件