蓝桥杯java历年真题及答案整理(闭关一个月,呕心沥血整理出来的)
1 全排列
是这样的,如果给定N个不同字符,将这N个字符全排列,最终的结果将会是N!种。如:给定 A、B、C三个不同的字符,则结果为:ABC、ACB、BAC、BCA、CAB、CBA一共3!=3*2=6种情况。
package Question1_9;
import java.util.Scanner;
import java.util.Vector;
public class Question1 {
public static long count=0;
private void fullPermutation(Vectorsourse, Vector result) {
if(sourse.size()==0){
for (int i = 0; i < result.size(); i++) {
System.out.print(result.elementAt(i));
}
System.out.print("n");
count++;
return;
}
for (int i = 0; i < sourse.size(); i++) {
Vectortsourse=new Vector(sourse);
Vectortresult=new Vector(result);
tresult.add(sourse.elementAt(i));
tsourse.remove(i);
new Question1().fullPermutation(tsourse, tresult);
}
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
Vector sourse=new Vector();
Vector result=new Vector();
for (int i = 0; i < n; i++) {
sourse.add((char)('A'+i));
}
new Question1().fullPermutation(sourse, result);
System.out.println(Question1.count);
}
}
2串的简单处理
串的处理
在实际的开发工作中,对字符串的处理是最常见的编程任务。
本题目即是要求程序对用户输入的串进行处理。具体规则如下:
- 把每个单词的首字母变为大写。
- 把数字与字母之间用下划线字符(_)分开,使得更清晰
- 把单词中间有多个空格的调整为1个空格。
例如:
用户输入:
you and me what cpp2005program
则程序输出:
You And Me What Cpp_2005_program
用户输入:
this is a 99cat
则程序输出:
This Is A 99_cat
我们假设:用户输入的串中只有小写字母,空格和数字,不含其它的字母或符号。
每个单词间由1个或多个空格分隔。
假设用户输入的串长度不超过200个字符。
package Question1_9;
import java.util.Scanner;
import java.util.Vector;
public class Question2 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String string=scanner.nextLine();
Vectorvector=new Vector();
for (int i = 0; i < string.length(); i++) {
vector.add(string.charAt(i));
}
try {
int index=0;
while (index='a'&&vector.elementAt(index)<='z'){
vector.set(index, (char)(vector.elementAt(index)-('a'-'A')));
}else if(vector.elementAt(index-1)==' '&&vector.elementAt(index)==' '){
vector.remove(index);
index--;
}else if (vector.elementAt(index-1)==' '&&(vector.elementAt(index)>='a'&&vector.elementAt(index)<='z')) {
vector.set(index, (char)(vector.elementAt(index)-('a'-'A')));
}else if((vector.elementAt(index)>='a'&&vector.elementAt(index)<='z')&&(vector.elementAt(index-1)>='0'&&vector.elementAt(index-1)<='9')){
vector.add(index, '_');
index++;
}else if((vector.elementAt(index-1)>='a'&&vector.elementAt(index-1)<='z')&&(vector.elementAt(index)>='0'&&vector.elementAt(index)<='9')){
vector.add(index, '_');
index++;
}
index++;
}
for (int i = 0; i
运行结果:
you and me what cpp2005program
You And Me What Cpp_2005_program
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
.....
请编程,输出所有可能的情况!
注意:
左边的两个乘数交换算同一方案,不要重复输出!
不同方案的输出顺序不重要
package Question1_9;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Vector;
public class Question3 {
public static long count=0;
public static List> filteredNonRedundantResults;
private static boolean isfilter(Vector 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(Vectorvector) {
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(Vectorsourse, Vector 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(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 sourse=new Vector();
Vector result=new Vector();
for (int i = 1; i <= n; i++) {
sourse.add((char)('0'+i));
}
Question3.filteredNonRedundantResults=new ArrayList>();
Question3.fullPermutation(sourse, result);
for (int i = 0; i < Question3.filteredNonRedundantResults.size(); i++) {
Question3.print(Question3.filteredNonRedundantResults.get(i));
System.out.println();
}
}
}
运行结果:
46 x 79 = 23 x 158
54 x 69 = 27 x 138
54 x 93 = 27 x 186
58 x 67 = 29 x 134
58 x 69 = 23 x 174
58 x 73 = 29 x 146
58 x 96 = 32 x 174
63 x 74 = 18 x 259
64 x 79 = 32 x 158
73 x 96 = 12 x 584
76 x 98 = 14 x 532
4 Excel地址转换
Excel是最常用的办公软件。每个单元格都有唯一的地址表示。 比如:第12行第4列表示为:“D12”,第5行第255列表示为“IU5”。
事实上,Excel提供了两种地址表示方法,还有一种表示法叫做RC格式地址。
第12行第4列表示为:“R12C4”,第5行第255列表示为“R5C255”。
你的任务是:编写程序,实现从RC地址格式到常规地址格式的转换。
【输入、输出格式要求】
用户先输入一个整数n(n<100)表示接下来有n行输入数据。
接着输入的n行数据是RC格式的Excel单元格地址表示法。
程序则输出n行数据,每行是转换后的常规地址表示法。
例如:用户输入:
2
R12C4
R5C255
则程序应该输出:
D12
IU5
package Question1_9;
import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;
public class Question4 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
scanner.nextLine(); //必须加上的,不然会导致输入不准确!
while (n>0) {
String string=scanner.nextLine();
String strings[]=string.split("C");
strings[0]=strings[0].substring(1, strings[0].length());
int hangshu=Integer.parseInt(strings[0]),lieshu=Integer.parseInt(strings[1]);//获取行数和列数
Stackstack=new Stack();
while(lieshu>0){
if(lieshu%26==0){
stack.push('Z');
lieshu=lieshu/26-1;
}else {
stack.push((char)('A'-1+lieshu%26));
lieshu=lieshu/26;
}
}
while (!stack.empty()) {
System.out.print(stack.pop());
}
System.out.println(hangshu);
n--;
}
}
}
运行结果:
输入一个整数n(n<100)
2
R12C4
R5C255
D12
IU5
5. 手机尾号
package Question1_9;
import java.util.Scanner;
import java.util.Stack;
import java.util.Vector;
public class Question5 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
scanner.nextLine();
while ((n--)>0) {
String telphone=scanner.nextLine();
int sum=0;
if(telphone.charAt(0)-telphone.charAt(1)==1){
char ch=telphone.charAt(0);
int index=0;
while (index<4&&ch==telphone.charAt(index)) {
ch--;
index++;
}
if(index>=4){
sum+=5;
}
}
if (telphone.charAt(0)-telphone.charAt(1)==-1) {
char ch=telphone.charAt(0);
int index=0;
while (index<4&&ch==telphone.charAt(index)) {
ch++;
index++;
}
if(index>=4){
sum+=5;
}
}
if (telphone.charAt(0)==telphone.charAt(1)&&telphone.charAt(1)==telphone.charAt(2)) {
sum+=3;
}
if(telphone.charAt(1)==telphone.charAt(2)&&telphone.charAt(2)==telphone.charAt(3)){
sum+=3;
}
if(telphone.charAt(0)==telphone.charAt(1)&&telphone.charAt(2)==telphone.charAt(3)){
sum+=1;
}
if(telphone.charAt(0)==telphone.charAt(2)&&telphone.charAt(1)==telphone.charAt(3)){
sum+=1;
}
for (int i = 0; i < 4; i++) {
if(telphone.charAt(i)=='6'||telphone.charAt(i)=='8'||telphone.charAt(i)=='9'){
sum+=1;
}
}
System.out.println(sum);
}
}
}
运行结果:
14
3045
0211
…..
…..
……
8
5
6. 括号问题
下面的代码用于判断一个串中的括号是否匹配 所谓匹配是指不同类型的括号必须左右呼应,可以相互包含,但不能交叉
例如:
..(..[..]..).. 是允许的
..(...[...)....].... 是禁止的
对于 main 方法中的测试用例,应该输出:
false
true
false
false
请分析代码逻辑,并推测划线处的代码。
答案写在 “解答.txt” 文件中
注意:只写划线处应该填的内容,划线前后的内容不要抄写。
import java.util.*;
public class Demo06 {
public static boolean isGoodBracket(String s) {
Stack a = new Stack();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '(')
a.push(')');
if (c == '[')
a.push(']');
if (c == '{')
a.push('}');
if (c == ')' || c == ']' || c == '}') {
if (a.size()==0)
return false; // 填空
if (a.pop() != c)
return false;
}
}
if (a.size()!=0)
return false; // 填空
return true;
}
public static void main(String[] args) {
System.out.println(isGoodBracket("...(..[.)..].{.(..).}..."));
System.out.println(isGoodBracket("...(..[...].(.).).{.(..).}..."));
System.out.println(isGoodBracket(".....[...].(.).){.(..).}..."));
System.out.println(isGoodBracket("...(..[...].(.).){.(..)...."));
}
}
运行结果:
false
true
false
false
7. 扑克牌移动
package Question1_9;
import java.util.Arrays;
import java.util.List;
import java.util.Vector;
public class Question7 {
public static List moveCard(List src) {
if (src == null)
return null;
List dst = new Vector();
for (;;) {
if (src.size()==0)
break; // 填空
src.add(src.remove(0));
dst.add(src.remove(0)); // 填空
}
return dst;
}
public static void main(String[] args) {
List a = new Vector();
a.addAll(Arrays.asList("A", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "J", "Q", "K"));
System.out.println(moveCard(a));
}
}
运行结果:
[2, 4, 6, 8, 10, Q, A, 5, 9, K, 7, 3, J]
8. 第一个数字
public class Demo04 {
public static int getFirstNum(String s) {
if (s == null || s.length() == 0)
return -1;
char c = s.charAt(0);
if (c >= '0' && c <= '9')
return s.charAt(0)-'0'; // 填空
return getFirstNum(s.substring(1)); // 填空
}
public static void main(String[] args) {
String s1 = "abc24us43"; //则返回2
String s2 = "82445adb5"; //则返回8
String s3 = "ab"; //则返回-1
System.out.println(getFirstNum(s1));
System.out.println(getFirstNum(s2));
System.out.println(getFirstNum(s3));
}
}
运行结果:
2
8
-1
9. 放麦子
package Question1_9;
import java.math.BigInteger;
public class Question9 {
public static void main(String[] args) {
BigInteger total=new BigInteger("0");
BigInteger base=new BigInteger("2");
for (int i = 0; i < 64; i++) {
total=total.add(base.pow(i));
//System.out.println(total);
}
System.out.println(total);
System.out.println(base.pow(64).add(new BigInteger("-1")));
}
}
运行结果:
18446744073709551614
10. 求21位数的水仙花数
package Question10_19;
import java.math.BigInteger;
import java.util.Scanner;
class Question10Think2OptimizeMustRemember {
public static int size;
public static int array[]={0,1,2,3,4,5,6,7,8,9};
public static BigInteger powArray[] = new BigInteger[10]; // 记录0~9的size次方
public static int usedTimes[]=new int[10];// 记录0~9的使用次数
public static BigInteger iPowSizeMultiplyj[][]; //记录0到9中任意数字i的N次方乘以i出现的次数j的结果(i^N*j)
public static BigInteger MAX; // size位的数字能表示的最大值
public static BigInteger MIN; // size位的数字能表示的最小值
public static void init() {// 用于初始化powArray[],MAX,MIN
for (int i = 0; i < 10; i++) {// 初始化powArray[]
powArray[i] = (new BigInteger("" + i)).pow(size);
}
MIN = (new BigInteger("10")).pow(size - 1); // 初始化最小值
MAX = (new BigInteger("10").pow(size).add(new BigInteger("-1")));// 初始化最大值
iPowSizeMultiplyj=new BigInteger[10][size+1]; //初始化iPowSizeMultiplyj[][]
for (int i = 0; i < 10; i++) {
iPowSizeMultiplyj[i][0]=BigInteger.valueOf(0);
for (int j = 1; j < size+1; j++) {
iPowSizeMultiplyj[i][j]=iPowSizeMultiplyj[i][j-1].add(powArray[i]);
}
}
}
public static void exhaustion(int arrayIndex,int used,BigInteger current) {
if (current.compareTo(MAX)>1) {//超过最大值,递归结束
return;
}
if(used==size){//size位全部分配完毕
if(current.compareTo(MIN)<0){ //已获得的值小于最小值
return;
}else {
String s=current+"";
int avaliablevalueUsed[]=new int[10];
for (int i = 0; i < s.length(); i++) {
avaliablevalueUsed[s.charAt(i)-'0']++;
}
for (int i = 0; i < 10; i++) {
if(usedTimes[i]!=avaliablevalueUsed[i]){
return;
}
}
System.out.println(current);
return;
}
}
if(arrayIndex==0){
usedTimes[0]=size-used;
exhaustion(-1, size, current);
usedTimes[0]=0;
return;
}
if(current.add(iPowSizeMultiplyj[arrayIndex][size-used]).compareTo(MIN)<0){
return;
}
if(arrayIndex>=0){
for (int i = 0; i <= size-used; i++) {
if(current.add(iPowSizeMultiplyj[arrayIndex][i]).compareTo(MAX)>0){
return;
}
usedTimes[arrayIndex]=i;
exhaustion(arrayIndex-1, used+i,current.add(iPowSizeMultiplyj[arrayIndex][i]));
usedTimes[arrayIndex]=0;
}
}else {
return;//1到9已分配完毕,不可再延伸了
}
}
public static void main(String[] args) {
// Scanner scanner = new Scanner(System.in);
// Question10Think2.size = scanner.nextInt();
long startTime = System.currentTimeMillis(); // 程序开始时间
Question10Think2OptimizeMustRemember.size=21;
Question10Think2OptimizeMustRemember.init();
Question10Think2OptimizeMustRemember.exhaustion(9, 0, BigInteger.valueOf(0));
long endTime = System.currentTimeMillis(); // 程序结束时间
System.out.println((endTime-startTime)/1000f+"秒"); // 运行总时
}
}
运行结果:
128468643043731391252
449177399146038697307
19.062秒
11. 猜生日
package Question10_19;
public class Question11 {
public static void main(String[] args) {
for (int i = 20120312; ; i--) {
String s=""+i;
int year=Integer.parseInt(s.substring(0, 4));
int month=Integer.parseInt(s.substring(4, 6));
int day=Integer.parseInt(s.substring(6, 8));
// System.out.println(year+" "+month+" "+day);
if(day==0||day>31){
continue;
}
if(!(i%2012==0&&i%3==0&&i%12==0)){
continue;
}
if(month!=6){
continue;
}
System.out.println(i);
break;
}
}
}
运行结果:
19550604
12. 填算式
package Question10_19;
import java.util.Vector;
public class Question12 {
public static int count;
public static void AllType(Vector sourse,Vectorresult) {
if(sourse.size()==0){
//System.out.println(result);
int a=(result.elementAt(0)-'0')*100+(result.elementAt(1)-'0')*10+result.elementAt(2)-'0';
int b=(result.elementAt(3)-'0')*100+(result.elementAt(4)-'0')*10+result.elementAt(5)-'0';
int c=(result.elementAt(6)-'0')*100+(result.elementAt(7)-'0')*10+result.elementAt(8)-'0';
if(a+b==c){
System.out.printf("%d + %d = %dn",a,b,c);
count++;
}
}else{
for (int i = 0; i < sourse.size(); i++) {
result.add(sourse.elementAt(i));
sourse.remove(i);
AllType(sourse, result);
sourse.add(i, result.elementAt(result.size()-1));
result.remove(result.size()-1);
}
}
}
public static void main(String[] args) {
Vectorsourse=new Vector();
Vectorresult=new Vector();
for (int i = 1; i <= 9; i++) {
sourse.add((char)('0'+i));
}
AllType(sourse, result);
System.out.println(count);
}
}
运行结果:
124 + 659 = 783
125 + 739 = 864
127 + 359 = 486
127 + 368 = 495
128 + 367 = 495
128 + 439 = 567
129 + 357 = 486
129 + 438 = 567
129 + 654 = 783
129 + 735 = 864
134 + 658 = 792
135 + 729 = 864
138 + 429 = 567
138 + 654 = 792
139 + 428 = 567
139 + 725 = 864
142 + 596 = 738
142 + 695 = 837
143 + 586 = 729
145 + 692 = 837
146 + 583 = 729
146 + 592 = 738
152 + 487 = 639
152 + 784 = 936
154 + 629 = 783
154 + 638 = 792
154 + 782 = 936
157 + 329 = 486
157 + 482 = 639
158 + 634 = 792
159 + 327 = 486
159 + 624 = 783
162 + 387 = 549
162 + 783 = 945
163 + 782 = 945
167 + 328 = 495
167 + 382 = 549
168 + 327 = 495
173 + 286 = 459
173 + 295 = 468
175 + 293 = 468
176 + 283 = 459
182 + 367 = 549
182 + 394 = 576
182 + 457 = 639
182 + 493 = 675
182 + 754 = 936
182 + 763 = 945
183 + 276 = 459
183 + 492 = 675
183 + 546 = 729
183 + 762 = 945
184 + 392 = 576
184 + 752 = 936
186 + 273 = 459
186 + 543 = 729
187 + 362 = 549
187 + 452 = 639
192 + 384 = 576
192 + 483 = 675
192 + 546 = 738
192 + 645 = 837
193 + 275 = 468
193 + 482 = 675
194 + 382 = 576
195 + 273 = 468
195 + 642 = 837
196 + 542 = 738
214 + 569 = 783
214 + 659 = 873
215 + 478 = 693
215 + 748 = 963
216 + 378 = 594
216 + 738 = 954
218 + 349 = 567
218 + 376 = 594
218 + 439 = 657
218 + 475 = 693
218 + 736 = 954
218 + 745 = 963
219 + 348 = 567
219 + 438 = 657
219 + 564 = 783
219 + 654 = 873
234 + 657 = 891
235 + 746 = 981
236 + 718 = 954
236 + 745 = 981
237 + 654 = 891
238 + 419 = 657
238 + 716 = 954
239 + 418 = 657
241 + 596 = 837
243 + 576 = 819
243 + 675 = 918
245 + 673 = 918
245 + 718 = 963
245 + 736 = 981
246 + 573 = 819
246 + 591 = 837
246 + 735 = 981
248 + 319 = 567
248 + 715 = 963
249 + 318 = 567
251 + 397 = 648
254 + 619 = 873
254 + 637 = 891
257 + 391 = 648
257 + 634 = 891
259 + 614 = 873
264 + 519 = 783
269 + 514 = 783
271 + 593 = 864
271 + 683 = 954
273 + 186 = 459
273 + 195 = 468
273 + 546 = 819
273 + 591 = 864
273 + 645 = 918
273 + 681 = 954
275 + 193 = 468
275 + 418 = 693
275 + 643 = 918
276 + 183 = 459
276 + 318 = 594
276 + 543 = 819
278 + 316 = 594
278 + 415 = 693
281 + 394 = 675
281 + 673 = 954
283 + 176 = 459
283 + 671 = 954
284 + 391 = 675
286 + 173 = 459
291 + 357 = 648
291 + 384 = 675
291 + 546 = 837
291 + 573 = 864
293 + 175 = 468
293 + 571 = 864
294 + 381 = 675
295 + 173 = 468
296 + 541 = 837
297 + 351 = 648
314 + 658 = 972
316 + 278 = 594
317 + 529 = 846
317 + 628 = 945
318 + 249 = 567
318 + 276 = 594
318 + 627 = 945
318 + 654 = 972
319 + 248 = 567
319 + 527 = 846
324 + 567 = 891
324 + 657 = 981
327 + 159 = 486
327 + 168 = 495
327 + 519 = 846
327 + 564 = 891
327 + 618 = 945
327 + 654 = 981
328 + 167 = 495
328 + 617 = 945
329 + 157 = 486
329 + 517 = 846
341 + 586 = 927
342 + 576 = 918
346 + 572 = 918
346 + 581 = 927
348 + 219 = 567
349 + 218 = 567
351 + 297 = 648
352 + 467 = 819
354 + 618 = 972
354 + 627 = 981
357 + 129 = 486
357 + 291 = 648
357 + 462 = 819
357 + 624 = 981
358 + 614 = 972
359 + 127 = 486
362 + 187 = 549
362 + 457 = 819
364 + 527 = 891
367 + 128 = 495
367 + 182 = 549
367 + 452 = 819
367 + 524 = 891
368 + 127 = 495
372 + 546 = 918
376 + 218 = 594
376 + 542 = 918
378 + 216 = 594
381 + 294 = 675
381 + 546 = 927
382 + 167 = 549
382 + 194 = 576
384 + 192 = 576
384 + 291 = 675
386 + 541 = 927
387 + 162 = 549
391 + 257 = 648
391 + 284 = 675
392 + 184 = 576
394 + 182 = 576
394 + 281 = 675
397 + 251 = 648
415 + 278 = 693
418 + 239 = 657
418 + 275 = 693
419 + 238 = 657
428 + 139 = 567
429 + 138 = 567
438 + 129 = 567
438 + 219 = 657
439 + 128 = 567
439 + 218 = 657
452 + 187 = 639
452 + 367 = 819
457 + 182 = 639
457 + 362 = 819
462 + 357 = 819
467 + 352 = 819
475 + 218 = 693
478 + 215 = 693
482 + 157 = 639
482 + 193 = 675
483 + 192 = 675
487 + 152 = 639
492 + 183 = 675
493 + 182 = 675
514 + 269 = 783
517 + 329 = 846
519 + 264 = 783
519 + 327 = 846
524 + 367 = 891
527 + 319 = 846
527 + 364 = 891
529 + 317 = 846
541 + 296 = 837
541 + 386 = 927
542 + 196 = 738
542 + 376 = 918
543 + 186 = 729
543 + 276 = 819
546 + 183 = 729
546 + 192 = 738
546 + 273 = 819
546 + 291 = 837
546 + 372 = 918
546 + 381 = 927
564 + 219 = 783
564 + 327 = 891
567 + 324 = 891
569 + 214 = 783
571 + 293 = 864
572 + 346 = 918
573 + 246 = 819
573 + 291 = 864
576 + 243 = 819
576 + 342 = 918
581 + 346 = 927
583 + 146 = 729
586 + 143 = 729
586 + 341 = 927
591 + 246 = 837
591 + 273 = 864
592 + 146 = 738
593 + 271 = 864
596 + 142 = 738
596 + 241 = 837
614 + 259 = 873
614 + 358 = 972
617 + 328 = 945
618 + 327 = 945
618 + 354 = 972
619 + 254 = 873
624 + 159 = 783
624 + 357 = 981
627 + 318 = 945
627 + 354 = 981
628 + 317 = 945
629 + 154 = 783
634 + 158 = 792
634 + 257 = 891
637 + 254 = 891
638 + 154 = 792
642 + 195 = 837
643 + 275 = 918
645 + 192 = 837
645 + 273 = 918
654 + 129 = 783
654 + 138 = 792
654 + 219 = 873
654 + 237 = 891
654 + 318 = 972
654 + 327 = 981
657 + 234 = 891
657 + 324 = 981
658 + 134 = 792
658 + 314 = 972
659 + 124 = 783
659 + 214 = 873
671 + 283 = 954
673 + 245 = 918
673 + 281 = 954
675 + 243 = 918
681 + 273 = 954
683 + 271 = 954
692 + 145 = 837
695 + 142 = 837
715 + 248 = 963
716 + 238 = 954
718 + 236 = 954
718 + 245 = 963
725 + 139 = 864
729 + 135 = 864
735 + 129 = 864
735 + 246 = 981
736 + 218 = 954
736 + 245 = 981
738 + 216 = 954
739 + 125 = 864
745 + 218 = 963
745 + 236 = 981
746 + 235 = 981
748 + 215 = 963
752 + 184 = 936
754 + 182 = 936
762 + 183 = 945
763 + 182 = 945
782 + 154 = 936
782 + 163 = 945
783 + 162 = 945
784 + 152 = 936
336
13. 火柴游戏
package Question10_19;
import java.util.Scanner;
public class Question13 {
public static boolean isOk(char[][] state, int i, int j) {
if (state[i][j] == '-') {
for (int j2 = j + 1; j2 < 4; j2++) {
if (state[i][j2] == '-') {
return false;
} else if (state[i][j2] == '1') {
return true;
}
}
for (int j2 = j - 1; j2 >= 0; j2--) {
if (state[i][j2] == '-') {
return false;
} else if (state[i][j2] == '1') {
return true;
}
}
} else if (state[i][j] == '1') {
for (int i2 = i + 1; i2 < 3; i2++) {
if (state[i2][j] == '1') {
return false;
} else if (state[i2][j] == '-') {
return true;
}
}
for (int i2 = i - 1; i2 >= 0; i2--) {
if (state[i2][j] == '1') {
return false;
} else if (state[i2][j] == '-') {
return true;
}
}
}
return true;
}
private static void jasdklf(char[][] state) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
if (state[i][j] == '0') {
state[i][j] = '-';
if (isOk(state, i, j)) {
System.out.println(i + "" + j + '-');
return;
}
state[i][j] = '0';
state[i][j] = '1';
if (isOk(state, i, j)) {
System.out.println(i + "" + j + '1');
return;
}
state[i][j] = '0';
}
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
scanner.nextLine();
char[][] state = new char[3][4];
String s;
while ((n--) > 0) {
for (int i = 0; i < 3; i++) {
s = scanner.nextLine();
for (int j = 0; j < 4; j++) {
state[i][j] = s.charAt(j);
}
}
jasdklf(state);
}
}
}
14. 古代赌局
package Question10_19;
import java.util.Scanner;
public class Question14 {
public static void main(String[] args) {
int a,b,c,d,sum = 0;
for (int i = 0; i < 500000; i++) {
a=(int) (Math.random()*6)+1;
b=(int) (Math.random()*6)+1;
c=(int) (Math.random()*6)+1;
d=(int) (Math.random()*6)+1;
// System.out.println(a+" "+b+" "+c+" "+d);
if(a==b&&a==c&&a==d){
sum-=6;
}else if((a==b&&a==c)||(a==c&&a==d)||(a==b&&a==d)){
sum-=2;
}else if(a==b||a==c||a==d){
sum-=1;
}else if ((a*b==c*d)||(a*c==b*d)||(a*d==b*c)) {
sum-=0;
}else {
sum+=1;
}
}
System.out.printf("%.3f",sum/500000f);
}
}
程序输出:
0.021
15. 源码变换
package Question10_19;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Question15FinishedBefore {
private String BoldComments(String tempString, String note) {
return null;
}
public static void main(String[] args) {
try {
File inFile = new File("test.java");
FileInputStream fileInputStream;
fileInputStream = new FileInputStream(inFile);
InputStreamReader inputStreamReader = new InputStreamReader(
fileInputStream);
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader);
File outFile = new File("test.html");
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
OutputStreamWriter outStreamWriter = new OutputStreamWriter(
fileOutputStream);
BufferedWriter bufferedWriter = new BufferedWriter(outStreamWriter);
outStreamWriter.write("n");
outStreamWriter.write("n");
String tempString;
while ((tempString = bufferedReader.readLine()) != null) {
tempString = tempString.replaceAll("&", "&");
tempString = tempString.replaceAll(" ", " ");
tempString = tempString.replaceAll("<", "<");
tempString = tempString.replaceAll(">", ">");
int index1 = tempString.lastIndexOf("//");
int index2 = tempString.indexOf(""");
if (index1 != -1 && index2 == -1) {
String s1 = tempString.substring(0, index1);
String s2 = tempString.substring(index1);
s2 = "" + s2 + "";
tempString = s1 + s2;
} else if (index1 != -1 && index2 != -1) {
int startMark = -1, endMark = -1;
boolean isNote = true;
for (int i = 0; i < tempString.length(); i++) {
if ('"' == tempString.charAt(i)) {
if (startMark == -1) {
startMark = i;
} else {
endMark = i;
if (index1 > startMark && index1 < endMark) {
isNote = false;
break;
} else {
startMark = -1;
endMark = -1;
}
}
}
}
if (isNote == true) {
String s1 = tempString.substring(0, index1);
String s2 = tempString.substring(index1);
s2 = "" + s2 + "";
tempString = s1 + s2;
}
}
tempString = tempString.replaceAll(""", """);
tempString = tempString.replaceAll("t",
" ");
tempString = tempString.replaceAll(" public ",
" public ");
tempString = tempString.replaceAll(" class ",
" class ");
tempString = tempString.replaceAll(" static ",
" static ");
tempString = tempString.replaceAll(" void ",
" void ");
outStreamWriter.write(tempString + "
" + "rn");
}
outStreamWriter.write("nn");
outStreamWriter.write("n");
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
运行结果:
// 我的工具类
public class MyTool
{
public static void main(String[] args)
{
int a = 100;
int b = 20;
if(a>b && true)
System.out.println(a);
else
System.out.println("this! //aaa//kkk"); // 测试注释显示是否正确
}
}
16. 数量周期
public class Demo01 {
static int count = 100; // 执行100次退出
public static void f(double x,double r){
if(count<=0) return;
x = x * (1 - x) * r;
System.out.println(x);
count--;
f(x,r);
}
public static void main(String[] args){
double x = 0.2;
double r = 3.62;
f(x,r);
System.out.println("网络某某结论:虫口数目函数呈锯齿状变化," +
"虫口数目不存在连续两年增加和连续两年减少的情况。");
}
}
运行结果:
0.5792000000000002
……
……
……
0.878401825611548
网络某某结论:虫口数目函数呈锯齿状变化,虫口数目不存在连续两年增加和连续两年减少的情况。
17. 提取子串
public class Demo02_two {
public static String maxS(String s){
String maxS = "";
char[] c = s.toCharArray();
for(int i=0;imaxS.length()){
maxS = temp;
}
}
return maxS;
}
public static void main(String[] args){
String s = "abcdeefghhgfeiieje444k444lmn";
System.out.println(maxS(s));
}
}
方法二:
public class Demo02 {
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(i - step + 1, i + step); // 填空1
// 第二种对称模式
step = 0;
try {
for (;;) {
if (s.charAt(i - step) != s.charAt(i + step + 1))
break; // 填空2
step++;
}
} catch (Exception e) {
}
String s2 = s.substring(i - step + 1, i + step + 1);
if (s1.length() > max_s.length())
max_s = s1;
if (s2.length() > max_s.length())
max_s = s2;
}
return max_s;
}
public static void main(String[] args) {
String s = "abcdeefghhgfeiieje444k444lmn";
System.out.println(getMaxMirrorString(s));
}
}
运行结果:
efghhgfe
18. 取球游戏
package Question10_19;
import java.util.Scanner;
public class Question18Think2MustRemember {
public static boolean array[]=new boolean[10020];
public static void main(String[] args) {
array[0]=true;
for (int i = 1; i < array.length; i++) {
array[i]=(i>=8&&!array[i-8])||(i>=7&&!array[i-7])||(i>=3&&!array[i-3])||(i>=1&&!array[i-1]);
}
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int total;
scanner.nextLine();
while ((n--)>0) {
total=scanner.nextInt();
System.out.println(array[total]?1:0);
}
}
}
运行结果:
4
1
2
10
18
输出结果:
0
1
1
0
19. 密码发生器
package Question10_19;
import java.util.Scanner;
public class Question19 {
public static int simplify(int n) {
String s;
while (n>=10) {
s=n+"";
n=0;
for (int i = 0; i < s.length(); i++) {
n+=s.charAt(i)-'0';
}
}
return n;
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
String s;
scanner.nextLine();
while((n--)>0){
int array[]=new int[6];
s=scanner.nextLine();
for (int i = 0; i < s.length(); i++) {
array[i%6]+=(int)(s.charAt(i));
}
for (int i = 0; i < 6; i++) {
System.out.print(simplify(array[i]));
}
System.out.println();
}
// System.out.println(simplify(123456789));
}
}
运行结果:
输入整数n(<100)表示下边有多少输入行:
5
zhangfeng
wangximing
jiujingfazi
woaibeijingtiananmen
haohaoxuexi
772243
344836
297332
716652
875843
20. 转方阵
public class Demo03 {
// 矩阵顺时针旋转
public static void rotation(int[][] n,int [][] m,int i,int j){
int t = j; // 标记最后一行的位置
if(i>=n.length) return;
for(int k=0;k
运行结果:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
顺时针旋转结果:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



