藍橋杯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(Vector<Character>sourse, Vector<Character> 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++) {
Vector<Character>tsourse=new Vector<Character>(sourse);
Vector<Character>tresult=new Vector<Character>(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<Character> sourse=new Vector<Character>();
Vector<Character> result=new Vector<Character>();
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();
Vector<Character>vector=new Vector<Character>();
for (int i = 0; i < string.length(); i++) {
vector.add(string.charAt(i));
}
try {
int index=0;
while (index<vector.size()) {
if(index==0&&vector.elementAt(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 <vector.size(); i++) {
System.out.print(vector.elementAt(i));
}
System.out.println();
} catch (ArrayIndexOutOfBoundsException e) {
// TODO: handle exception
}
}
}
運行結果:
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<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) {
Scanner scanner=new Scanner(System.in);
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));
}
Question3.filteredNonRedundantResults=new ArrayList<Vector<Character>>();
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]);//獲取行數和列數
/*
* 對列數進行變換
*/
Stack<Character>stack=new Stack<Character>();
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. 手機尾號
/*
30年的改革開放,給中國帶來了翻天覆地的變化。2011全年中國手機產量約為11.72億部。手機已經成為百姓的基本日用品! 給手機選個好聽又好記的號碼可能是許多人的心願。
但號源有限,只能輔以有償選號的方法了。
這個程序的目的就是:根據給定的手機尾號(4位),按照一定的規則來打分。其規則如下:
1. 如果出現連號,不管升序還是降序,都加5分。例如:5678,4321都滿足加分標准。
2. 前三個數字相同,或后三個數字相同,都加3分。例如:4888,6665,7777都滿足加分的標准。
注意:7777因為滿足這條標准兩次,所以這條規則給它加了6分。
3. 符合AABB或者ABAB模式的加1分。例如:2255,3939,7777都符合這個模式,所以都被加分。
注意:7777因為滿足這條標准兩次,所以這條標准給它加了2分。
4. 含有:6,8,9中任何一個數字,每出現一次加1分。例如4326,6875,9918都符合加分標准。其中,6875被加2分;9918被加3分。
尾號最終得分就是每條標准的加分總和!
要求程序從標准輸入接收數據,在標准輸出上輸出結果。
輸入格式為:第一行是一個整數n(<100),表示下邊有多少輸入行,接下來是n行4位一組的數據,就是等待計算加分的手機尾號。
例如,輸入:
14
3045
….
…..
6789
8866
則輸出:
0
0
….
…
8
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<Character> a = new Stack<Character>();
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. 撲克牌移動
/*
下面代碼模擬了一套撲克牌(初始排序A~K,共13張)的操作過程。
操作過程是:
手里拿着這套撲克牌,從前面拿一張放在后面,再從前面拿一張放桌子上,再從前面拿一張放在后面,....
如此循環操作,直到剩下最后一張牌也放在桌子上。
下面代碼的目的就是為了求出最后桌上的牌的順序。
初始的排列如果是A,2,3...K,則最后桌上的順序為:
[2, 4, 6, 8, 10, Q, A, 5, 9, K, 7, 3, J]
請分析代碼邏輯,並推測划線處的代碼。
答案寫在 “解答.txt” 文件中
注意:只寫划線處應該填的內容,划線前后的內容不要抄寫。
*/
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. 第一個數字
/*
以下的靜態方法實現了:把串s中第一個出現的數字的值返回。
如果找不到數字,返回-1
例如:
s = "abc24us43" 則返回2
s = "82445adb5" 則返回8
s = "ab" 則返回-1
請分析代碼邏輯,並推測划線處的代碼。
答案寫在 “解答.txt” 文件中
注意:只寫划線處應該填的內容,划線前后的內容不要抄寫。
*/
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. 放麥子
/*
你一定聽說過這個故事。國王對發明國際象棋的大臣很佩服,
問他要什么報酬,大臣說:請在第1個棋盤格放1粒麥子,
在第2個棋盤格放2粒麥子,在第3個棋盤格放4粒麥子,
在第4個棋盤格放8粒麥子,......后一格的數字是前一格的兩倍,
直到放完所有棋盤格(國際象棋共有64格)。
國王以為他只是想要一袋麥子而已,哈哈大笑。
當時的條件下無法准確計算,但估算結果令人吃驚:即使全世界都鋪滿麥子也不夠用!
請你借助計算機准確地計算,到底需要多少粒麥子。
答案寫在“解答.txt”中,不要寫在這里!
*/
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. 猜生日
/*
今年的植樹節(2012年3月12日),小明和他的叔叔還有小伙伴們一起去植樹。
休息的時候,小明的同學問他叔叔多大年紀,他叔叔說:“我說個題目,看你們誰先猜出來!”
“把我出生的年月日連起來拼成一個8位數(月、日不足兩位前補0)正好可以被今天的年、月、日整除!”
他想了想,又補充到:“再給個提示,我是6月出生的。”
根據這些信息,請你幫小明算一下,他叔叔的出生年月日。
答案寫在“解答.txt”中,不要寫在這里!
格式是年月日連成的8位數。
例如,如果是1948年6月12日,就寫:19480612
*/
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. 填算式
/*
看這個算式:
☆☆☆ + ☆☆☆ = ☆☆☆
如果每個五角星代表 1 ~ 9 的不同的數字。
這個算式有多少種可能的正確填寫方法?
173 + 286 = 459
295 + 173 = 468
173 + 295 = 468
183 + 492 = 675
以上都是正確的填寫法!
注意:
111 + 222 = 333 是錯誤的填寫法!
因為每個數字必須是不同的!
也就是說:1~9中的所有數字,每個必須出現且僅出現一次!
注意:
不包括數字“0”!
注意:
滿足加法交換率的式子算兩種不同的答案。
所以答案肯定是個偶數!
注意:
只要求計算不同的填法的數目
不要求列出所有填寫法
更不要求填寫源代碼!
*/
package Question10_19;
import java.util.Vector;
public class Question12 {
public static int count;
public static void AllType(Vector<Character> sourse,Vector<Character>result) {
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 = %d\n",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) {
Vector<Character>sourse=new Vector<Character>();
Vector<Character>result=new Vector<Character>();
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. 火柴游戲
/*
【編程題】(滿分34分)
這是一個縱橫火柴棒游戲。如圖[1.jpg],在3x4的格子中,游戲的雙方輪流放置火柴棒。其規則是:
1. 不能放置在已經放置火柴棒的地方(即只能在空格中放置)。
2. 火柴棒的方向只能是豎直或水平放置。
3. 火柴棒不能與其它格子中的火柴“連通”。所謂連通是指兩根火柴棒可以連成一條直線,
且中間沒有其它不同方向的火柴“阻攔”。
例如:圖[1.jpg]所示的局面下,可以在C2位置豎直放置(為了方便描述格子位置,圖中左、下都添加了標記),
但不能水平放置,因為會與A2連通。同樣道理,B2,B3,D2此時兩種方向都不可以放置。但如果C2豎直放置后,
D2就可以水平放置了,因為不再會與A2連通(受到了C2的阻擋)。
4. 游戲雙方輪流放置火柴,不可以棄權,也不可以放多根。直到某一方無法繼續放置,則該方為負(輸的一方)。
游戲開始時可能已經放置了多根火柴。
你的任務是:編寫程序,讀入初始狀態,計算出對自己最有利的放置方法並輸出。
如圖[1.jpg]的局面表示為:
00-1
-000
0100
即用“0”表示空閑位置,用“1”表示豎直放置,用“-”表示水平放置。
【輸入、輸出格式要求】
用戶先輸入整數 n(n<100), 表示接下來將輸入 n 種初始局面,每種局面占3行(多個局面間沒有空白行)。
程序則輸出:每種初始局面情況下計算得出的最佳放置法(行號+列號+放置方式)。
例如:用戶輸入:
2
0111
-000
-000
1111
----
0010
則程序可以輸出:
00-
211
不難猜出,輸出結果的含義為:
對第一個局面,在第0行第0列水平放置
對第二個局面,在第2行第1列垂直放置
注意:
行號、列號都是從0開始計數的。
對每種局面可能有多個最佳放置方法(解不唯一),只輸出一種即可。
例如,對第一個局面,001 也是正解;最第二個局面,201也是正解。
*/
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. 古代賭局
/*
* 【編程題】(滿分23分)
俗話說:十賭九輸。因為大多數賭局的背后都藏有陰謀。不過也不盡然,有些賭局背后藏有的是:“陽謀”。
有一種賭局是這樣的:桌子上放六個匣子,編號是1至6。多位參與者(以下稱玩家)可以把任意數量的錢押在某個編號的匣子上。
所有玩家都下注后,庄家同時擲出3個骰子(骰子上的數字都是1至6)。輸贏規則如下:
1. 若某一個骰子上的數字與玩家所押注的匣子號相同,則玩家拿回自己的押注,庄家按他押注的數目賠付(即1比1的賠率)。
2. 若有兩個骰子上的數字與玩家所押注的匣子號相同,則玩家拿回自己的押注,庄家按他押注的數目的2倍賠付(即1比2的賠率)。
3. 若三個骰子上的數字都與玩家所押注的匣子號相同,則玩家拿回自己的押注,庄家按他押注的數目的6倍賠付(即1比6的賠率)。
4. 若玩家所押注匣子號與某個骰子示數乘積等於另外兩個骰子示數的乘積,則玩家拿回自己的押注,庄家也不賠付(流局)。
5. 若以上規則有同時滿足者,玩家可以選擇對自己最有利的規則。規則執行后,則庄家收獲所有匣子上剩余的押注。
乍一看起來,好像規則對玩家有利,庄家吃虧。但經過大量實戰,會發現局面很難說,於是懷疑是否庄家做了手腳,
庄家則十分爽快地說:可以由玩家提供骰子,甚至也可以由玩家來投擲骰子。
你的任務是:通過編程模擬該過程。模擬50萬次,假定只有1個玩家,他每次的押注都是1元錢,其押注的匣子號是隨機的。
再假定庄家有足夠的資金用於賠付。最后計算出庄家的盈率(庄家盈利金額/押注總金額)。
【輸入、輸出格式要求】
程序無輸入,程序運行輸出庄家的盈率,四舍五入保留到小數后3位。
【注意】
請仔細調試!您的程序只有能運行出正確結果的時候才有機會得分!
請把所有類寫在同一個文件中,調試好后,存入與【考生文件夾】下對應題號的“解答.txt”中即可。
相關的工程文件不要拷入。
請不要使用package語句。
源程序中只能出現JDK1.5中允許的語法或調用。不能使用1.6或更高版本。
*/
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. 源碼變換
/*
超文本標記語言(即HTML),是用於描述網頁文檔的一種標記語言。
HTML通過文本來描述文檔顯示出來應該具有的“樣子”。它主要通過標簽來定義對象的顯示屬性或行為。
如果把java的源文件直接拷貝到HTML文檔中,用瀏覽器直接打開,會發現本來整齊有序的源文件變成了一團遭。
這是因為,文本中的許多回車和空格都被忽略了。而有些符號在html中有特殊的含義,引起了更復雜的局面。
為了源文件能正常顯示,我們必須為文本加上適當的標簽。對特殊的符號進行轉義處理。
常用的有:
HTML 需要轉義的實體:
& ---> &
空格 --->
< ---> <
> ---> >
" ---> "
此外,根據源碼的特點,可以把 TAB 轉為4個空格來顯示。
TAB --->
為了顯示為換行,需要在行尾加<br/>標簽。
為了顯示美觀,對關鍵字加粗顯示,即在關鍵字左右加<b>標簽。比如:
<b>public</b>
對單行注釋文本用綠色顯示,可以使用<font>標簽,形如:
<font color=green>//這是我的單行注釋!</font>
注意:如果“//”出現在字符串中,則注意區分,不要錯誤地變為綠色。
不考慮多行注釋的問題 /* .... */ /*或*/ /** .... */
/*
你的任務是:編寫程序,把給定的源文件轉化為相應的html表達。
【輸入、輸出格式要求】
與你的程序同一目錄下,存有源文件 a.txt,其中存有標准的java源文件。要求編寫程序把它轉化為b.html。
例如:目前的 a.txt 文件與 b.html 文件就是對應的。可以用記事本打開b.html查看轉換后的內容。用瀏覽器打開b.html則可以看到顯示的效果。
注意:實際評測的時候使用的a.txt與示例是不同的。
*/
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("<html>\n");
outStreamWriter.write("<body>\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 = "<font color=green>" + s2 + "</font>";
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 = "<font color=green>" + s2 + "</font>";
tempString = s1 + s2;
}
}
tempString = tempString.replaceAll("\"", """);
tempString = tempString.replaceAll("\t",
" ");
tempString = tempString.replaceAll(" public ",
" <b>public</b> ");
tempString = tempString.replaceAll(" class ",
" <b>class</b> ");
tempString = tempString.replaceAll(" static ",
" <b>static</b> ");
tempString = tempString.replaceAll(" void ",
" <b>void</b> ");
outStreamWriter.write(tempString + "<br/>" + "\r\n");
}
outStreamWriter.write("\n</body>\n");
outStreamWriter.write("</html>\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. 數量周期
/*
復雜現象背后的推動力,可能是極其簡單的原理。科學的目標之一就是發現紛
繁復雜的自然現象背后的簡單法則。愛因斯坦的相對論是這方面的典范例證。
很早的時候,生物學家觀察某區域某種昆蟲的數量(稱為蟲口數)之逐年變化規律,
就十分迷惑:有的時候是逐漸增多達到一個平衡值。有的時候在兩個數字間周期跳動。
有的時候則進入一片混亂,類似隨機數字一樣變化(稱為混沌現象)。
慢慢地,人們從數學中更清晰地觀察到了這一現象,並因此開創了:符號動力學、非線性動力學等研究領域。
一個著名的蟲口數目簡化模型如下:
x' = x * (1 - x) * r
這里,x x' r 都是浮點數。
其中,x 表示當年的蟲口數,x' 表示下一年的蟲口數。
它們的取值范圍在 0 與 1 之間,實際上表示的是:蟲口的總數占環境所能支持的最大數量的比率。
r 是常數(環境參數),r的取值范圍在 [0,4]。
令人驚訝的是:這個簡單的迭代公式有着不同尋常的神秘性質!
一般來說,多次迭代后,蟲口數的穩定模式與x的初始值無關,而與 r 有關!
例如:無論x初始值是多少,當 r = 2.5 的時候,x 多次迭代后會趨向於 0.6。
而當 r = 3.2 的時候,x 的值會趨向於在 0.799 與 0.513 之間周期性擺動。
那么,r = 3.62 的時候,你觀察到有什么周期現象發生嗎?
不需要提交源代碼,只要寫出你的結論即可!
答案寫在:“解答.txt”中,不要寫在這里。
*/
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. 提取子串
/*
串“abcba”以字母“c”為中心左右對稱;串“abba” 是另一種模式的左右對稱。
這兩種情況我們都稱這個串是鏡像串。特別地,只含有1個字母的串,可以看成是第一種模式的鏡像串。
一個串可以含有許多鏡像子串。我們的目標是求一個串的最大鏡像子串(最長的鏡像子串),
如果有多個最大鏡像子串,對稱中心靠左的優先選中。例如:“abcdeefghhgfeiieje444k444lmn”的最大鏡像子串是:“efghhgfe”
下面的靜態方法實現了該功能,請仔細閱讀並分析代碼,填寫空白處的代碼,使得程序的邏輯合理,結果正確。
方法一:
*/
public class Demo02_two {
public static String maxS(String s){
String maxS = "";
char[] c = s.toCharArray();
for(int i=0;i<c.length-1;i++){
int mark = 0; // 下標標記
String temp = ""; // 記錄一個鏡像串
if(c[i]==c[i+1]){
for(;;){ // abba模式
if((i-mark<0)||c[i-mark]!=c[i+mark+1]) break;
mark++;
}
temp = s.substring(i-mark+1,i+mark+1);
}else if((i+2)<c.length&&c[i]==c[i+2]){
for(;;){ // abcba模式
if((i-mark<0)||c[i-mark]!=c[i+mark+2]) break;
mark++;
}
temp = s.substring(i-mark+1,i+mark+2);
}
if(temp.length()>maxS.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. 取球游戲
/*
今盒子里有n個小球,A、B兩人輪流從盒中取球,每個人都可以看到另一個人取了多少個,
也可以看到盒中還剩下多少個,並且兩人都很聰明,不會做出錯誤的判斷。
我們約定:
每個人從盒子中取出的球的數目必須是:1,3,7或者8個。
輪到某一方取球時不能棄權!
A先取球,然后雙方交替取球,直到取完。
被迫拿到最后一個球的一方為負方(輸方)
請編程確定出在雙方都不判斷失誤的情況下,對於特定的初始球數,A是否能贏?
程序運行時,從標准輸入獲得數據,其格式如下:
先是一個整數n(n<100),表示接下來有n個整數。然后是n個整數,每個占一行(整數<10000),表示初始球數。
程序則輸出n行,表示A的輸贏情況(輸為0,贏為1)。
例如,用戶輸入:
4
1
2
10
18
則程序應該輸出:
0
1
1
0
*/
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. 密碼發生器
/*
在對銀行賬戶等重要權限設置密碼的時候,我們常常遇到這樣的煩惱:如果為了好記用生日吧,
容易被破解,不安全;如果設置不好記的密碼,又擔心自己也會忘記;如果寫在紙上,擔心紙張被別人發現或弄丟了...
這個程序的任務就是把一串拼音字母轉換為6位數字(密碼)。
我們可以使用任何好記的拼音串(比如名字,王喜明,就寫:wangximing)作為輸入,程序輸出6位數字。
變換的過程如下:
第一步. 把字符串6個一組折疊起來,比如wangximing則變為:
wangxi
ming
第二步. 把所有垂直在同一個位置的字符的ascii碼值相加,得出6個數字,如上面的例子,則得出:
228 202 220 206 120 105
第三步. 再把每個數字“縮位”處理:就是把每個位的數字相加,得出的數字如果不是一位數字,
就再縮位,直到變成一位數字為止。例如: 228 => 2+2+8=12 => 1+2=3
上面的數字縮位后變為:344836, 這就是程序最終的輸出結果!
要求程序從標准輸入接收數據,在標准輸出上輸出結果。
輸入格式為:第一行是一個整數n(<100),表示下邊有多少輸入行,接下來是n行字符串,就是等待變換的字符串。
輸出格式為:n行變換后的6位密碼。
例如,輸入:
5
zhangfeng
wangximing
jiujingfazi
woaibeijingtiananmen
haohaoxuexi
則輸出:
772243
344836
297332
716652
875843
*/
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. 轉方陣
/*
對一個方陣轉置,就是把原來的行號變列號,原來的列號變行號
例如,如下的方陣:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
轉置后變為:
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
但,如果是對該方陣順時針旋轉(不是轉置),卻是如下結果:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
下面的代碼實現的功能就是要把一個方陣順時針旋轉。
*/
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<n.length;k++){
m[i][k] = n[j--][i]; // 解決一行
}
rotation(n,m,++i,t); // 遞歸解決下一行
}
// 輸出矩陣
public static void print(int[][] t){
for(int[] x: t){
for(int y:x){
System.out.print(y+"\t");
}
System.out.println();
}
}
public static void main(String[] args){
int[][] n = {
{1 ,2 ,3 ,4 },
{5 ,6 ,7 ,8 },
{9 ,10,11,12},
{13,14,15,16}
};
print(n); // 顯示原矩陣
int len = n.length;
int[][] m = new int[len][len]; // 目標矩陣
rotation(n,m,0,len-1); // 矩陣順時針旋轉
System.out.println("順時針旋轉結果:");
print(m); // 顯示目標矩陣
}
}
運行結果:
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