java实现第六届蓝桥杯三羊献瑞
2024-09-07 16:47:48
三羊献瑞
题目描述
观察下面的加法算式:
祥 瑞 生 辉
- 三 羊 献 瑞
三 羊 生 瑞 气
(如果有对齐问题,可以参看【图1.jpg】)
其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。
请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。
结果:1085
import java.util.Arrays;
public class Main {
public static boolean judge(int[] A) {
int[] tempA = new int[A.length];
for(int i = 0;i < A.length;i++)
tempA[i] = A[i];
Arrays.sort(tempA); //对tempA元素进行从小到大排序
for(int i = 1;i < tempA.length;i++) {
if(tempA[i - 1] == tempA[i])
return false;
}
return true;
}
public static boolean judge1(int[] A, int[] B) {
int[] temp = new int[A.length + B.length - 1];
int i = 0;
for(;i < A.length;i++)
temp[i] = A[i];
for(;i < temp.length;i++)
temp[i] = B[i - A.length];
Arrays.sort(temp);
for(i = 1;i < temp.length;i++) {
if(temp[i - 1] == temp[i])
return false;
}
return true;
}
public static void main(String[] args) {
for(int i = 1000;i <= 9999;i++) {
int[] A = new int[4];
A[0] = i / 1000;
A[1] = i / 100 % 10;
A[2] = i / 10 % 10;
A[3] = i % 10;
if(judge(A) == false)
continue;
for(int j = 1000;j <= 9999;j++) {
int[] B = new int[4];
B[0] = j / 1000;
B[1] = j / 100 % 10;
B[2] = j / 10 % 10;
B[3] = j % 10;
if(judge(B) == false)
continue;
if(B[3] != A[1])
continue;
if(judge1(A, B) == false)
continue;
int temp = i + j;
if(temp < 9999 || temp > 99999)
continue;
int[] C = new int[5];
C[0] = temp / 10000;
C[1] = temp / 1000 % 10;
C[2] = temp / 100 % 10;
C[3] = temp / 10 % 10;
C[4] = temp % 10;
if(C[0] == B[0] && C[1] == B[1] && C[2] == A[2] && C[3] == A[1]) {
if(C[4] != A[0] && C[4] != A[1] && C[4] != A[2] && C[4] != A[3]) {
if(C[4] != B[0] && C[4] != B[1] && C[4] != B[2] && C[4] != B[3])
System.out.println("i = "+i+", j = "+j+", temp = "+temp);
}
}
}
}
}
}
最新文章
- C#小方法PadLeft 和 PadRight
- #研发中间件介绍#定时任务调度与管理JobCenter
- xss跨站实例总结
- 设置TextView下划线并响应点击事件(SpannableString)
- php删除html标签的三种解决方法
- json,serialze之格式
- spring--处理器拦截器详解——跟着开涛学SpringMVC
- 一个很cool的C#的高性能数学库
- Java高精度学习第二弹——求N!
- 转:TestLink1.9.3测试用例:Excel转换XML工具<;二>;实现代码
- UILocalNotification
- Windows平台内核级文件访问
- 让Linux开机运行命令
- 用windows性能监视器监控sqlserver的常见指标
- JPA 系列教程11-复合主键-2个@Id
- LeetCode 40. Combination Sum II (组合的和之二)
- window下eclipse4.5+hadoop2.6.1开发环境配置
- 构建FTP服务
- (八)lambda、列表生成式、字典转list排序
- 曙光服务器挂载EMC存储