申请华为暑期实习生,前两天收到通知机试,给了样题,于是就练手做了一下。样题链接,因为前两道比较简单,就不在这里写,大家可百度之。第三题有点难度,网上大多用佛洛依德算法,我这里用的是广度搜索,和大家分享一下~
原题描述:
已知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
我的代码如下:
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
using namespace std;
string A[]={"A1","A2", "A3","A4","A5","A6","A7","A8","A9","T1","A10","A11","A12","A13","T2","A14","A15","A16","A17","A18"};
string B[]={"B1","B2", "B3","B4","B5","T1" ,"B6","B7","B8","B9","B10","T2","B11","B12","B13","B14","B15"};
bool a[20];
bool b[17];
struct node{
string station;
int count;
node(string s){
station = s;
count = 1;
}
node(string s,int c){
station = s;
count = c;
}
};
int find(string*arr,int size,string target){
for(int i = 0; i <size; i++)
if(arr[i]==target) return i;
}
int bfs(string s1,string s2){
queue<node> yu;
int index;
bool flag = false;
yu.push(node(s1));
memset(a,false,sizeof(a));
memset(b,false,sizeof(b));
while(!yu.empty()){
node now = yu.front();
yu.pop();
if(now.station == s2) return now.count;
if(now.station[0]=='A'){
index = find(A,20,now.station);
if(!a[(index+1)%20]) {yu.push(node(A[(index+1)%20],now.count+1));};
if(index-1>=0&&!a[index-1]) {yu.push(node(A[(index-1)%20],now.count+1));};
if(index == 0&&!a[19]) {yu.push(node(A[19],now.count+1));};
a[index]=true;
}
if(now.station[0]=='B'){
index = find(B,17,now.station);
if(index+1<17&&!b[index+1]) yu.push(node(B[index+1],now.count+1));
if(index-1>=0&&!b[index-1]) yu.push(node(B[index-1],now.count+1));
b[index]=true;
}
if(now.station=="T1"){
if(!b[4]){yu.push(node(B[4],now.count+1));}
if(!b[6]){yu.push(node(B[6],now.count+1));}
if(!a[8]){yu.push(node(A[8],now.count+1));}
if(!a[10]){yu.push(node(A[10],now.count+1));}
b[5]=true;
a[9]=true;
}
if(now.station=="T2"){
if(!b[10]){yu.push(node(B[10],now.count+1));}
if(!b[12]){yu.push(node(B[12],now.count+1));}
if(!a[13]){yu.push(node(A[13],now.count+1));}
if(!a[15]){yu.push(node(A[15],now.count+1));}
b[11]=true;
a[14]=true;
}
}
}
int main(){
string s1,s2;
while (cin>>s1>>s2)
{
cout<<bfs(s1,s2)<<endl;
}
}
测试结果如图
这里要注意,A是环形的地铁,所以用取余。
=====================================
第二天:紧接着就参加机试了
机试
第一题:
将对应数字翻译成字母,不在1~26范围的用?表示,如
输入1 2 3 5 26 27
输出a b c e z ?
分析:这道比较简单,char(a[i]+'a'-1)就好了
第二题:
判断为2的N次方的数字字数
输入 1,2,3,4,5
输出 3
分析:注意里面含有逗号,判断是否是2的N次方用位运算,n&(n-1)==0 则是,用2一个个往上乘的话太慢了,没有过。
第三题:
四则运算,含有+,-,*,/和括号(,)
输入1+2+3
输出 6
输入 1++2
输出wrong
大概是这样的,回忆的
直接上代码吧
#include <iostream>
#include<stack>
#include <string>
using namespace std;
stack<char> op;
stack<int> num;
char s[100];
bool high(char c){
if(op.empty()||c=='('||op.top()=='(') return true;
if((op.top()=='+'||op.top()=='-')&&(c=='*'||c=='/')) return true;
return false;
}
void work(){
if (num.size()<2)
{
return;
}
switch(op.top()){
case '+':{
int a = num.top();num.pop();
int b = num.top();num.pop();
num.push(a+b);
}break;
case '-':{
int a = num.top();num.pop();
int b = num.top();num.pop();
num.push(b-a);
}break;
case '*':{
int a = num.top();num.pop();
int b = num.top();num.pop();
num.push(a*b);
}break;
case '/':{
int a = num.top();num.pop();
int b = num.top();num.pop();
num.push(b/a);
}break;
}
op.pop();
}
int main(){
string s;
cin>>s;
int i = 0;
int sum = 0;
while(i<s.length()){
if(s[i]-'0'>=0&&s[i]-'0'<=9){
while(i<s.length()&&s[i]-'0'>=0&&s[i]-'0'<=9){
sum = sum*10+s[i]-'0';
i++;
}
num.push(sum);
sum = 0;
}
else{
if(s[i]==')'){
while(op.top()!='(')
work();
op.pop();
}
if(high(s[i])){
op.push(s[i]);
}
else{
work();
op.push(s[i]);
}
i++;
}
}
work();
if(op.empty()){
cout<<num.top();
}
else
cout<<"wrong";
}
个人能力有限,不足的地方请大家指出。由于是回忆,也许有些出入,但大体上是这样的^_^
转载请注明出处:http://blog.csdn.net/yuchen_2012/article/details/22607223