2013届华为校园招聘机试题

时间:2022-08-01 18:54:18

2013届华为校招机试题

  • 题目一:子串分离
    题目描述:
    通过键盘输入任意一个字符串序列,字符串可能包含多个子串,子串以空格分隔。请编写一个程序,自动分离出各个子串,并使用’,’将其分隔,并且在最后也补充一个’,’并将子串存储。
    如果输入“abc def gh i d”,结果将是abc,def,gh,i,d,
    【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出
    示例
    输入:“abc def gh i d”
    输出:“abc,def,gh,i,d,”

说明:此题比较简单,直接用正则就可以解决;java实现如下

package org.wrh.huaweiproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class InterviewDemo2013 {

public static void main(String[] args) {
String str=input();
substringSplit(str);
}
/*
* 函数功能:输入
* */

private static String input() {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
return br.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/*
* 函数功能:将字符串分割成子串,并输出
* */

public static void substringSplit(String str){
String[] strArray=str.split("\\s+");//利用空格将其分割开来
print(strArray);
}
/*
* 函数功能:输出
* */

public static void print(String [] arr){
for(int i=0;i<arr.length-1;i++){
System.out.print(arr[i]+",");

}
System.out.print(arr[arr.length-1]);

}

}
  • 题目二:逆序链表输出。
    题目描述:
    将输入的一个单向链表,逆序后输出链表中的值。链表定义如下:
    typedef struct tagListNode
    {
    int value;
    struct tagListNode *next;
    }ListNode;

要求实现函数:
void converse(ListNode **head);
【输入】head: 链表头节点,空间已经开辟好
【输出】head: 逆序后的链表头节点
【返回】无
【注意】只需要完成该函数功能算法,中间不需要有任何IO 的输入输出

说明:用于选择的是java实现,因此,实现如下

结点类如下:

package org.wrh.huaweiproject;
/*
* 节点类
* */

public class Node {
/*
* 存储数据信息
* */

public int info;
/*
* 节点的引用,用来指向下一个节点
* */

public Node next;
public Node(){
}
public Node(int info){
this(info,null);

}
public Node(int info,Node next){
this.info=info;
this.next=next;
}



}

实现代码如下:

package org.wrh.huaweiproject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

/*
* 题目二:逆序链表输出。
题目描述:
将输入的一个单向链表,逆序后输出链表中的值。
* */

public class InterviewDemo012013 {

public static void main(String[] args) {
/* 建表
* 并返回头结点
*
* */

Node head=createList();
System.out.println("输出构造后的链表的信息");
print(head);
/*
* 返回反转后的头结点
* */

head=reserve(head);
System.out.println("输出反转后的链表的信息");
print(head);


}

private static Node reserve(Node head) {
/*
* 前一个结点
* */

Node preNode=null;
/*
* 当前结点
* */

Node currentNode=head;
/*
* 临时结点
* */

Node tempNode;
while(currentNode.next!=null){
tempNode=currentNode.next;
currentNode.next=preNode;
preNode=currentNode;
currentNode=tempNode;

}
//当退出while循环之后
currentNode.next=preNode;
head=currentNode;
return head;

}

private static void print(Node head) {
while(head.next!=null){
System.out.print(head.info+" ");
head=head.next;

}
System.out.println(head.info);
}

private static Node createList() {
/*
* 产生随机数
* */

Random r=new Random(47);
/*
* 创建头结点
* */

Node head=new Node(r.nextInt(100));
/*
* 临时结点,
* */

Node tempNode;
/*
* 当前结点
* */

Node current=head;
System.out.println("请输入要创建的结点数:");
String m=null;
try {
m = new BufferedReader(new InputStreamReader(System.in)).readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
* 创建链表
* */

if(m!=null){
for(int i=0;i<Integer.valueOf(m);i++){
tempNode=new Node(r.nextInt(100));
current.next=tempNode;
current=tempNode;

}
}

return head;

}

}

实现起来也不太难,关于链表的反转呀等等这样的我相信大家对其解决思路都比较清晰,对上面的代码也应该就比较好理解,这里不再讲解。