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

(题目来源--西工大计算机微信公众号)

.

#include<stdio.h>
#include<stdlib.h>
typedef struct TIME{
	int m;
	int s;
	int ms;
}T;
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
	T a,b,c,aver;
	int sum;
	scanf("%d%d%d%d%d%d%d%d%d",&a.m,&a.s,&a.ms,&b.m,&b.s,&b.ms,&c.m,&c.s,&c.ms);
	sum=((a.m+b.m+c.m)*60*1000+(a.s+b.s+c.s)*1000+a.ms+b.ms+c.ms)/3;
	aver.m=sum/60000;
	aver.s=(sum%60000)/1000;
	aver.ms=(sum%60000)%1000;

	printf("%d %d %d\n",aver.m,aver.s,aver.ms);
}
	return 0;
}

2.排序问题:给定n组数,每组m个,对每组数进行从小到大排序;

Input:
2 4
3 5 2 8
2 7 9 8

Output:
2 3 5 8
2 7 8 9
2017
2018
2019均出现此题

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

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

Output:
2
0.7
9
#include <stdio.h>
#include <string.h>
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.利用海伦公式计算三角形面积

Input:
2
3 4 5
1 1 1
Output:
6
NaN
#include<stdio.h>
#include<math.h>
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
	float a,b,c,area,p;
	scanf("%f %f %f",&a,&b,&c);
	if(a+b<=c||a+c<=b||b+c<=a)
		printf("0.00\n");
	else{
		p=(a+b+c)/2;
		area=sqrt(p*(p-a)*(p-b)*(p-c));
		printf("%.2f",area);
	}
	}
	return 0;
}

 

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值