PAT甲级1008,1009解题报告

49 篇文章 0 订阅
47 篇文章 0 订阅

1008 Elevator (20 分)

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41

题目大意:给定一个电梯要停留的层数list,每层要停5s,每上一层要6s,下一层要4s。输出总秒数。一开始没看仔细,把第一个数字也算成指定停留层,还有一个坑是,如果层数相同,也就是不动,也要续5s。

解题思路:很简单的随便模拟一下就好了。

代码如下

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
using namespace std;
int main()
{
	int n;
	int cur = 0;
	int stay = 0;
	int sum = 0;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> cur;
		if (cur >= stay) {
			sum += 6 * (cur - stay) + 5;
		}
		else {
			sum += 4 * (stay - cur) + 5;
		}
		stay = cur;
	}
	cout << sum << endl;
	return 0;
}

1009.

1009 Product of Polynomials (25 分)

This time, you are supposed to find A×B where A and B are two polynomials.

Input Specification:

Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial:

K N​1​​ a​N​1​​​​ N​2​​ a​N​2​​​​ ... N​K​​ a​N​K​​​​

where K is the number of nonzero terms in the polynomial, N​i​​ and a​N​i​​​​ (i=1,2,⋯,K) are the exponents and coefficients, respectively. It is given that 1≤K≤10, 0≤N​K​​<⋯<N​2​​<N​1​​≤1000.

Output Specification:

For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.

Sample Input:

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output:

3 3 3.6 2 6.0 1 1.6

题目大意:很容易理解,模拟一下多项式的乘法。保留一位小数的系数和次数从大到小输出一下就好了。

解题思路:没想到什么高效的算法,可能和矩阵链乘法的方法相关,看题目要求数据不高就随便搞个暴力的三重循环算了。要注意一点就是如果结果为0的话直接输出0.

代码如下:

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
using namespace std;
struct poly {
	int count;
	double num;
};
poly Mulpoly(poly a, poly b) {
	poly res;
	res.count = a.count + b.count;
	res.num = a.num*b.num;
	return res;
}
bool comp(poly a, poly b) {
	return a.count >= b.count;
}
int main()
{
	int Ka, Kb;
	vector<poly> a;
	vector<poly> b;
	vector<poly> res;
	cin >> Ka;
	for (int i = 0; i < Ka; i++) {
		poly cur;
		cin >> cur.count >> cur.num;
		a.push_back(cur);
	}
	cin >> Kb;
	for (int i = 0; i < Kb; i++) {
		poly cur;
		cin >> cur.count >> cur.num;
		b.push_back(cur);
	}
	for (auto i = a.begin(); i != a.end(); i++) {
		for (auto j = b.begin(); j != b.end(); j++) {
			poly cur = Mulpoly(*i, *j);
			bool flag = false;
			for (auto m = res.begin(); m != res.end(); m++) {
				if ((*m).count == cur.count) {
					(*m).num = cur.num + (*m).num;
					if ((*m).num == 0) {
						res.erase(m);
					}
					flag = true;
					break;
				}
			}
			if (!flag) {
				res.push_back(cur);
			}
		}

	}
	sort(res.begin(), res.end(), comp);
	if (res.size() == 0)cout << 0 << endl;
	else {
		cout << res.size() << " ";
		for (auto i = res.begin(); i != res.end(); i++) {
			if(i!=res.end()-1)
			cout << (*i).count <<" " <<setiosflags(ios::fixed) << setprecision(1) << (*i).num << " ";
			else
				cout << (*i).count <<" "<< setiosflags(ios::fixed) << setprecision(1) << (*i).num << endl;;
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值