PAT 第二周 (1005-1010)

稍微熟练后又遇上这周的题目很是简单,观察了这两天做的六道题并不是一组,都是较简单的题,简单整理一下。

目录

1005 Spell It Right

1006 Sign In and Sign Out

1007 Maximum Subsequence Sum

1008 Elevator

1009 Product of Polynomials

1010 Radix


1005 Spell It Right

Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.

Input Specification:

Each input file contains one test case. Each case occupies one line which contains an N (≤{\color{Red}10^{100} }).

Output Specification:

For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.

Sample Input:

12345

Sample Output:

one five

本题实质是一个简单的高精度加法问题。

【本题代码】

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	char c = getchar();
	vector<int> str;
	vector<int> result;
	while (c != '\n') {
		str.push_back(int(c-'0'));
		c = getchar();
	}
	result.push_back(str[0]);
	for (int i = 1; i < str.size(); i++)
	{
		int add = str[i];
		for (int j = 0; j < result.size(); j++) {
			int temp = result[j] + add;
			if (temp < 10) {
				result[j] += add;
				break;
			}
			else {
				result[j] += add - 10;
				if (j == result.size() - 1) {
					result.push_back(1);
					break;
				}
				else add = 1;
			}
		}
	}
	for (int i = result.size() - 1; i >= 0; i--)
	{
		switch (result[i])
		{
		case 0:cout << "zero"; 
			break;
		case 1:cout << "one";
			break;
		case 2:cout << "two";
			break;
		case 3:cout << "three";
			break;
		case 4:cout << "four";
			break;
		case 5:cout << "five";
			break;
		case 6:cout << "six";
			break;
		case 7:cout << "seven";
			break;
		case 8:cout << "eight";
			break;
		case 9:cout << "nine";
			break;
		default:
			break;
		}
		if (i != 0) 
			cout << " ";
	}
	return 0;
}

1006 Sign In and Sign Out

At the beginning of every day, the first person who signs in the computer room will unlock the door, and the last one who signs out will lock the door. Given the records of signing in's and out's, you are supposed to find the ones who have unlocked and locked the door on that day.

Input Specification:

Each input file contains one test case. Each case contains the records for one day. The case starts with a positive integer M, which is the total number of records, followed by M lines, each in the format:

ID_number Sign_in_time Sign_out_time

where times are given in the format HH:MM:SS, and ID_number is a string with no more than 15 characters.

Output Specification:

For each test case, output in one line the ID numbers of the persons who have unlocked and locked the door on that day. The two ID numbers must be separated by one space.

Note: It is guaranteed that the records are consistent. That is, the sign in time must be earlier than the sign out time for each person, and there are no two persons sign in or out at the same moment.

Sample Input:

3
CS301111 15:30:28 17:00:10
SC3021234 08:00:00 11:25:25
CS301133 21:45:00 21:58:40

Sample Output:

SC3021234 CS301133

本题的实质是排序问题。 用快速排序如下。

【本题代码】

#include<iostream>
#include<algorithm>
using namespace std;
void Sort(string*, string*, string*,int, int);
void swap(string&, string&);
bool cmp(string, string);
int main()
{
	int M;
	cin >> M;
	string* s1 = new string[M];
	string* s2 = new string[M];
	string* s3 = new string[M];
	for (int i = 0; i < M; i++)
		cin >> s1[i] >> s2[i] >> s3[i];
	Sort(s1, s3, s2, 0, M - 1);
	cout << s1[0] << " ";
	Sort(s1, s2, s3, 0, M - 1);
	cout << s1[M-1];
	return 0;
}
void Sort(string* s1, string* s2, string* s,int left,int right)
{
	if (left >= right)
		return;
	int index = left+1;
	for (int i = left + 1; i <= right; i++)
	{
		if (cmp(s[left], s[i])) {
			swap(s[index], s[i]);
			swap(s1[index], s1[i]);
			swap(s2[index], s2[i]);
			index++;
		}
	}
	swap(s[index-1], s[left]);
	swap(s1[index-1], s1[left]);
	swap(s2[index - 1], s2[left]);
	Sort(s1, s2, s, left, index - 2);
	Sort(s1, s2, s, index, right);
}
void swap(string& a, string& b)
{
	string temp = a;
	a = b;
	b = temp;
}
bool cmp(string a, string b)
{
	if (a[0] * 10 + a[1] > b[0] * 10 + b[1])
		return true;
	else if (a[0] * 10 + a[1] < b[0] * 10 + b[1])
		return false;
	else if (a[3] * 10 + a[4] > b[3] * 10 + b[4])
		return true;
	else if (a[3] * 10 + a[4] < b[3] * 10 + b[4])
		return false;
	else if (a[6] * 10 + a[7] > b[6] * 10 + b[7])
		return true;
	else if (a[6] * 10 + a[7] < b[6] * 10 + b[7])
		return false;
	else return true;
}

1007 Maximum Subsequence Sum

Given a sequence of K integers \left \{ N_1,N_2,...,N_K \right \}. A continuous subsequence is defined to be \left \{ N_i,N_{i+1},...,N_j \right \} where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (≤10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

本题的实质为最大子串和问题。其实它是动态规划经典问题max(a[i],sum[i-1]+a[i]),但我写的时候还没意识到。

另外,本题需要注意的一点是对于N个数均为负数的情况,需要单独处理。

【本题代码】

#include<iostream>
using namespace std;
int main()
{
	int K, key = 0;
	cin >> K;
	int* a = new int[K];
	for (int i = 0; i < K; i++)
	{
		cin >> a[i];
		if (a[i] >= 0)key = 1;
	}
	if (key == 0)
		cout << 0 << " " << a[0] <<" "<< a[K - 1];
	else {
		int* sum = new int[K];
		sum[0] = a[0];
		int begin = 0;
		int end = 0;
		int max = sum[0];
		int x = 0, y = 0;
		for (int j = 1; j < K; j++)
		{
			if (sum[j - 1] < 0) {
				begin = end = j;
				sum[j] = a[j];
			}
			else {
				sum[j] = sum[j - 1] + a[j];
				end = j;
			}
			if (sum[j] > max)
			{
				max = sum[j];
				x = begin;
				y = end;
			}
		}
		cout << max << " " << a[x] << " " << a[y];
	}
}

1008 Elevator

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

 本题就是一道小学数学题,不愿多说,我和我班上大一孩子说来着不会做就是没好好读题而已。

【本题代码】

#include<iostream>
using namespace std;
/*
电梯6s上升一层,4s下降一层,每层停5s
*/
int main()
{
	int N;
	cin >> N;
	int* a = new int[N];
	int sum = 0;
	int temp = 0;
	for (int i = 0; i < N; i++)
	{
		cin >> a[i];
		if (a[i] - temp > 0)
			sum += 5 + (a[i] - temp) * 6;
		else
			sum += 5 + (temp - a[i]) * 4;
		temp = a[i];
	}
	cout << sum;
	return 0;
}

1009 Product of Polynomials

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\leq K\leq 10, 0\leq N_{K}<...<N_{2}<N_{1}\leq 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

本题和1002异曲同工,从多项式加法转变为多项式乘法,其实反而更简单,最暴力的方法就行,两层循环解决。

【本题代码】

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
	int K1,K2;
	cin >> K1;
	int* a = new int[K1];
	double* aa = new double[K1];
	for (int i = 0; i < K1; i++)
		cin >> a[i] >> aa[i];
	cin >> K2;
	int* b = new int[K2];
	double* bb = new double[K2];
	for (int i = 0; i < K2; i++)
		cin >> b[i] >> bb[i];
	double* m = new double[a[0] + b[0] + 1];
	for (int i = 0; i <= a[0] + b[0]; i++)
		m[i] = 0.0;
	for (int i = 0; i < K1; i++)
		for (int j = 0; j < K2; j++)
			m[a[i] + b[j]] += aa[i] * bb[j];
	int num = 0, key = 0;
	for (int i = a[0] + b[0]; i >= 0; i--) 
		if (m[i] != 0) 
			num++;
	cout << num << " ";
	for (int i = a[0] + b[0]; i >= 0; i--) {
		if (m[i] != 0) {
			if (key != 0)
				cout << " ";
			cout << fixed << setprecision(0) << i << setprecision(1) << " " << m[i];
			key++;
		}
	}
	return 0;
}

1010 Radix

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N_1 and N_2​, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:

N1 N2 tag radix

Here {\color{DarkGreen} N_1} and {\color{DarkGreen} N_2} each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number radix is the radix of {\color{DarkGreen} N_1} if tag is 1, or of {\color{DarkGreen} N_2} if tag is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation {\color{Pink} N_1=N_2} is true. If the equation is impossible, print Impossible. If the solution is not unique, output the smallest possible radix.

Sample Input 1:

6 110 1 10

Sample Output 1:

2

Sample Input 2:

1 ab 1 2

Sample Output 2:

Impossible

本题是六道题中花费时间最久的,主要源于以下几点原因:

N_1N_2的范围超过int,可用long long int解决,若超过long long int 范围可用<0来判断;

A digit is less than its radix,基数的下界最大的digit+1,不可单纯从2开始枚举;

③ 逐一增加基数枚举的算法会导致测试点7超时,可用二分法解决,由此需确定基数的上界N_1/N_2的值,因为任何基数的0次方都是1。

【本题代码】

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
long long int minRadix(string x);
long long int cal(string x,long long int radix);//int范围,测试样例中会有越界的
int main()
{
	string x, y;
	int tag;
	long long int radix, m1, m2;
	cin >> x >> y >> tag >> radix;
	if (tag == 1) {
		m1 = cal(x, radix); 
		long long int radix1, radix2;
		radix1 = minRadix(y);//不能从2开始枚举,因为digit要<radix且暴力枚举会超时
		radix2 = max(m1, radix1);
		while (radix1<=radix2) {
			long long int mid = (radix1 + radix2) / 2;
			m2 = cal(y, mid);
			if (m2 == m1) {
				cout << mid;
				return 0;
			}
			else if (m2 > m1 || m2 < 0)
				radix2 = mid - 1;
			else radix1 = mid + 1;
		}
	}
	else if (tag == 2) {
		m1 = cal(y, radix);
		long long int radix1, radix2;
		radix1 = minRadix(x);
		radix2 = max(m1, radix1);
		while (radix1 <= radix2) {
			long long int mid = (radix1 + radix2) / 2;
			m2 = cal(x, mid);
			if (m2 == m1) {
				cout << mid;
				return 0;
			}
			else if (m2 > m1 || m2 < 0)
				radix2 = mid - 1;
			else radix1 = mid + 1;
		}
	}
	cout << "Impossible";
	return 0;
}
long long int cal(string x,long long int radix)
{
	long long int m = 0;
	for (int i = 0; i < x.size(); i++) {
		if(x[i]<=57)
			m += (x[i] - '0') * pow(double(radix), x.size() - 1 - i);
		else
			m += (x[i] - 87) * pow(double(radix), x.size() - 1 - i);//a代表10
	}
	return m;
}
long long int minRadix(string x)
{
	long long int m = 0, max = 0;
	for (int i = 0; i < x.size(); i++) {
		if (x[i] <= 57)
			m = (x[i] - '0');
		else
			m = (x[i] - 87);
		if (m > max)
			max = m;
	}
	return max + 1;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值