2017-西工大计算机复试-机试代码参考

题目来源-西工大计算机

1.输入两组时间(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<stdio.h>
#include<stdlib.h>
typedef struct TIME{
	int h;
	int m;
	int s;
}T;//定义一个关于时间的结构体;
int main(){
	int n=2;//两组数据
	while(n--){
			T a,b,aver;
			int sum;
	                scanf("%d%d%d%d%d%d",&a.h,&a.m,&a.s,&b.h,&b.m,&b.s);
	                if(a.h==0)//0代表12
				a.h=12;
			if(b.h==0)//0代表12
				b.h=12;
	                sum=((a.h+b.h)*60*60+(a.m+b.m)*60+(a.s+b.s))/2;//把时间和化成秒为单位
	                aver.h=sum/3600;//平均时间的小时位
	                aver.m=(sum%3600)/60;//平均时间的分钟位
	                aver.s=(sum%3600)%60;//平均时间的秒位
	                printf("%d %d %d\n",aver.h,aver.m,aver.s);
	}
	system("pause");
	return 0;
}

2.排序,输入n组数,由小到大进行排序

输入样例:
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

参考代码:

#include<stdio.h>
void quicksort(int data[],int begin,int end)
{
    //变量定义及初始化
    int i = begin,j = end;
    int key,temp;
    key = data[begin];

    //递归跳出条件
    if(begin >= end)
        return;

    //快速排序算法
    while(i < j)
    {
        while(i < j && key <= data[j])
            j--;

        if(key >= data[j])
        {
            temp = data[i];
            data[i] = data[j];
            data[j] = temp;
        }

        while(i < j && key >= data[i])
            i++;

        if(key <= data[i])
        {
            temp = data[i];
            data[i] = data[j];
            data[j] = temp;
        }
    }

    //快速排序的二分法
    quicksort(data,begin,i-1);
    quicksort(data,j+1,end);
}
int main()
{
    int n,data[100],i,length;
    scanf("%d",&n);
    char ch;

    while(n--)
    {
        length = 0;
        do
        {
            scanf("%d",&data[length]);
            length++;
        }while((ch=getchar())!= '\n');

        quicksort(data,0,length-1);

        for(i = 0;i < length;i++)
            printf("%d ",data[i]);
        printf("\n");
    }
}

3.输入行数,再在每行输入一个表达式,得出结果

Input:
3
1+1
2.2/3
1+2*3+2

Output:
2
0.7
9

参考代码1:

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int n;
    scanf("%d",&n);
        while(n--)
        {
            double a[100],sum=0;
            char c;
            int i=0;
            scanf("%lf",&a[0]);
            c=getchar();
            while(c!='\n')
            {
                double temp;
                scanf("%lf",&temp);
                switch(c)
                {
                    case '+':a[++i]=temp;break;
                    case '-':a[++i]=-temp;break;
                    case '*':a[i]*=temp;break;
                    case '/':a[i]/=temp;break;
                }
                c=getchar();
            }
            for(int j=0;j<=i;j++)
            {
                sum=sum+a[j];
            }
            if(sum-int(sum)<=1e-6) printf("%.0f\n",sum);
            else printf("%.1f\n",sum);
        }
    return 0;
}

参考代码2:(栈)

用两个栈分别压数字和运算符;
如果当前运算符优先级('*/')高于栈顶运算符('+-')优先级,则将运算符入栈;反之,从数字栈中弹出两个数,从运算符栈中弹出栈顶运算符,进行运算,数字栈压入运算结果,符号栈压入当前运算符。重复该操作直到不满足条件。
出现左括号,则直接压入;出现右括号,则从数字栈中弹出两个数,从运算符栈中弹出栈顶运算符,进行运算,数字栈压入运算结果,重复该操作直到栈顶弹出右括号位置。

#include <iostream>
#include <stack>
using namespace std;

string mp = "+-*/)]}";
// 当前运算符与符号栈的栈顶运算符做优先级比较,如果当前优先级高,则不做运算压入栈中,相同进行运算
bool cmp(char c1, char c2)
{
    if (c1 =='(') {
        return false;
    } else if((c1=='+' || c1=='-') && (c2=='*' || c2=='/')){
        return false;
    }
    return true;
}

void doCal(stack<double> &st, stack<char> &so)
{
    double b = st.top();
    st.pop();
    double a = st.top();
    st.pop();
    int op = so.top();
    so.pop();
    if(op == '+') a = a+b;
    else if(op == '-') a = a-b;
    else if(op == '*') a = a*b;
    else if(op == '/') a = a/b;
    st.push(a);
    return ;
}

int main()
{
    string s;
    while(getline(cin, s))
    {
        stack<double> st;
        stack<char> so;
        so.push('(');
        s += ')';
        bool nextIsOp = false;
        for(int i = 0; i < s.size(); i++)
        {
            if(s[i]=='{' || s[i]=='[' || s[i]=='(') {
                so.push('(');
            } else if(s[i]==')' || s[i]==']' || s[i]=='}') {
                while(so.top() != '(') doCal(st, so);
                so.pop();
            } else if (nextIsOp) {
                while(cmp(so.top(), s[i])) doCal(st, so);
                so.push(s[i]);
                nextIsOp = false;
            } else {
                int j = i;
                if(s[j] == '-' || s[j] == '+') i++;
                while(mp.find(s[i]) == mp.npos) i++;
                string t = s.substr(j, i-j);
                st.push((double)stoi(t));
                i--;
                nextIsOp = true;
            }
        }
        cout << st.top() << endl;
    }
    return 0;
}

4.括号匹配,输入测试行数n,每行输入一个样例,判断是否匹配,是则输出yes,否则输出no。

Input:
2
([89])
([{}])
(({{}}

Output:
yes
yes
no

此处是栈的应用,只需要建立一个栈,此处使用vector容器作为栈的容器。

  1. 建立一个栈;
  2. 遍历string,遇到左括号入栈,遇到右括号则与栈顶元素比较;
  3. 如果栈顶元素与遍历到的元素不匹配,false;
  4. 遍历完成,如果栈内还有元素,false;
  5. 其他情况true
class Solution {
public:
    /**
     *
     * @param s string字符串
     * @return bool布尔型
     */
    bool isValid(string s) {
        // write code here
        /** 遍历括号

         */
        // 每个括号的标识
        const char b1 = '{', b2 = '}',
            m1 = '[', m2 = ']',
            s1 = '(', s2 = ')';

        // 存储目前括号的容器
        vector<char> cvec;

        // 遍历字符串
        for (auto c : s) {
            switch (c) {
            case m1:
            case b1:
            case s1:
                cvec.push_back(c);
                break;
            case m2:
                // 防止访问越界
                if (0 != cvec.size() && *(cvec.end() - 1) == m1) {
                    cvec.pop_back();
                } else {
                    return false;
                }
                break;
            case b2:
                if (0 != cvec.size() && *(cvec.end() - 1) == b1) {
                    cvec.pop_back();
                } else {
                    return false;
                }
                break;
            case s2:
                if (0 != cvec.size() && *(cvec.end() - 1) == s1) {
                    cvec.pop_back();
                } else {
                    return false;
                }
                break;
                // 若非符号,则下一个
            default:
                continue;
            }
        }

        // 容器最后无内容,匹配成功
        if (0 == cvec.size()) {
            return true;
        }

        // 默认返回false
        return false;

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值