前言:西工大机试真题记录
代码在CodeBlocks16.01环境下编译通过
第一题-输入两组时间(h,m,s),计算平均时间,两组时间不超过一个小时,h在0-11之间
/*
*题目:输入两组时间(h,m,s),计算平均时间,两组时间不超过一个小时,h在0-11之间
*Input:
1 20 30 1 30 30
0 20 30 11 30 30
*Output:
1 25 30
11 55 30
*/
#include <iostream>
#include <stdio.h>
using namespace std;
int NoOne()
{
int num;
int hourOne, minOne, secOne, hourTwo, minTwo, secTwo;
int secSum = 0;
int avrgHour, avrgMin, avrgSec;
cin >> num;
getchar();
while (num--)
{
cin >> hourOne >> minOne >> secOne >> hourTwo >> minTwo >> secTwo;
if (hourOne == 0 && hourTwo == 11)
{
hourOne = 12;
}
if (hourTwo == 0 && hourOne == 11)
{
hourTwo = 12;
}
secSum = hourOne * 60 * 60 + minOne * 60 + secOne + hourTwo * 60 * 60 + minTwo * 60 + secTwo;
secSum /= 2;
avrgHour = secSum / 3600;
avrgMin = secSum % 3600 / 60;
avrgSec = secSum % 3600 % 60;
if (avrgHour == 12)
{
avrgHour = 0;
}
cout << avrgHour << " " << avrgMin << " " << avrgSec;
cout << endl;
}
return 0;
}
第二题-排序,输入n组数,由小到大进行排序
Input:
2
1 5 8 6 3 2 0
4 2 3 8 15 63 20 1
Output:
0 1 2 3 5 6 8
1 2 3 4 8 15 20 63
见18年真题第四题,提供了五种解法 西工大18年机试真题(点这里)
第三题-题目:输入行数,再在每行输入一个表达式,得出结果(逻辑可能有点乱,有时间再重构吧,总体思想是用了一个数字栈和一个操作符栈,进行优先级判断,本代码优点是可以嵌套小括号,中括号和大括号,缺点是没办法进行负数运算)
/*
*题目:输入行数,再在每行输入一个表达式,得出结果
*Input:
3
1+1
2.2/3
1+2*3+2
*Output:
2
0.7
9
*/
#include <iostream>
#include <stdio.h>
#include <stack>
#include <math.h>
#include <sstream>
using namespace std;
/*************判断 "+-/*([{" 的优先级函数**************/
int OprtPriority(char op)
{
int priority = 0;
switch (op)
{
case '+':
case '-':
priority = 1;
break;
case '/':
case '*':
priority = 2;
break;
case '{':
priority = 3;
break;
case '[':
priority = 4;
break;
case '(':
priority = 5;
break;
default:
priority = 0;
break;
}
return priority;
}
/*****数栈弹出的两个数和操作符栈弹出的操作符进行运算函数*****/
double CalEqu(double val1, double val2, char op)
{
double result = 0;
switch (op)
{
case '/':
result = val1 / val2;
result = round(result * 10) / 10; //保留小数点后一位
break;
case '*':
result = val1 * val2;
break;
case '+':
result = val1 + val2;
break;
case '-':
result = val1 - val2;
break;
default:
break;
}
return result;
}
int NoThree()
{
int num; //测试用例组数
string strData; //获取输入9以上的数字的中间转换
double data; //保存输入式子中大于等于10的数字
double leftVal, rightVal; //数字栈弹出的两个操作数
double tempVal; //保存CalEqu()函数的结果
char op; //保存操作符栈弹出的操作符
string equation; //输入的表达式
stringstream str2dbl; //string转换成int的流
stack<double> number; //数字栈
stack<char> oprt; //操作符栈
cin >> num;
getchar();
while (num--)
{
while (!number.empty()) //清空数字栈和操作符栈
{
number.pop();
}
while (!oprt.empty())
{
oprt.pop();
}
getline(cin, equation);
oprt.push('('); //给总的表达式套上一个(),有利于边界判断
equation += ')';
for (auto &i : equation)
{
if ()
if (isdigit(i) || i == '.') //提取表达式中的数字
{
strData += i;
}
else
{
if (!strData.empty()) //将提取到的数字入数字栈
{
str2dbl << strData;
str2dbl >> data;
number.push(data);
strData.erase();
str2dbl.clear();
}
/*左括号外的操作符优先级小于左括号,左括号内的操作符优先级大于左括号*/
if (oprt.top() == '(' || oprt.top() == '[' || oprt.top() == '{')
{
oprt.push(i);
}
/*如果出现右括号,则不停弹出两个数字栈顶和操作符栈顶进行运算,直到操作符栈顶为左括号*/
else if (i == ')' || i == ']' || i == '}')
{
while (oprt.top() != '(' && oprt.top() != '[' && oprt.top() != '{')
{
op = oprt.top();
oprt.pop();
rightVal = number.top();
number.pop();
leftVal = number.top();
number.pop();
tempVal = CalEqu(leftVal, rightVal, op);
number.push(tempVal);
}
oprt.pop();
}
/*若此时操作符的优先级高于操作符栈顶的优先级,则该操作符进栈,否则栈顶出栈*/
else
{
while (OprtPriority(i) <= OprtPriority(oprt.top()) && oprt.top() != '(' && oprt.top() != '[' && oprt.top() != '{')
{
op = oprt.top();
oprt.pop();
rightVal = number.top();
number.pop();
leftVal = number.top();
number.pop();
tempVal = CalEqu(leftVal, rightVal, op);
number.push(tempVal);
}
if (oprt.top() == '(' || oprt.top() == '[' || oprt.top() == '{' || (OprtPriority(i) > OprtPriority(oprt.top())))
{
oprt.push(i);
}
}
}
}
/*最后剩下的数字栈顶即为所求表达式的值*/
cout << number.top() << endl;
}
return 0;
}
第四题-括号匹配,输入测试行数n,每行输入一个样例,判断是否匹配,是则输出yes,否则输出no。
见18年真题第七题,用了栈 西工大18年机试真题第七题(点这里)