以下華為編程比賽題目都是網上整理得到的,代碼都是自己調試過的,由于網上java答案較少,歡迎大家批評指正,也希望對準備華為上機的童鞋們有一點點幫助。在練習的過程中成長,加油!~~
1. 就餐抽查(30分)
問題描述:
某公司由于人多,午餐分為多批次就餐,嚴格要求每批次就餐時間。并定期抽查就餐情況。請編寫程序實現就餐抽查情況。
要求實現函數:
void check_lunch(int num, int time,int input[], int output[])
【輸入】 int num,就餐總人數
int time,就餐分批數
char input[],就餐情況
【輸出】 char output[], 違規就餐情況
【返回】 無
注:對就餐分3批的情況,12人就餐,正確的就餐情況應如下分布[1,2,3,1,2,3,1,2,3,1,2,3],不符合該分布的即是違規,輸出時對相應位置0。
示例
1) 輸入:num = 12,time = 3,input =[1,2,3,3,1,3,1,1,1,1,2,3]
輸出:output = [1,2,3,0,0,3,1,0,0,1,2,3]
2) 輸入:num = 11,time = 4,intput = [1,2,3,4,2,3,3,4,1,2,3]
輸出:output = [1,2,3,4,0,0,3,4,1,2,3]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.sheepmu.text; import java.util.arrays; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ int num= 11 ,time= 4 ; int [] input={ 1 , 2 , 3 , 4 , 2 , 3 , 3 , 4 , 1 , 2 , 3 }; // int[] output=new int[]{}; int [] output= new int [num]; hwcompetition hwc= new hwcompetition(); hwc.check_lunch( num, time, input, output); } void check_lunch( int num, int time, int input[], int output[]){ system.out.println(arrays.tostring(input)); int j= 0 ; for ( int i= 0 ;i<num;i++){ int yushu=(i+ 1 )%time; if (yushu!= 0 ){ if (input[i]==yushu){ output[j]=yushu; } else output[j]= 0 ; j++; } else { //余數==0的情況 if (input[i]==time){ output[j]=time; } else output[j]= 0 ; j++; } } system.out.println(arrays.tostring(output)); } } |
2. 輸入聯想(30分)
問題描述:
輸入聯想功能是非常實用的一個功能,請編程實現類似功能。
要求實現函數:
void auto_complete(char *str, char *tmp,char *output)
【輸入】 char *str,候選字符串
char *tmp,輸入字符串
【輸出】 int *output,聯想匹配的字符串
【返回】 無
注:候選字符串以空格隔開,輸入字符串僅從字符串開始處匹配。將匹配的子字符串輸出,同樣以空格隔開。如無匹配成功的子字符串,則輸出空字符串。
示例
1) 輸入:str = chengdu chongqing,tmp = c
輸出:output = chengdu chongqing
2) 輸入:str = chengdu chongqing,tmp = che
輸出:end = chengdu
3)輸入:str = beijing nanjing,tmp = jing
輸出:end =
方法一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
package com.sheepmu.text; import java.util.arraylist; import java.util.list; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ string str= "chengdu chongqing" ; string tmp= "che" ; string output= "" ; hwcompetition hwc= new hwcompetition(); hwc.auto_complete( str,tmp, output); } void auto_complete(string str,string tmp, string output){ string[] strs=str.split( "\\s" ); list<string> list= new arraylist<string>(); for ( int i= 0 ;i<strs.length;i++) list.add(strs[i]); system.out.println( "list--->" +list); system.out.println( "tmp--->" +tmp); char [] tmps=tmp.tochararray(); int len_list=list.size(); int len_t=tmps.length; for ( int j= 0 ;j<len_list;j++){ int len_list_j=list.get(j).length(); char [] list_j=list.get(j).tochararray(); for ( int k= 0 ;k<len_t;k++){ if (len_t>len_list_j){ list.remove(j); len_list--; //!!!!!!!!!!!!!!! j--; //!!!!!!!!!!!!! 要是不這樣28行會出問題,因為remove后size變成了1,但是j即index卻變成了1 break ; } else { //temp的長度小于帶收索的長度 if (tmps[k]!=list_j[k]){ list.remove(j); len_list--; //!!!!!!!!!!!!!!! j--; //!!!!!!!!!!!!! break ; } } } } // output= list.tostring();//這樣會 [chengdu] ,即會帶有兩邊的[] if (!list.isempty()){ stringbuffer sb= new stringbuffer(); sb.append( "end=" ); for (string result:list){ sb.append(result+ " " ); //加上空格!!最后再給去掉尾巴的“ ”; } output=sb.tostring().trim(); //!! } else { output= "end=" ; } system.out.println(output); } } |
方法二:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.sheepmu.text; import java.util.arraylist; import java.util.list; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ string str= "chengdu chongqing" ; string tmp= "che" ; string output= "" ; hwcompetition hwc= new hwcompetition(); hwc.auto_complete( str,tmp, output); } void auto_complete(string str,string tmp, string output){ string[] strs=str.split( "\\s" ); //和下面一樣,應該是只有\s,\d啊之類的才加\ list<string> list= new arraylist<string>(); for ( int i= 0 ;i<strs.length;i++) list.add(strs[i]); system.out.println( "list--->" +list); system.out.println( "tmp--->" +tmp); int len_list=list.size(); for ( int j= 0 ;j<len_list;j++){ //還有一個好方法:!list.get(j).startswith(tmp);!!!!!!!!!!!!!!!!!!!! if (!list.get(j).matches(tmp+ "[a-z]*" )){ //正則表達式就是爽啊!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! list.remove(j); len_list--; j--; } } if (!list.isempty()){ stringbuffer sb= new stringbuffer(); sb.append( "end=" ); for (string result:list){ sb.append(result+ " " ); //加上空格!!最后再給去掉尾巴的“ ”; } output=sb.tostring().trim(); //!! } else { output= "end=" ; } system.out.println(output); } } |
3. 農場計數問題(20分)
問題描述:
已知某農場中有一群雞和兔子,總共有m個頭和n只腳,計算總共有多少雞和兔子 ·
要求實現函數:
public string getfowlsnum(int iheadnum, int ifootnum, arraylist ichickennum, arraylist irabbitnum)
【輸入】iheadnum: 總共頭的數量
ifootnum: 總共腳的數量
【輸出】ichickennum: 雞的數量
irabbitnum: 兔子的數量
【返回】"0": 找到符合要求的雞和兔子的數量
"-1": 未找到符合要求的數量
示例
輸入:iheadnum =201, ifootnum=604
輸出:ichickennum.add(100), irabbitnum.add(101) 返回:"0"
輸入:iheadnum =201, ifootnum=123
輸出: ichickennum.add(0), irabbitnum.add(0) 返回:"-1"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.sheepmu.text; import java.util.arraylist; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ int iheadnum= 201 ; int ifootnum= 604 ; arraylist ichickennum= new arraylist(); arraylist irabbitnum= new arraylist(); hwcompetition hwc= new hwcompetition(); hwc.getfowlsnum( iheadnum,ifootnum,ichickennum,irabbitnum); } public string getfowlsnum( int iheadnum, int ifootnum,arraylist ichickennum,arraylist irabbitnum){ if (ifootnum% 2 != 0 ){ //!!!!! system.out.println( "ichickennum.add(0),irabbitnum.add(0)" ); return "-1" ; //如果腳的數量為奇數,則明顯不對, } else { int ji= 2 *iheadnum-ifootnum/ 2 ; int tui=ifootnum/ 2 - iheadnum; if (ji>= 0 &&tui>= 0 ) system.out.println( "ichickennum.add(" +ji+ "),irabbitnum.add(" +tui+ ")" ); return "0" ; } } } |
4.字符串壓縮(30分)
問題描述:
將給定的字符串,按照規格壓縮,輸出壓縮后的字符串。壓縮規格為:相同字符連續,則壓縮為“字符+數字個數”,如”aaaa”壓縮為”a4”
注:1、僅是單個字符連續才壓縮,如babababa則不能壓縮
2、待壓縮字符串中不包含數字和轉義符 ·
要求實現方法:
public string compressstr(string srcstr) 【輸入】srcstr: 待壓縮的字符串
【輸出】無
【返回】 壓縮后的字符串
示例
輸入:srcstr = "aaacccddef" 返回:"a3c3d2ef"
方法一:(用arraylist)詳情見華為上機匯總第 8 題
方法二:(用string,可讀性不如上者,就當為熟悉api吧)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package com.sheepmu.text; import java.util.arraylist; import java.util.list; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ string str= "abcddef" ; hwcompetition hwc= new hwcompetition(); string result=hwc.compressstr(str); system.out.println(result); } public string compressstr(string str){ stringbuffer sb= new stringbuffer(); for ( int i= 0 ;i<str.length();i++){ if (str.length()== 0 ) break ; if (str.length()== 1 ) sb.append(str.charat(i)); //針對aaacccddef for ( int j=i+ 1 ;j<str.length();j++){ if (str.charat(i)==str.charat(j)){ if (j==str.length()- 1 ){ //針對當后面一直沒有出現不同時:aaacccddeffff sb.append(str.length()).append(str.charat(i)); str=str.substring(j); //長度只剩0了。一定要賦給新的str!!!!!!!!!!!!!!!!!!!!!!!! break ; } } else { //遇到不等時 if (j== 1 ) sb.append(str.charat(i)); else sb.append(j).append(str.charat(i)); system.out.println(sb.tostring()); str=str.substring(j); i--; break ; } } } return sb.tostring(); } } |
5. 排序算法(20分)
問題描述:
將給定的無序整數數組降序排列后輸出,輸入的無序數組長度為n,類型為unsigned int
要求實現函數
void dscsort (const int inputarray[], unsigned int n, int outputarray[])
【輸入】inputarray: 給定的無序數組
n: 數組長度
【輸出】outputarray: 排序后的數組
【返回】無
示例
輸入:inputarray={1,5,4,8,3,2,9,6,7,0}
輸出:outputarray={9,8,7,6,5,4,3,2,1,0}
方法一: (直接調用api)思路:升序再倒過來輸出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.sheepmu.text; import java.util.arrays; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ int [] inputarray={ 1 , 5 , 4 , 8 , 3 , 2 , 9 , 6 , 7 , 0 }; int n=inputarray.length; int [] outputarray= new int [n]; hwcompetition hwc= new hwcompetition(); hwc.dscsort (inputarray,n,outputarray); } void dscsort ( int inputarray[], int n, int outputarray[]){ arrays.sort(inputarray); //升序 int i= 0 ; while (--n>= 0 ){ outputarray[i++]=inputarray[n]; } system.out.println(arrays.tostring(outputarray)); } } |
方法二: (若題目規定不能調用api)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package com.sheepmu.text; import java.util.arrays; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ int [] inputarray={ 1 , 5 , 4 , 8 , 3 , 2 , 9 , 6 , 7 , 0 }; int n=inputarray.length; int [] outputarray= new int [n]; hwcompetition hwc= new hwcompetition(); hwc.dscsort (inputarray,n,outputarray); system.out.println(arrays.tostring(inputarray)); } void dscsort ( int inputarray[], int n, int outputarray[]){ //自己寫:快排降序 int high= 0 ; int low=n- 1 ; sort(inputarray,high,low); } void sort( int inputarray[], int high, int low){ int i,j,temp; i=high; //高端下標 j=low; //低端下標 temp=inputarray[i]; //取第一個元素為標準元素。 while (i<j){ //遞歸出口是 low>=high while (i<j&&temp>inputarray[j]) //后端比temp小,符合降序,不管它,low下標前移 j--; //while完后指比temp大的那個 if (i<j){ inputarray[i]=inputarray[j]; i++; } while (i<j&&temp<inputarray[i]) i++; if (i<j){ inputarray[j]=inputarray[i]; j--; } } //while完,即第一盤排序 inputarray[i]=temp; //把temp值放到它該在的位置。 if (high<i) //注意,下標值 sort(inputarray,high,i- 1 ); //對左端子數組遞歸 if (i<low) //注意,下標值 sort(inputarray,i+ 1 ,low); //對右端子數組遞歸 ;對比上面例子,其實此時i和j是同一下標!!!!!!!!!!!!! } } |
6.查找最大的不重復數(30分)
問題描述
如果一個數字十進制表達時,不存在連續兩位相同,則稱之為“不重復數”。例如,105、1234和12121都是“不重復數”,而11、100和1225不是。給定一個正整數a,返回大于a的最小“不重復數”。a小于100000
要求實現函數
int getnotrepeatnum(int ivalue)
【輸入】lvalue: 給定的數字,返回大于該值的最小不重復數
【輸出】無
【返回】大于ivalue的最小不重復數
示例
輸入:ivalue =54
返回: 56
輸入:ivalue =10
返回: 12
輸入:ivalue =98
返回: 101
輸入:ivalue =21099
返回: 21201
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package com.sheepmu.text; import java.util.arrays; import java.util.scanner; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ scanner input= new scanner(system.in); int a=input.nextint(); system.out.println( "輸入的數字為---->" +a) ; hwcompetition hwc= new hwcompetition(); int result=hwc.getnotrepeatnum(a); system.out.println( "返回的大于" +a+ "的最小不重復數---->" +result) ; } int getnotrepeatnum( int ivalue){ int i= 0 ; for (i=ivalue+ 1 ;i< 100000 ;i++){ if (!isrepeatnum(i)){ break ; //!!!不然要白白運行好多好多次 } } return i; } public boolean isrepeatnum( int a){ string str=a+ "" ; char [] cs=str.tochararray(); int len=cs.length; for ( int i= 0 ;i<len- 1 ;i++){ //因為后面要i+1,如果是i<len就要下標越界。 if (cs[i]==cs[i+ 1 ]) return true ; } return false ; } } |
7. 撲克牌比較(30分)
問題描述:
在撲克中,牌的類型包括:a(1),2,3,4,5,6,7,8,9,t(10),j(11),q(12),k(13),d(小鬼devilkin),b(大鬼belial)。
請做一個簡單的程序,輸入兩張牌的字符,比如"2"和"k",判斷牌的大小,規則如下:
b>d>2>a>k>q>j>10....>3 最小的為3
判斷規則:比較cfirstcard和csecondcard,如果firstcar大,那么返回1;如果相同,返回0;如果firstcar小,返回-1。
要求實現函數:
int compareonecard(char cfirstcard, char csecondcard)
【輸入】 char cfirstcard:需要比較的第一張牌
char csecondcard: 需要比較的第二張牌
注意:輸入的為字符'a','2',…,'9','t','j','q','k','d','b'
【返回】 int類型:返回兩張牌的比較結果
注意:不用考慮輸入的合法性,這個由函數的使用者保證。輸入的牌均為字符'1','2'…'9',大寫的'a','t','j','q','k','d','b'。
舉例:
輸入:'4'、'5',返回:-1
輸入:'6'、'6',返回:0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package com.sheepmu.text; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ char cfirstcard= '5' ; char csecondcard= '6' ; hwcompetition hwc= new hwcompetition(); int result=hwc.compareonecard(cfirstcard,csecondcard); system.out.println( "比較的結果" +result); } int compareonecard( char cfirstcard, char csecondcard){ int if =getrealln(cfirstcard); system.out.println( "if--->" + if ); int is=getrealln(csecondcard); system.out.println( "is--->" + is); return if >is ? 1 : if <is? - 1 : 0 ; //不需要添括號 } int getrealln( char c){ int value= 0 ; switch (c){ case 't' : value= 10 ; break ; case 'j' : value= 11 ; break ; case 'q' : value= 12 ; break ; case 'k' : value= 13 ; break ; case 'a' : value= 14 ; break ; case '2' : value= 15 ; break ; case 'd' : value= 16 ; break ; case 'b' : value= 17 ; break ; case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : // value=c; // 超級笨蛋錯誤!!!!! 若輸入9,'9'=57!!!!字符9對于的值不是數字9而是該字符本身對于的值。 value=integer.parseint(c+ "" ) ; break ; } return value; } } |
8. 干瞪眼(30分)
問題描述:
在成都,流行一種撲克游戲叫“干瞪眼”。使用撲克牌,包括:a(1),2,3,4,5,6,7,8,9,t(10),j(11),q(12),k(13)。
注意:10用t替換,這里暫時不考慮大鬼和小鬼。
兩手牌的大小規則如下:
a) 單牌:4比3大,5比4大,只有兩張牌剛好大一點時才能進行比較,比較順序為:a>k>q>j>t>9>8>7>6>5>4>3。
比如:6大于5,但是不能比4大,6和4不能比較。單牌2屬于特殊牌,他可以和其他所有普通單牌比較,并且是最大的。
請注意3,他不能大于任何牌。
b) 對子:即兩張牌的點數相同,規則和單牌相似,也需要進行類似處理。兩個2是特殊對子,可以大于所有的其他對子。
注意:對子和單牌是不能進行比較的。
c) 炸彈:3個點數相同的牌。炸彈可以大于任何單張和對子,炸彈之間的比較不用像單牌和對子那樣,只能大一點才能比較。
只要滿足:222>aaa>kkk>qqq>jjj>ttt>…>333的規則的即可。即222是最大的,aaa可以大于kkk,也可以大于333。
d) 其他規則暫不考慮實現
現在請你實現一個程序,自動判斷兩手牌的大小,注意:輸入的牌只會出現3種類型:單張,對子,炸彈。張數最多3張。
不會出現2個單牌。比如”25”,也不會出現一個對子加單牌,比如”334”等,類似輸入異常你可以不用考慮。
但是pfirstcards為單牌,psecondcards為對子,類似的組合輸入是合法的。
要求實現函數:
int comparecards(char *pfirstcards, char *psecondcards)
【輸入】 char *pfirstcards:需要比較的第一手牌
char *psecondcards:需要比較的第二手牌
【返回】 int 類型,返回值說明:
如果pfirstcards和 psecondcards無法比較,比如”3”和”6”;”55”和”6”等,返回0。
如果pfirstcards大于psecondcards,返回1。
如果pfirstcards等于 psecondcards,返回2。
如果pfirstcards小于 psecondcards,返回3。
注意:不用考慮輸入的合法性,這個由函數的使用者保證。輸入的牌均為字符'1','2'..'9',大寫的'a','t','j','q','k'。
示例
輸入: “77”、 “33”,返回:0
輸入: “77”、 “77”,返回:2
思路:1. 1vs2 or 2vs1 此情況只需判斷長度即可得出結果,無需后續比較
2. 1vs1 or 2 vs2 同一種比較方式
3. 3vs3 和情況2不同的比較方式
4. 3 vs 非3 or 非3vs3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package com.sheepmu.text; import java.util.arrays; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ string pfirstcards= "qq" ; string psecondcards= "444" ; hwcompetition hwc= new hwcompetition(); int result=hwc.comparecards(pfirstcards,psecondcards); system.out.println( "比較的結果" +result); } int comparecards( string pfirstcards, string psecondcards){ int len1=pfirstcards.length(); int len2=psecondcards.length(); if ((len1== 1 &&len2== 2 )||(len1== 2 &&len2== 1 )) // 1vs2,無法比較。為提供效率,能先搞定的搞定,這種情況就不需要執行后面的了。 return 0 ; int [] is1=getreallns(pfirstcards); int [] is2=getreallns(psecondcards); // system.out.println(arrays.tostring(is1));//[12, 12] // system.out.println(arrays.tostring(is2));//[4, 4, 4] if ((len1== 1 &&len2== 1 )||(len1== 2 &&len2== 2 )){ //1vs1 或者2vs2,比較方式是一樣的。其實不用天括號 if (math.abs(is1[ 0 ]-is2[ 0 ])== 1 ) //有題目知,長度為2肯定是對子情況 return is1[ 0 ]-is2[ 0 ]> 0 ? 1 : 3 ; else if (is1[ 0 ]==is2[ 0 ]) return 2 ; else return 0 ; } if (len1== 3 &&len2== 3 ) //炸彈不可能相等,因為一副牌沒有6個一樣的。 return is1[ 0 ]>is1[ 0 ]? 1 : 3 ; if (len1== 3 &&len2< 3 ||len1< 3 &&len2== 3 ) return len1== 3 ? 1 : 3 ; return 0 ; //其實測試用例應該永遠都不會執行這一句。 } int [] getreallns(string s){ int len=s.length(); int [] cs = new int [len]; for ( int i= 0 ;i<len;i++){ cs[i]=getrealln(s.charat(i)); } return cs; } int getrealln( char c){ int value= 0 ; switch (c){ case 't' : value= 10 ; break ; case 'j' : value= 11 ; break ; case 'q' : value= 12 ; break ; case 'k' : value= 13 ; break ; case 'a' : value= 14 ; break ; case '2' : value= 15 ; break ; case '3' : case '4' : case '5' : case '6' : case '7' : case '8' : case '9' : // value=c; // 超級笨蛋錯誤!!!!! 若輸入9,'9'=57!!!!字符9對于的值不是數字9而是該字符本身對于的值。 value=integer.parseint(c+ "" ) ; break ; } return value; } } |
9. 矩陣轉置(20分)
問題描述:
將一個n*n矩陣的行列互換。 ·
要求實現函數:
public string matrixtranspose (string inarr, int n)
【輸入】inarr: 輸入的字符矩陣
n: n*n矩陣的行數
【返回】轉置后的字符矩陣 注:
輸入輸出的矩陣都是以一維形式保存的二維數組,比如輸入為"1,2,3,4,5,6,7,8,9",實際上表示如下3*3的矩陣:
1,2,3,4,5,6,7,8,9
示例
輸入inarr ="1,2,3,4,5,6,7,8,9",n=3 返回:"1,4,7,2,5,8,3,6,9"
注:筆者人為的把題目稍微添成了每項為字符串而不是字符。字符更簡單,字符串時注意截取時,13不是1,3
方法一:被題目忽悠的用了矩陣。就當是熟悉下二維數組嘛
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package com.sheepmu.text; import java.util.arrays; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ string inarr= "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16" ; int n= 4 ; system.out.println( "inarr---->" +inarr); hwcompetition hw= new hwcompetition(); string result=hw.matrixtranspose(inarr,n); system.out.println( "result---->" +result); } public string matrixtranspose(string inarr, int n){ //要把字符串中的逗號去掉,不然逗號也是string的一個下標值了,輸出時再加上就是咯 string[] ss=inarr.split( "," ); string[][] css= new string[n][n]; int k= 0 ; for ( int i= 0 ;i<n;i++){ //字符串轉換為二維數組 for ( int j= 0 ;j<n;j++){ css[i][j]=ss[k]; k++; } } stringbuffer sb= new stringbuffer(); for ( int i= 0 ;i<n;i++){ // 二維數組逆置 for ( int j= 0 ;j<n;j++){ sb.append(css[j][i]+ "," ); //尾巴多了一個, } } return sb.substring( 0 , sb.length()- 1 ); } } |
方法二:更簡單,完全不需矩陣。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.sheepmu.text; import java.util.arrays; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ string inarr= "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16" ; int n= 4 ; system.out.println( "inarr---->" +inarr); hwcompetition hw= new hwcompetition(); string result=hw.matrixtranspose(inarr,n); system.out.println( "result---->" +result); } public string matrixtranspose(string inarr, int n){ //要把字符串中的逗號去掉,不然逗號也是string的一個下標值了,輸出時再加上就是咯 string[] ss=inarr.split( "," ); stringbuffer sb= new stringbuffer(); for ( int i= 0 ;i<n;i++){ //對于題目例子: 需要的下標順序是 036147258 for ( int j=i;j<ss.length;j+=n){ sb.append(ss[j]+ "," ); //尾巴多了逗號 } } return sb.substring( 0 , sb.length()- 1 ); //去掉尾巴逗號 } } |
10.路燈(20分)
某省會城市街道縱橫交錯,為了監控路燈的運行狀況,每條街道使用一個數字字符串標識該街道上所有路燈的運行狀況。
假設路燈只有如下3種狀態(分別用數字0, 1, 2標識,一盞路燈只對應其中一種狀態):
0 標識路燈熄滅;
1 標識路燈開啟;
2 標識路燈故障;
請根據輸入的字符串,找出該街道上連續的處于相同狀態的路燈的最大個數。若兩種狀態的路燈數量相同,則返回最先出現的路燈狀態。
輸入
街道上連續的路燈組成的狀態字符串。字符串中只包含數字,每個路燈的狀態為0,1,2中的一種狀態。如“1101”代表4盞路燈,第3盞路燈為熄滅狀態,其它3盞為開啟狀態。
輸出
連續為相同狀態的路燈的最大數量;
上述路燈的狀態;
要求:先輸出數量,再輸出狀態,兩個整數間采用一個空格間隔。如輸出:
53 2
樣例輸入
112200111
樣例輸出
3 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.sheepmu.text; import java.util.arrays; /* * @author sheepmu */ public class hwcompetition { public static void main(string[] args){ string s= "112001110" ; //這類題一定要注意最后全部相同的情況,即沒有再出現與之不同的,不然很容易死循環。 system.out.println( "inarr---->" +s); hwcompetition hw= new hwcompetition(); string result=hw.ludeng(s); system.out.println( "題目結果---->" +result); } public string ludeng(string s){ //要把字符串中的逗號去掉,不然逗號也是string的一個下標值了,輸出時再加上就是咯 char [] as=s.tochararray(); int len=as.length; int maxc= 1 ; // int mubiaobindex=0;//不要制造多余變量 // int mubiaovalue=as[0]; int bindex= 0 ; //如果需要返回最大連續的第一個下標,如此時的6;若題目有此需求:則設置兩個變量:“每部分”的次數+首次出現的下標。 char value=as[ 0 ]; //若題目需要返回最大長度部分的值即本題狀態的1 for ( int i= 0 ;i<len- 1 ; ){ int count= 1 ; //每次外層循環是把count令為1,開始新的計數 bindex=i; value=as[i]; for ( int j=i+ 1 ;j<len;j++){ if (as[i]!=as[j]){ i=j; break ; } else { i++; //!!!!!!!!!!!如果后面沒有出現不同的;如果不加這一句,外層循環會一直執行。 count++; } } if (count>maxc){ maxc=count; // mubiaobindex=bindex; // mubiaovalue=value; system.out.println( "maxc--->" +maxc+ " 起始下標---->" +bindex+ " 狀態---->" +value); } } stringbuffer sb= new stringbuffer(); return sb.append(maxc+ " " ).append(value).tostring(); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/sheepmu/article/details/23000263