Java--蓝桥杯真题(二)

题目1:


标题:分机号

X老板脾气古怪,他们公司的电话分机号都是3位数,老板规定,所有号码必须是降序排列,且不能有重复的数位。比如:

751,520,321 都满足要求,而,
766,918,201 就不符合要求。

现在请你计算一下,按照这样的规定,一共有多少个可用的3位分机号码?

请直接提交该数字,不要填写任何多余的内容。

解决方案:

 1 package test;
 2 
 3 public class fenjihao {
 4     public static void main(String args[]) {
 5         int num = 0;
 6 
 7         here: for (int i = 0; i <= 9; i++) {
 8             for (int j = 0; j <= 9; j++) {
 9                 for (int n = 0; n <= 9; n++) {
10                     if (i > j && j > n) {
11                         num++;
12 
13                         // break here;
14 
15                     }
16                 }
17 
18             }
19 
20         }
21         System.out.println(num);
22 
23     }
24 
25 }

题目2:


循环节长度

两个整数做除法,有时会产生循环小数,其循环部分称为:循环节。
比如,11/13=6=>0.846153846153..... 其循环节为[846153] 共有6位。
下面的方法,可以求出循环节的长度。

请仔细阅读代码,并填写划线部分缺少的代码。

public static int f(int n, int m)
{
n = n % m;
Vector v = new Vector();

for(;;)
{
v.add(n);
n *= 10;
n = n % m;
if(n==0) return 0;
if(v.indexOf(n)>=0) _________________________________ ; //填空
}
}

注意,只能填写缺少的部分,不要重复抄写已有代码。不要填写任何多余的文字。

解决方案:

Java中字符串中子串的查找共有四种方法,如下:
1、int indexOf(String str) :返回第一次出现的指定子字符串在此字符串中的索引。 
2、int indexOf(String str, int startIndex):从指定的索引处开始,返回第一次出现的指定子字符串在此字符串中的索引。 
3、int lastIndexOf(String str) :返回在此字符串中最右边出现的指定子字符串的索引。 
4、int lastIndexOf(String str, int startIndex) :从指定的索引处开始向后搜索,返回在此字符串中最后一次出现的指定子字符串的索引。

题目3:


三羊献瑞

观察下面的加法算式:

祥 瑞 生 辉
+ 三 羊 献 瑞
-------------------
三 羊 生 瑞 气

(如果有对齐问题,可以参看【图1.jpg】)

其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。

请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。

 

解决方法(朕):

 1 package test;
 2 
 3 public class test {
 4     // 假设“祥”,“瑞”,“生”,“辉”,“羊”,“献”,“气”为:
 5     //
 6 
 7     public static void main(String args[]) {
 8 
 9         int n = 0;
10         int x = 0;
11         int m = 0;
12         int o = 0;
13         int y = 0;
14         int p = 0;
15         int q = 0;
16         int number1;
17         int number2;
18         int sum;
19 
20         here: {
21             for (n = 1; n <= 9; n++) {
22                 for (x = 0; x <= 9; x++) {
23                     for (m = 0; m <= 9; m++) {
24                         for (o = 0; o <= 9; o++) {
25                             for (y = 0; y <= 9; y++) {
26                                 for (p = 0; p <= 9; p++) {
27                                     for (q = 0; q <= 9; q++) {
28                                         if (n != x && n != m && n != o && n != y && n != p && n != q && x != m && x != o
29                                                 && x != y && x != p && x != q && m != 0 && m != y && m != p && m != q
30                                                 && o != y && o != p && o != q && y != p && y != q && p != q && q != 1) {
31                                             number1 = n * 1000 + x * 100 + m * 10 + o;
32                                             number2 = 1 * 1000 + y * 100 + p * 10 + x;
33                                             sum = 1 * 10000 + y * 1000 + m * 100 + x * 10 + q;
34                                             if (number1 + number2 == sum) {
35 
36                                                 System.out.println("1" + y + m + x + q);
37                                                 break here;
38 
39                                             }
40 
41                                         }
42 
43                                     }
44 
45                                 }
46 
47                             }
48                         }
49 
50                     }
51 
52                 }
53 
54             }
55 
56         }
57     }
58 }

chuchu:

 1 package test;
 2 
 3 public class q {
 4     // 祥0 瑞1 生2 辉3
 5     // 三4 羊5 献6 气7
 6     // 求三羊献瑞4562
 7     public static void main(String[] args) {
 8         // 先定义数组存放八个字对应的数字、
 9         // 祥和三不能为0
10         int[] n = new int[8];
11         long s1, s2, res;
12         int t = 1;
13         int i0 = 0, i1 = 0, i2 = 0, i3 = 0, i4 = 0, i5 = 0, i6 = 0, i7 = 0;
14         int[] k = new int[8];
15         int i = 0;
16         // 应注意不同汉字代表不同数字
17         for (i0 = 1; i0 <= 9 && t == 1; i0++) {
18             for (i1 = 0; i1 <= 9 && t == 1; i1++) {
19                 if (i1 == i0)
20                     continue;
21                 for (i2 = 0; i2 <= 9 && t == 1; i2++) {
22                     if (i2 == i0 || i2 == i1)
23                         continue;
24                     for (i3 = 0; i3 <= 9 && t == 1; i3++) {
25                         if (i3 == i0 || i3 == i1 || i3 == i2)
26                             continue;
27                         for (i4 = 1; i4 <= 9 && t == 1; i4++) {
28                             if (i4 == i0 || i4 == i1 || i4 == i2 || i4 == i3)
29                                 continue;
30                             for (i5 = 0; i5 <= 9 && t == 1; i5++) {
31                                 if (i5 == i0 || i5 == i1 || i5 == i2 || i5 == i3 || i5 == i4)
32                                     continue;
33                                 for (i6 = 0; i6 <= 9 && t == 1; i6++) {
34                                     if (i6 == i0 || i6 == i1 || i6 == i2 || i6 == i3 || i6 == i4 || i6 == i5)
35                                         continue;
36                                     for (i7 = 0; i7 <= 9 && t == 1; i7++) {
37                                         if (i7 == i0 || i7 == i1 || i7 == i2 || i7 == i3 || i7 == i4 || i7 == i5
38                                                 || i7 == i6)
39                                             continue;
40 
41                                         // 祥瑞生辉+三羊献瑞 = 三羊生瑞气
42 
43                                         s1 = i0 * 1000 + i1 * 100 + i2 * 10 + i3;
44                                         s2 = i4 * 1000 + i5 * 100 + i6 * 10 + i1;
45                                         res = i4 * 10000 + i5 * 1000 + i2 * 100 + i1 * 10 + i7;
46                                         if (s1 + s2 == res) {
47                                             t = 0;
48                                             k[0] = i0;
49                                             k[1] = i1;
50                                             k[2] = i2;
51                                             k[3] = i3;
52                                             k[4] = i4;
53                                             k[5] = i5;
54                                             k[6] = i6;
55                                             k[7] = i7;
56 
57                                         }
58 
59                                     }
60                                 }
61                             }
62                         }
63                     }
64                 }
65 
66             }
67 
68         }
69         for (int u = 0; u < 8; u++) {
70             System.out.print(k[u] + " ");
71         }
72         System.out.println();
73 
74         System.out.println(k[4] + "" + k[5] + "" + k[6] + "" + k[2]);
75 
76     }
77 }
原文地址:https://www.cnblogs.com/Catherinezhilin/p/8677418.html