2014年华为招聘机试重邮第一场
第一个题:
#include <iostream> using namespace std; const int N = 30; int main() { char outputStr[N]; unsigned int inputStr; int i = 0; cin>>inputStr; while (inputStr){ outputStr[i++] = inputStr % 10 + 'a'; inputStr = inputStr / 10; } outputStr[i] = '\0'; for (--i; i >= 0; i--){ cout<<outputStr[i]; } cout<<endl; return 0; }
第二个题:
#include <iostream> using namespace std; typedef struct node{ int num; struct node *next; }nodeList; int main() { nodeList *head = new nodeList; nodeList *p = head; head->num = 1; head->next = NULL; int n, k; cin>>n>>k; for (int i = 2; i < n + 1; i++){ nodeList *node = new nodeList; node->num = i; node->next = NULL; p->next = node; p = node; } p->next = head; p = head; while (p->next != p){ int flag = 1; while (flag < k - 1){ p = p->next; flag++; } nodeList *q = p->next; p->next = q->next; delete q; p = p->next; } cout<<p->num<<endl; delete p; return 0; }
第三个题:
#include <iostream> using namespace std; const int N = 1024; int cac(int num1, int num2, char ch) { if (ch == '+'){ return num1 + num2; }else if (ch == '-'){ return num1 - num2; }else if (ch == '*'){ return num1 * num2; }else if (ch == '/'){ return num1 / num2; } } int main() { char inputStr[N], operation[N]; cin>>inputStr; int i, len = strlen(inputStr), num[N],n1 = 0, n2 = 0; if (len < 3){ cout<<"输入数据不对!"<<endl; } for (i = 0; i < len; i++){ if (inputStr[i] == '+' || inputStr[i] == '-' || inputStr[i] == '*' || inputStr[i] == '/'){ operation[n2++] = inputStr[i]; }else if (inputStr[i] >= '0' && inputStr[i] <= '9'){ int tmp = 0; while (inputStr[i] >= '0' && inputStr[i] <= '9' && i < len){ tmp = tmp * 10 + (inputStr[i] - '0'); i++; } i--; num[n1++] = tmp; } } operation[n2] = '\0'; int num1 = num[0], num2 = num[1], i1 = 2, i2 = 1; char ch = operation[0]; while (i1 < n1 && i2 < n2){ if (ch == '*' || ch == '/' || operation[i2] == '+' || operation[i2] == '-'){ num1 = cac(num1, num2, ch); ch = operation[i2++]; num2 = num[i1++]; }else { num2 = cac(num2, num[i1++], operation[i2++]); } } if (i1 != n1 || i2 != n2){ cout<<"输入数据不对!"<<endl; }else{ cout<<cac(num1, num2, ch)<<endl; } return 0; }