版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/x1825048925/article/details/86007055
/**************************************
*Headline: 17年瓜大机试解答
*Author: 周小枫
*Email: [email protected]
*Date: 2019-1-5
*Brief: 这是一份菜鸡帮更菜的鸡写的参
考code,经测试,以下code都OK,如有错误
欢迎大佬指正,
**************************************/
1:计算平均时间
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout<<"please input each number:"<<endl;
int a[2][6];
for(int i=0;i<2;i++)
for(int j=0;j<6;j++)
cin>>a[i][j];
for(int i=0;i<2;i++){
long int sum;
int h,m,s;
if((a[i][0]>11&&a[i][0]<0)||(a[i][3]>11&&a[i][3]<0))
return 0;
if((a[i][0]==0)&&(a[i][3]==11)){
a[i][0]=12; //这里的思路是把时分秒统一成秒单位计数
sum=(a[i][2]+a[i][5])+(a[i][1]+a[i][4])*60+(a[i][0]+a[i][3])*3600;
sum=sum/2; h=sum/3600; //解析工作
m=(sum-h*3600)/60;
s=sum-m*60-h*3600;
cout<<h<<" "<<m<<" "<<s<<endl;
}
else if(abs(a[i][0]-a[i][3])>1)
return 0;
else{
sum=(a[i][2]+a[i][5])+(a[i][1]+a[i][4])*60+(a[i][0]+a[i][3])*3600;
sum=sum/2; h=sum/3600;
m=(sum-h*3600)/60;
s=sum-m*60-h*3600;
cout<<h<<" "<<m<<" "<<s<<endl;
}
}
return 0;
}
2:排序(输入二维不定组int数组调试了好久)
#include <iostream>
#include <vector>
#include <stdio.h>
#include <cstring>
using namespace std;
#define MAXSIZE 20
void mysort(int* a,int m){
int temp;
for(int i=0;i<m-1;i++)
for(int j=0;j<m-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
int main()
{
cout<<"please enter the number of group:"<<endl;
int n;
cin>>n;
if(n<=0)
return 0;
int a[n][MAXSIZE]={0}; //初始化需要置零,方便后续统计有效个数
for(int i=0;i<n;i++){
for(int j=0;j<MAXSIZE;j++){
char c;
scanf("%d%c",&a[i][j],&c); //获取键盘二维不定长数组处理办法
if(c=='\n')
break;
}
}
for(int i=0;i<n;i++){
int cnt=0;
while(a[i][cnt]!=0)
cnt++; //获取数组长度办法
mysort(a[i],cnt); //暂时用冒泡,后面再调试快排
}
for(int i=0;i<n;i++){
int cnt=0;
while(a[i][cnt]!=0)
cnt++;
for(int j=0;j<cnt;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
3:求表达式值
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
using namespace std;
#define MAXSIZE 20
int g_pos;//字符数组的下标
double Translation(char* s,int & pos);
int GetLevel(char ch);
double Operate(double a1, char op, double a2);
double Compute(char* s);
int main()
{
cout<<"please enter the number of group:"<<endl;
int n;
cin>>n;
if(n<=0)
return 0;
char s[n][MAXSIZE];
for(int i=0;i<n;i++)
cin>>s[i];
for(int i=0;i<n;i++)
cout<<Compute(s[i])<<endl;
return 0;
}
/* 字符转数字 */
double Translation(char* s,int & pos)
{
double integer = 0.0; // 整数部分
double remainder = 0.0; // 余数部分
while (s[pos] >= '0' && s[pos] <= '9')
{
integer *= 10;
integer += (s[pos] - '0');
pos++;
}
if (s[pos] == '.')
{
pos++;
int c = 1;
while (s[pos] >= '0' && s[pos] <= '9')
{
double t = s[pos] - '0';
t *= pow(0.1, c);
c++;
remainder += t;
pos++;
}
}
return integer + remainder;
}
/* 返回运算符级别 */
int GetLevel(char ch)
{
switch (ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '(':
return 0;
case '#':
return -1;
};
}
/* 对两个数进行运算 */
double Operate(double a1, char op, double a2)
{
switch (op)
{
case '+':
return a1 + a2;
case '-':
return a1 - a2;
case '*':
return a1 * a2;
case '/':
return a1 / a2;
};
}
/* 利用两个栈进行模拟计算 */
double Compute(char* s)
{
stack<char> optr; // 操作符栈
stack<double> opnd; // 操作数栈
optr.push('#'); //置于符栈顶
int len = strlen(s);
bool is_minus = true; // 判断'-'是减号还是负号, true表示负号
for (g_pos = 0; g_pos < len;)
{
//1. 负号
if (s[g_pos] == '-' && is_minus) // 是负号
{
opnd.push(0);
optr.push('-');
g_pos++;
}
//2. 是右括号 )
else if (s[g_pos] == ')')
{
is_minus = false;
g_pos++;
while (optr.top() != '(')
{
double a2 = opnd.top();
opnd.pop();
double a1 = opnd.top();
opnd.pop();
char op = optr.top();
optr.pop();
double result = Operate(a1, op, a2);
opnd.push(result);
}
optr.pop(); // 删除'('
}
//3. 数字
else if (s[g_pos] >= '0' && s[g_pos] <= '9')
{
is_minus = false;
opnd.push(Translation(s,g_pos));
}
//4. ( 左括号
else if (s[g_pos] == '(')
{
is_minus = true;
optr.push(s[g_pos]);
g_pos++;
}
//5. + - * / 四种
else
{
while (GetLevel(s[g_pos]) <= GetLevel(optr.top())) //当前优先级小于栈尾优先级
{
double a2 = opnd.top();
opnd.pop();
double a1 = opnd.top();
opnd.pop();
char op = optr.top();
optr.pop();
double result = Operate(a1, op, a2);
opnd.push(result);
}
optr.push(s[g_pos]);
g_pos++;
}
}
while (optr.top() != '#')
{
double a2 = opnd.top();
opnd.pop();
double a1 = opnd.top();
opnd.pop();
char op = optr.top();
optr.pop();
double result = Operate(a1, op, a2);
opnd.push(result);
}
return opnd.top();
}
4:括号匹配(同18年)
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
int n;
cout<<"please input the number of group:"<<endl;
cin>>n;
if(n<=0)
return 0;
char s[n][20];
for(int i=0;i<=n;i++)
cin.getline(s[i],20);//获取键盘输入
for(int i=1;i<=n;i++){
bool flag=true;
int len=strlen(s[i]);
char st[len];int top=0;//建造栈
for(int j=0;j<len;j++){
switch(s[i][j]){
case '{':
case '[':
case '(':st[top++]=s[i][j]; break;//老弟入栈了
case '}':
if(st[--top]!='{') //出栈认亲
flag=false;
break;
case ']':
if(st[--top]!='[')
flag=false;
break;
case ')':
if(st[--top]!='(')
flag=false;
break;
}
}
if((flag==true)&&top==0) //栈空且flag为true
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}