十道java华为编程大赛题目

2025-05-29 0 14

以下华为编程比赛题目都是网上整理得到的,代码都是自己调试过的,由于网上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);

}

}

十道java华为编程大赛题目十道java华为编程大赛题目十道java华为编程大赛题目

方法二:

?

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";

}

}

}

十道java华为编程大赛题目十道java华为编程大赛题目

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));

}

}

十道java华为编程大赛题目

方法二: (若题目规定不能调用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;

}

}

十道java华为编程大赛题目十道java华为编程大赛题目

十道java华为编程大赛题目十道java华为编程大赛题目

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);//去掉尾巴逗号

}

}

十道java华为编程大赛题目

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();

}

}

十道java华为编程大赛题目

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/sheepmu/article/details/23000263

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 十道java华为编程大赛题目 https://www.kuaiidc.com/112311.html

相关文章

发表评论
暂无评论