http://blog.csdn.net/shuangshuang37278752/article/details/11365629
华为公司2014届校园招聘软件类上机考试样题 需进行上机考试的岗位:软件开发工程师、操作系统工程师、底层软件开发工程师、云计算开发工程师、DSP工程师 在线考试:机考系统的内核为VS2005及JDK1.7,使用Java答题时,类名必须为“Main”;使用C/C++答题时,使用VS2005支持的数据类型和函数。 题目类型:涉及数组、链表、指针、字符串、循环、枚举、排序等等。 考试时长:2小时 考试题目:3道题(共计320分),初级题(60分),中级题(100),高级题(160分),难度递增。
初级题:从考试成绩中划出及格线
10
个学生考完期末考试评卷完成后,A老师需要划出及格线,要求如下:
(
1
) 及格线是
10
的倍数;
(
2
) 保证至少有
60
%的学生及格;
(
3
) 如果所有的学生都高于
60
分,则及格线为
60
分
C语言版本:
[cpp]
view plaincopyprint?
- #include
- void insertion_sort(int a[10]);
-
- int main()
- {
- int a []={45,78,46,59,72,58,84,91,61,56};
-
- insertion_sort(a);
-
- if(a[0]>=60)
- {
- printf("及格线为60");
- }
- else
- {
-
- int grade = a[4];
-
- int gradeLine = grade/10*10;
-
- printf("及格线为:%d
",gradeLine) ;
- }
-
- }
- void insertion_sort(int a[10])
- {
- int i, j, key;
- for (j = 1; j < 10; j++) {
- key = a[j];
- i = j - 1;
- while (i >= 0 && a[i] > key) {
- a[i+1] = a[i];
- i--;
- }
- a[i+1] = key;
- }
- }
#include
void insertion_sort(int a[10]);
int main()
{
int a []={45,78,46,59,72,58,84,91,61,56};
insertion_sort(a);
if(a[0]>=60)
{
printf("及格线为60");
}
else
{
int grade = a[4];
int gradeLine = grade/10*10;
printf("及格线为:%d
",gradeLine) ;
}
}
void insertion_sort(int a[10])
{
int i, j, key;
for (j = 1; j < 10; j++) {
key = a[j];
i = j - 1;
while (i >= 0 && a[i] > key) {
a[i+1] = a[i];
i--;
}
a[i+1] = key;
}
}
JAVA语言版本:
[java]
view plaincopyprint?
- package huawei;
- import java.util.Arrays;
-
-
-
-
-
- public class GradeScore {
-
- public static void main(String[] args) {
-
- String s1 = "45,78,46,59,72,58,84,91,61,56";
- System.out.println(getscore(s1));
- }
-
- public static String getscore(String score){
-
- String[] scorearray = score.split(",");
-
- int gradeArray[] = new int[scorearray.length];
-
- for(int i=0;i
-
- gradeArray[i] = Integer.valueOf(scorearray[i]);
-
- }
-
- Arrays.sort(gradeArray);
-
- if(gradeArray[0]>=60){
-
- return "60";
-
- }else{
-
- int grade = gradeArray[4];
-
- int gradeLine = grade/10*10;
-
- return String.valueOf(gradeLine);
- }
- }
- }
package huawei;
import java.util.Arrays;
/**
* @author Lee
*
*/
public class GradeScore {
public static void main(String[] args) {
String s1 = "45,78,46,59,72,58,84,91,61,56";
System.out.println(getscore(s1));
}
public static String getscore(String score){
String[] scorearray = score.split(",");
int gradeArray[] = new int[scorearray.length];
for(int i=0;i=60){
return "60";
}else{
int grade = gradeArray[4];
int gradeLine = grade/10*10;
return String.valueOf(gradeLine);
}
}
}
中级题:亮着电灯的盏数
一条长廊里依次装有n(
1
≤ n ≤
65535
)盏电灯,从头到尾编号
1
、
2
、
3
、…n-
1
、n。每盏电灯由一个拉线开关控制。开始,电灯全部关着。
有n个学生从长廊穿过。第一个学生把号码凡是
1
的倍数的电灯的开关拉一下;接着第二个学生把号码凡是
2
的倍数的电灯的开关拉一下;接着第三个学生把号码凡是
3
的倍数的电灯的开关拉一下;如此继续下去,最后第n个学生把号码凡是n的倍数的电灯的开关拉一下。n个学生按此规定走完后,长廊里电灯有几盏亮着。
注:电灯数和学生数一致。
C语言版本:
方法1:
思路:一开始所有的灯都是关着的,初始化flag = 0,如编号为12的灯,只有编号为1、2、3、4、6、12才会触碰这盏灯的开关,即求12的约数,若其约数个数为奇数,则最终该盏灯是亮着的;否则,该盏灯是灭的。
[cpp]
view plaincopyprint?
- #include
-
- int GetLightLampNum(int n);
-
- int main()
- {
-
- int Num_of_LightLamp;
-
- printf("请输入电灯或者学生的个数:");
-
- scanf("%d",&Num_of_LightLamp);
-
- printf("亮着的电灯的个数为:%d
",GetLightLampNum(Num_of_LightLamp));
- }
-
-
-
- int GetLightLampNum(int n)
- {
- int i = 0;
- int j = 0;
- int count = 0;
- int flag = 0;
-
- if( n < 1 || n > 65535)
- return ;
-
- for (i = 1; i <= n; i++)
- {
- flag = 0;
-
- for (j = 1; j <= n; j++)
- {
- if (0 == i % j)
- {
- flag = ~flag;
- }
- }
-
- if (flag)
- {
- count++;
- }
- }
-
- return count;
- }
#include
int GetLightLampNum(int n);
int main()
{
int Num_of_LightLamp;
printf("请输入电灯或者学生的个数:");
scanf("%d",&Num_of_LightLamp);
printf("亮着的电灯的个数为:%d
",GetLightLampNum(Num_of_LightLamp));
}
int GetLightLampNum(int n)
{
int i = 0;
int j = 0;
int count = 0;// 最终亮着的灯数
int flag = 0;// 灯的状态:0表示灭,1表示亮
if( n < 1 || n > 65535)
return ;
for (i = 1; i <= n; i++)//i表示灯的编号
{
flag = 0;//一开始灯都是不亮的
for (j = 1; j <= n; j++)//j表是学生的编号
{
if (0 == i % j)
{
flag = ~flag;
}
}
if (flag)
{
count++;
}
}
return count;
}
测试用例:
方法2:
[cpp]
view plaincopyprint?
- #include
-
- #define MAX_BULB_NUM 65535
-
-
-
-
-
-
-
-
-
-
-
- int GetLightLampNum(int n)
- {
- char Bulb_Flag[MAX_BULB_NUM] = {0};
- unsigned int i;
- unsigned int j = 1;
- unsigned int Count = 0;
- if ((n < 1)||(n > 65535))
- {
- return false;
- }
- while ( j <= n)
- {
- for (int i = 1; i <= n; i++)
- {
- if (0 == (i%j))
- {
- Bulb_Flag[i-1] += 1;
- Bulb_Flag[i-1] = Bulb_Flag[i-1]%2 ;
- }
- }
- j++;
- }
- for (i = 0; i < MAX_BULB_NUM; i++)
- {
- if (1 == Bulb_Flag[i])
- {
- Count++;
- }
- }
- return Count;
- }
-
- int main(void)
- {
- int input;
- scanf("%d",&input);
- printf("%d",GetLightLampNum(input));
- }
#include
#define MAX_BULB_NUM 65535
/*
功能: n个学生按规定走完后,长廊里电灯有几盏亮着。
原型:
int GetLightLampNum(int n);
输入参数:
int n: 电灯/学生的数量。
返回值:
int: 亮着的电灯数量。
*/
int GetLightLampNum(int n)
{
char Bulb_Flag[MAX_BULB_NUM] = {0}; //0代表灯灭,1代表灯亮
unsigned int i;
unsigned int j = 1;
unsigned int Count = 0;
if ((n < 1)||(n > 65535))
{
return false;
}
while ( j <= n)
{
for (int i = 1; i <= n; i++)
{
if (0 == (i%j))
{
Bulb_Flag[i-1] += 1;
Bulb_Flag[i-1] = Bulb_Flag[i-1]%2 ; //if操作用来反转满足条件的灯泡状态
}
}
j++;
}
for (i = 0; i < MAX_BULB_NUM; i++)
{
if (1 == Bulb_Flag[i])
{
Count++;
}
}
return Count;
}
int main(void)
{
int input;
scanf("%d",&input);
printf("%d",GetLightLampNum(input));
}
高级题:地铁换乘
已知
2
条地铁线路,其中A为环线,B为东西向线路,线路都是双向的。经过的站点名分别如下,两条线交叉的换乘点用T1、T2表示。编写程序,任意输入两个站点名称,输出乘坐地铁最少需要经过的车站数量(含输入的起点和终点,换乘站点只计算一次)。
地铁线A(环线)经过车站:A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18
地铁线B(直线)经过车站:B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15
输入:输入两个不同的站名
输出:输出最少经过的站数,含输入的起点和终点,换乘站点只计算一次
输入样例:A1 A3
输出样例:3
C++语言版本:
[cpp]
view plaincopyprint?
- #include
- #include
- #include
- using namespace std;
-
- typedef struct EdgeNode
- {
- int adjvex;
- struct EdgeNode *next;
- }EdgeNode;
-
- typedef struct VertexNode
- {
- string data;
- EdgeNode *first;
- }VertexNode,AdjList[20];
-
- typedef struct Graph
- {
- AdjList vertex;
- int num_vertex;
- int num_edge;
- }Graph;
-
- int visit[20];
- int parent[20];
- int num = 0;
-
- int get_location(Graph g, string s)
- {
- for(int i=0;i
- {
- if(s == g.vertex[i].data)
- return i;
- }
- return -1;
- }
-
- void create_graph(Graph &g)
- {
- int i,j,k;
- string s1,s2;
- cout<<"请输入结点和边的个数:";
- cin>>g.num_vertex>>g.num_edge;
- cout<<"请输入节点:";
- for(i=0;i
- {
- cin>>g.vertex[i].data;
- g.vertex[i].first = NULL;
- }
- cout<<"请输入边:"<
- for(k=0;k
- {
- cin>>s1>>s2;
- i = get_location(g,s1);
- j = get_location(g,s2);
- EdgeNode *p = new EdgeNode();
- p->adjvex = j;
- p->next = g.vertex[i].first;
- g.vertex[i].first = p;
-
- p = new EdgeNode();
- p->adjvex = i;
- p->next = g.vertex[j].first;
- g.vertex[j].first = p;
- }
- }
-
- void dfs(Graph g, string begin, string end)
- {
- int start = get_location(g,begin);
- int stop = get_location(g,end);
- queue<int> q;
- memset(visit,0,sizeof(visit));
- memset(parent,-1,sizeof(parent));
- visit[start] = 1;
- parent[start] = -1;
- q.push(start);
- while(!q.empty())
- {
- int top = q.front();
- q.pop();
- EdgeNode *p = g.vertex[top].first;
- while(p)
- {
- if(!visit[p->adjvex])
- {
- visit[p->adjvex] = 1;
- parent[p->adjvex] = top;
- if(p->adjvex == stop)
- return;
- q.push(p->adjvex);
- }
- p = p->next;
- }
- }
- }
-
- void print_path(Graph g, int end)
- {
- if(parent[end] != -1)
- {
- num++;
- print_path(g,parent[end]);
- cout<<"->"<
- }
- }
-
- void print_path(Graph g, string begin, string end)
- {
- cout<<"最短路径为:"<
- cout<"->";
- num++;
- int j = get_location(g,end);
- print_path(g,j);
- cout<
- cout<<"最短路径长度为"<
- }
-
- int main()
- {
- Graph g;
- string begin,end;
- create_graph(g);
- cout<<"请输入要找的起始站点:";
- cin>>begin>>end;
- dfs(g,begin,end);
- print_path(g,begin,end);
- return 0;
- }
#include
#include
#include
using namespace std;
typedef struct EdgeNode
{
int adjvex;
struct EdgeNode *next;
}EdgeNode;
typedef struct VertexNode
{
string data;
EdgeNode *first;
}VertexNode,AdjList[20];
typedef struct Graph
{
AdjList vertex;
int num_vertex;
int num_edge;
}Graph;
int visit[20];
int parent[20];
int num = 0;
int get_location(Graph g, string s)
{
for(int i=0;i>g.num_vertex>>g.num_edge;
cout<<"请输入节点:";
for(i=0;i>g.vertex[i].data;
g.vertex[i].first = NULL;
}
cout<<"请输入边:"<>s1>>s2;
i = get_location(g,s1);
j = get_location(g,s2);
EdgeNode *p = new EdgeNode();
p->adjvex = j;
p->next = g.vertex[i].first;
g.vertex[i].first = p;
p = new EdgeNode();
p->adjvex = i;
p->next = g.vertex[j].first;
g.vertex[j].first = p;
}
}
void dfs(Graph g, string begin, string end)
{
int start = get_location(g,begin);
int stop = get_location(g,end);
queue q;
memset(visit,0,sizeof(visit));
memset(parent,-1,sizeof(parent));
visit[start] = 1;
parent[start] = -1;
q.push(start);
while(!q.empty())
{
int top = q.front();
q.pop();
EdgeNode *p = g.vertex[top].first;
while(p)
{
if(!visit[p->adjvex])
{
visit[p->adjvex] = 1;
parent[p->adjvex] = top;
if(p->adjvex == stop) //找到返回
return;
q.push(p->adjvex);
}
p = p->next;
}
}
}
void print_path(Graph g, int end)
{
if(parent[end] != -1)
{
num++;
print_path(g,parent[end]);
cout<<"->"<";
num++;
int j = get_location(g,end);
print_path(g,j);
cout<>begin>>end;
dfs(g,begin,end);
print_path(g,begin,end);
return 0;
}
JAVA语言版本:
[java]
view plaincopyprint?
- package huawei;
-
- import java.util.*;
-
-
-
-
-
-
- public class Main {
-
- private static int INVALID_POSITION = 255;
-
- class BusLine {
-
- String busstop[];
-
- String lineName;
-
- public BusLine(String line) {
-
- String[] stops = line.split(" ");
-
- this.busstop = new String[stops.length];
-
- for (int i = 0; i < stops.length; i++) {
-
- this.busstop[i] = stops[i];
-
- lineName = stops[0].substring(0, 1);
-
- }
-
- }
-
-
-
- int getStopPosition(String point) {
-
- for (int i = 0; i < busstop.length; i++) {
-
- if (busstop[i].equals(point)) {
-
- return i;
-
- }
-
- }
-
- return INVALID_POSITION;
-
- }
-
- int getDistance(String pointA, String pointB) {
-
- int positionA = 0;
-
- int positionB = 0;
-
- int len = 0;
-
- positionA = getStopPosition(pointA);
-
- positionB = getStopPosition(pointB);
-
- if (positionA != INVALID_POSITION && positionB != INVALID_POSITION) {
-
- len = Math.abs(positionA - positionB) + 1;
-
- if (lineName.equals("A") && len > (busstop.length - len + 2)) {
-
- len = (busstop.length - len + 2);
-
- }
-
- return len;
-
- }
-
- return INVALID_POSITION;
-
- }
-
- }
-
- public int getRide(String pointA, String pointB) {
-
- int i = 0;
-
- int min = 255;
-
- BusLine lineA = new BusLine(
- "A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18");
-
- BusLine lineB = new BusLine(
- "B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15");
-
- int[] way = { 255, 255, 255, 255, 255, 255, 255, 255 };
-
- way[0] = lineA.getDistance(pointA, pointB);
-
- way[1] = lineB.getDistance(pointA, pointB);
-
- way[2] = lineA.getDistance(pointA, "T1")
- + lineB.getDistance(pointB, "T1") - 1;
-
- way[3] = lineB.getDistance(pointA, "T1")
- + lineA.getDistance(pointB, "T1") - 1;
-
- way[4] = lineA.getDistance(pointA, "T2")
- + lineB.getDistance(pointB, "T2") - 1;
-
- way[5] = lineB.getDistance(pointA, "T2")
- + lineA.getDistance(pointB, "T2") - 1;
-
- way[6] = lineB.getDistance(pointA, "T1")
- + lineB.getDistance(pointB, "T2")
- + lineA.getDistance("T1", "T2") - 2;
-
- way[7] = lineB.getDistance(pointA, "T2")
- + lineB.getDistance(pointB, "T1")
- + lineA.getDistance("T1", "T2") - 2;
-
- for (i = 0; i < 7; i++) {
-
- if (min > way[i]) {
-
- min = way[i];
-
- }
-
- }
-
- return min;
-
- }
-
- public static void main(String[] args) {
-
- Main m = new Main();
-
- Scanner cin = new Scanner(System.in);
-
- String inputStr = cin.nextLine();
-
- String stops[] = inputStr.split(" ");
-
- System.out.println(m.getRide(stops[0], stops[1]));
-
- }
-
- }
package huawei;
import java.util.*;
/**
*
* @author Lee
*
*/
public class Main {
private static int INVALID_POSITION = 255;
class BusLine {
String busstop[];
String lineName;
public BusLine(String line) {
String[] stops = line.split(" ");
this.busstop = new String[stops.length];
for (int i = 0; i < stops.length; i++) {
this.busstop[i] = stops[i];
lineName = stops[0].substring(0, 1);
}
}
/* get the stop position from the line */
int getStopPosition(String point) {
for (int i = 0; i < busstop.length; i++) {
if (busstop[i].equals(point)) {
return i;
}
}
return INVALID_POSITION;
}
int getDistance(String pointA, String pointB) {
int positionA = 0;
int positionB = 0;
int len = 0;
positionA = getStopPosition(pointA);
positionB = getStopPosition(pointB);
if (positionA != INVALID_POSITION && positionB != INVALID_POSITION) {
len = Math.abs(positionA - positionB) + 1;
if (lineName.equals("A") && len > (busstop.length - len + 2)) {
len = (busstop.length - len + 2);
}
return len;
}
return INVALID_POSITION;
}
}
public int getRide(String pointA, String pointB) {
int i = 0;
int min = 255;
BusLine lineA = new BusLine(
"A1 A2 A3 A4 A5 A6 A7 A8 A9 T1 A10 A11 A12 A13 T2 A14 A15 A16 A17 A18");
BusLine lineB = new BusLine(
"B1 B2 B3 B4 B5 T1 B6 B7 B8 B9 B10 T2 B11 B12 B13 B14 B15");
int[] way = { 255, 255, 255, 255, 255, 255, 255, 255 };
way[0] = lineA.getDistance(pointA, pointB);
way[1] = lineB.getDistance(pointA, pointB);
way[2] = lineA.getDistance(pointA, "T1")
+ lineB.getDistance(pointB, "T1") - 1;
way[3] = lineB.getDistance(pointA, "T1")
+ lineA.getDistance(pointB, "T1") - 1;
way[4] = lineA.getDistance(pointA, "T2")
+ lineB.getDistance(pointB, "T2") - 1;
way[5] = lineB.getDistance(pointA, "T2")
+ lineA.getDistance(pointB, "T2") - 1;
way[6] = lineB.getDistance(pointA, "T1")
+ lineB.getDistance(pointB, "T2")
+ lineA.getDistance("T1", "T2") - 2;
way[7] = lineB.getDistance(pointA, "T2")
+ lineB.getDistance(pointB, "T1")
+ lineA.getDistance("T1", "T2") - 2;
for (i = 0; i < 7; i++) {
if (min > way[i]) {
min = way[i];
}
}
return min;
}
public static void main(String[] args) {
Main m = new Main();
Scanner cin = new Scanner(System.in);
String inputStr = cin.nextLine();
String stops[] = inputStr.split(" ");
System.out.println(m.getRide(stops[0], stops[1]));
}
}