蓝桥杯2014年以前JAVA历年真题及答案整理——猜算式
来源:互联网 发布:管线机什么牌子好 知乎 编辑:程序博客网 时间:2024/04/29 18:09
看下面的算式:
□□ x □□ = □□ x □□□
它表示:两个两位数相乘等于一个两位数乘以一个三位数。
如果没有限定条件,这样的例子很多。
但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。
该算式中1至9的每个数字出现且只出现一次!
比如:
46 x79 = 23 x 158
54 x69 = 27 x 138
54 x93 = 27 x 186
.....
请编程,输出所有可能的情况!
注意:
左边的两个乘数交换算同一方案,不要重复输出!
不同方案的输出顺序不重要
import java.util.ArrayList;import java.util.List;import java.util.Vector;public class Main {//y j gpublic 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) {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));}Main.filteredNonRedundantResults=new ArrayList<Vector<Character>>();Main.fullPermutation(sourse, result);for (int i = 0; i < Main.filteredNonRedundantResults.size(); i++) {Main.print(Main.filteredNonRedundantResults.get(i));System.out.println();}}}
0 0
- 蓝桥杯2014年以前JAVA历年真题及答案整理——猜算式
- 蓝桥杯2014年以前JAVA历年真题及答案整理——字符排序
- 蓝桥杯2014年以前JAVA历年真题及答案整理——Excel地址转换
- 蓝桥杯2014年以前JAVA历年真题及答案整理——数列排序
- 蓝桥杯2014年以前JAVA历年真题及答案整理——十六进制转八进制
- 蓝桥杯2014年以前JAVA历年真题及答案整理——十六进制转十进制
- 蓝桥杯2014年以前JAVA历年真题及答案整理——十进制转十六进制
- 蓝桥杯2014年以前JAVA历年真题及答案整理——特殊回文数
- 蓝桥杯2014年以前JAVA历年真题及答案整理——回文数
- 蓝桥杯2014年以前JAVA历年真题及答案整理——特殊的数字
- 蓝桥杯2014年以前JAVA历年真题及答案整理——杨辉三角形
- 蓝桥杯2014年以前JAVA历年真题及答案整理——查找整数
- 蓝桥杯2014年以前JAVA历年真题及答案整理——数列特征
- 蓝桥杯2014年以前JAVA历年真题及答案整理——字母图形
- 蓝桥杯2014年以前JAVA历年真题及答案整理——01字串
- 蓝桥杯2014年以前JAVA历年真题及答案整理——闰年判断
- 蓝桥杯2014年以前JAVA历年真题及答案整理——A+B问题
- 蓝桥杯2014年以前JAVA历年真题及答案整理——求和公式
- 定制自己的ide 基于vim+cscope+ctag+talist
- Microsoft SQL Server 管理 (常用管理及维护命令)
- 字符串匹配 之 KMP
- 清除Visual Studio查找的历史记录
- 使用Bootstrap3和Ladda UI实现的多种按钮“加载中”效果体验
- 蓝桥杯2014年以前JAVA历年真题及答案整理——猜算式
- 当前网站的mssql数据库连接数
- 数据结构基础(8)------------冒泡排序(Bubble Sort)
- Win7 64位系统 AddGraphToRot 调用成功,Graphedit 远程连接不上.
- MS SQL Server 获得所有表的表记录,和使用空间的SQL
- 一些随感
- GridView使用LinkButton和Button两种方式的删除确认
- 大气,才能成就大器
- Windows MDL原理总结