解题代码部分来自网友,如果有不对的地方,欢迎各位大佬评论
题目1、 填算式
【结果填空】 (满分11分)
看这个算式:
☆☆☆ + ☆☆☆ = ☆☆☆
如果每个五角星代表 1 ~ 9 的不同的数字。
这个算式有多少种可能的正确填写方法?
173 + 286 = 459
295 + 173 = 468
173 + 295 = 468
183 + 492 = 675
以上都是正确的填写法!
注意:
111 + 222 = 333 是错误的填写法!
因为每个数字必须是不同的!
也就是说:1~9中的所有数字,每个必须出现且仅出现一次!
注意:
不包括数字“0”!
注意:
满足加法交换率的式子算两种不同的答案。
所以答案肯定是个偶数!
注意:
只要求计算不同的填法的数目
不要求列出所有填写法
更不要求填写源代码!
答案不要写在这里,请写在“解答.txt”中!
336
public class Main {
public static int count = 0;
public static void swap(int[] A, int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public static void dfs(int[] A, int step) {
if(step == A.length) {
int a = A[0]*100 + A[1]*10 + A[2];
int b = A[3]*100 + A[4]*10 + A[5];
int c = A[6]*100 + A[7]*10 + A[8];
if(a + b == c)
count++;
return;
} else {
for(int i = step;i < A.length;i++) {
swap(A, i, step);
dfs(A, step + 1);
swap(A, i, step);
}
}
}
public static void main(String[] args) {
int[] A = {1,2,3,4,5,6,7,8,9};
dfs(A, 0);
System.out.println(count);
}
}
题目2、 提取子串
【代码填空】(满分16分)
串“abcba”以字母“c”为中心左右对称;串“abba” 是另一种模式的左右对称。这两种情况我们都称这个串是镜像串。特别地,只含有1个字母的串,可以看成是第一种模式的镜像串。
一个串可以含有许多镜像子串。我们的目标是求一个串的最大镜像子串(最长的镜像子串),如果有多个最大镜像子串,对称中心靠左的优先选中。例如:“abcdeefghhgfeiieje444k444lmn”的最大镜像子串是:“efghhgfe”
下面的静态方法实现了该功能,请仔细阅读并分析代码,填写空白处的代码,使得程序的逻辑合理,结果正确。
// 求最大(长度最大)镜像对称子串
public static String getMaxMirrorString(String s)
{
String max_s = ""; // 所求的最大对称子串
for(int i=0; i<s.length(); i++)
{
// 第一种对称模式
int step = 1;
try{
for(;;)
{
if(s.charAt(i-step) != s.charAt(i+step)) break;
step++;
}
}catch(Exception e){}
String s1 = s.substring(_____________________________); // 填空1
// 第二种对称模式
step = 0;
try{
for(;;)
{
if(_________________________________) break; // 填空2
step++;
}
}catch(Exception e){}
String s2 = s.substring(i-step+1,i