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

1

题目描述

小王围操场跑圈,每次跑三圈,请输入行数,每行输入九个整数,表示每圈所用的分,秒,和毫秒。输出每次跑步所用的平均时间。

输入

2
1 10 100 1 20 100 1 30 100
2 10 100 1 20 100 1 30 100

输出

1 20 100
1 40 100

参考代码

#include <cstdio>
#include <cstring>

int main(){
	int n;		//跑步次数
	scanf("%d", &n);
	int ans[n];
	memset(ans, 0, sizeof(ans));		//初始化数组
	int m, s, ms;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < 3; j++){
			scanf("%d%d%d", &m, &s, &ms);
			ans[i] += m * 60000 + s * 1000 + ms;
		}
		ans[i] /= 3;
	}
	
	for(int i = 0; i < n; i++)
		printf("%d %d %d\n", ans[i] / 60000, ans[i] % 60000 / 1000, ans[i] % 1000);
		
	return 0;
}

2

题目描述

排序:输入行数,在每行输入几个整数,再对每行进行排序。

输入

2
5 7 3 4
9 8 5

输出

3 4 5 7
5 8 9

参考代码

#include <cstdio>
const int maxn = 256;

void QuickSort(int a[], int low, int high){
	int i, j, temp;
	i = low;
	j = high;
	if(low < high){
		temp = a[low];
		while(i < j){
			while(i < j && a[j] >= temp) --j;
			if(i < j){
				a[i] = a[j];
				i++;
			}
			while(i < j && a[i] < temp) ++i;
			if(i < j){
				a[j] = a[i];
				--j;
			}
		}
		a[i] = temp;
		QuickSort(a, low, i - 1);
		QuickSort(a, i + 1, high);
	}
}

int main(){
	int n;
	scanf("%d", &n);
	int a[maxn], i;
	char c;
	while(n--){
		i = 0;
		do{
			scanf("%d", &a[i]);
			i++;
		}while((c=getchar()) != '\n');
		QuickSort(a, 0, i - 1);
		for(int j = 0; j < i; j++)
			printf("%d ", a[j]);
		printf("\n");
	}
	
	return 0;
}

3

题目描述

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

输入

3
1+1
2.2/3
1+2*3

输出

2
0.7
7

参考代码

#include <iostream>
#include <string>
#include <stack>
#include <stdlib.h>
#include <vector>
#include <algorithm>
using namespace std;


// 字符串预处理,-2+3、(-3+2),负号前添加0
string Format(string str)
{
	for(int i = 0; i < str.length(); i++)
	{
		if(str[i] == '-')
		{
			if(i == 0)
				str.insert(0, 1, '0');
			else if(str[i-1] == '(')
				str.insert(i, 1, '0');
		}
	}
	return str;
}

// 确定操作符优先级
int Prior(char c)
{
	if(c == '+' || c == '-')
		return 1;
	if(c == '*' || c == '/')
		return 2;
	else
		return 0;
}

// 中缀表达式转后缀表达式
vector<string> Change(string str)
{
	vector<string> vs;
	stack<char> st;
	for(int i = 0; i < str.length(); i++)
	{
		string temp = "";
		switch(str[i])
		{
			case '+':
			case '-':
			case '*':
			case '/':
				if(st.empty() || st.top() == '(')
					st.push(str[i]);
				else
				{
					while(!st.empty() && Prior(st.top()) >= Prior(str[i]))		//栈非空且栈顶优先级不小于栈外优先级,则弹出栈顶操作符
					{
						temp += st.top();
						vs.push_back(temp);
						st.pop();
						temp = "";
					}
					st.push(str[i]);				//操作完成,该操作符压入栈中
				}
				break;
			case '(':
				st.push(str[i]);
				break;
			case ')':
				while(st.top() != '(')
				{
					temp += st.top();
					vs.push_back(temp);
					st.pop();
					temp = "";				
				}
				st.pop();
				break;
			default:
				if(str[i] >= '0' && str[i] <= '9')
				{
					temp += str[i];
					while(i+1 < str.size() && ((str[i+1] >= '0' && str[i+1] <= '9') || str[i+1] == '.'))
					{
						temp += str[i+1];
						++i;
					}
					vs.push_back(temp);
				}
		}
	}
	while(!st.empty())
	{
		string temp = "";
		temp += st.top();
		vs.push_back(temp);
		st.pop();
	}
	return vs;
}


double Result(vector<string> bh)
{
	stack<double> st;
	double num, op1, op2;
	for(int i = 0; i < bh.size(); i++)
	{
		string temp = bh[i];
		if(temp[0] >= '0' && temp[0] <= '9')
		{
			num = atof(temp.c_str());
			st.push(num);
		}
		else if(bh[i] == "+")
		{
			op2=st.top();
            st.pop();
            op1=st.top();
            st.pop();
            st.push(op1 + op2);
		}
		else if(bh[i] == "-")
		{
			op2=st.top();
            st.pop();
            op1=st.top();
            st.pop();
            st.push(op1 - op2);
		}
		else if(bh[i] == "*")
		{
			op2=st.top();
            st.pop();
            op1=st.top();
            st.pop();
            st.push(op1 * op2);
		}
		else if(bh[i] == "/")
		{
			op2=st.top();
            st.pop();
            op1=st.top();
            st.pop();
            st.push(op1 / op2);
		}
	}
	return st.top();
}

void Solve(string str)
{
	str = Format(str);
	vector<string> bh = Change(str);
	double k = Result(bh);
	if((int)k == k)
		cout<<k<<endl;
	else
	{
		printf("%.1f", k);
		cout<<endl;
	}
}

int main()
{
	string str;
	int n;
	cin>>n;
	while(n--)
	{
		cin>>str;
		Solve(str);
	}
}

4

同一个题目

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值